fix: fallback to en if invalid locale is set

pull/3281/head
Ryan Miller 3 months ago
parent 7862301a5b
commit 4a04dc68ff
No known key found for this signature in database
GPG Key ID: D21C44D95F6A5495

@ -93,3 +93,6 @@ export const crowdinLocales = [
export type CrowdinLocale = (typeof crowdinLocales)[number];
export function isCrowdinLocale(locale: string): locale is CrowdinLocale {
return crowdinLocales.includes(locale as CrowdinLocale);
}

@ -1,6 +1,6 @@
import type { SetupI18nReturnType } from '../types/localizer';
import { setupI18n } from '../util/i18n/i18n';
import { CrowdinLocale } from '../localization/constants';
import {CrowdinLocale, isCrowdinLocale} from '../localization/constants';
export function normalizeLocaleName(locale: string) {
const dashedLocale = locale.replaceAll('_', '-');
@ -27,6 +27,15 @@ export function normalizeLocaleName(locale: string) {
return dashedLocale;
}
function resolveLocale(crowdinLocale: string): CrowdinLocale {
const locale = normalizeLocaleName(crowdinLocale)
if (isCrowdinLocale(locale)) {
return locale;
}
console.error(`Invalid locale: ${locale} falling back to en`);
return 'en' as CrowdinLocale;
}
export function loadLocalizedDictionary({
appLocale,
logger,
@ -50,7 +59,7 @@ export function loadLocalizedDictionary({
//
// possible locales:
// https://github.com/electron/electron/blob/master/docs/api/locales.md
const crowdinLocale = normalizeLocaleName(appLocale) as CrowdinLocale;
const crowdinLocale = resolveLocale(appLocale);
const i18n = setupI18n({
crowdinLocale,

Loading…
Cancel
Save