import { STRINGS } from './l10n-strings' ; import { isBrowser } from '../common/capabilities' ; interface L10n { locale : string ; _locale : string ; strings : Record < string , Record < string , string > > ; merge ( locale , strings ? ) : void ; } export const l10n = { strings : STRINGS , _locale : 'en' , get locale ( ) : string { if ( ! l10n . _locale ) { l10n . _locale = ( isBrowser ( ) ? navigator . language . slice ( 0 , 5 ) : null ) ?? 'en' ; } return l10n . _locale ; } , set locale ( value ) { l10n . _locale = value ; } , merge ( locale , strings ? ) { if ( locale && strings ) { const savedLocale = l10n . _locale ; l10n . locale = locale as string ; l10n . strings [ locale as string ] = { ... l10n . strings [ locale as string ] , ... strings , } ; l10n . locale = savedLocale ; } else if ( locale && ! strings ) { for ( const l of Object . keys ( locale as Record < string , Record < string , string > > ) ) { l10n . merge ( l , locale [ l ] ) ; } } } , } ; /**
 * Return a localised string for the `key`.
 */ export function localize ( key ? ) { if ( key === undefined ) return undefined ; const language = l10n . locale . slice ( 0 , 2 ) ; let result = '' ; if ( l10n . strings [ l10n . locale ] ) result = l10n . strings [ l10n . locale ] [ key ] ; if ( ! result && l10n . strings [ language ] ) result = l10n . strings [ language ] [ key ] ; if ( ! result ) result = l10n . strings . en [ key ] ; if ( ! result ) return undefined ; return result ; }