diff --git a/src/i18n/main.ts b/src/i18n/main.ts index 84221a08c..43b5ba94b 100644 --- a/src/i18n/main.ts +++ b/src/i18n/main.ts @@ -15,21 +15,18 @@ const getLng = async (): Promise => { const locale = app.getSystemLocale(); - let [languageCode, countryCode] = locale.split(/[-_]/) as [ - string, - string | null, - ]; + let [languageCode, countryCode] = locale.split(/[-_]/); if (!languageCode || languageCode.length !== 2) { return fallbackLng; } languageCode = languageCode.toLowerCase(); - if (!countryCode || countryCode.length !== 2) { - countryCode = null; - } else { - countryCode = countryCode.toUpperCase(); - } + const isCountryCodeInexistentOrNonStandard = + !countryCode || countryCode.length !== 2; + countryCode = isCountryCodeInexistentOrNonStandard + ? '' + : countryCode.toUpperCase(); const lng = countryCode ? `${languageCode}-${countryCode}` : languageCode; @@ -37,7 +34,9 @@ const getLng = async (): Promise => { return lng; } - return undefined; + return Object.keys(resources).find((language) => + language.startsWith(languageCode) + ) as keyof typeof resources | undefined; }; export let getLanguage = 'en';