From 4a04dc68ffe81c24a3ba1f9c647958d96ac17b50 Mon Sep 17 00:00:00 2001 From: Ryan Miller Date: Thu, 30 Jan 2025 11:39:26 +1100 Subject: [PATCH] fix: fallback to en if invalid locale is set --- ts/localization/constants.ts | 3 +++ ts/node/locale.ts | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ts/localization/constants.ts b/ts/localization/constants.ts index 38ba2b6fd..b1c893c24 100644 --- a/ts/localization/constants.ts +++ b/ts/localization/constants.ts @@ -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); +} diff --git a/ts/node/locale.ts b/ts/node/locale.ts index 1a28c81dc..56068ecbb 100644 --- a/ts/node/locale.ts +++ b/ts/node/locale.ts @@ -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,