From fe5b4bb0628be9e0bef2441c893ea25834b504c9 Mon Sep 17 00:00:00 2001 From: bonukai Date: Sat, 12 Mar 2022 18:51:02 +0000 Subject: [PATCH] activate detected language only if translations are available (#228) --- client/src/i18n/i18n.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/client/src/i18n/i18n.ts b/client/src/i18n/i18n.ts index 2e5041af..7c4ae70b 100644 --- a/client/src/i18n/i18n.ts +++ b/client/src/i18n/i18n.ts @@ -8,11 +8,24 @@ import { messages as en } from 'src/i18n/locales/en/translation'; import { messages as es } from 'src/i18n/locales/es/translation'; export const setupI18n = () => { - const locale = detect(fromNavigator(), 'en').split('-')?.at(0) || 'en'; + const allMessages = { + da: da, + de: de, + en: en, + es: es, + }; + + const supportedLanguages = Object.keys(allMessages); + + const detectedLocale = detect(fromNavigator(), 'en').split('-')?.at(0); + + const locale = supportedLanguages.includes(detectedLocale) + ? detectedLocale + : 'en'; i18n.loadLocaleData({ [locale]: { plurals: plurals[locale] }, }); - i18n.load({ da: da, de: de, en: en, es: es }); + i18n.load(allMessages); i18n.activate(locale); };