From 941be01185792987222a82ac1d5551d7d094ec1d Mon Sep 17 00:00:00 2001 From: Made Baruna Date: Sat, 12 Jun 2021 19:41:44 +0700 Subject: [PATCH] Fix language detection --- src/i18n.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/i18n.js b/src/i18n.js index 284c1c0d..4ee3e116 100644 --- a/src/i18n.js +++ b/src/i18n.js @@ -25,6 +25,7 @@ $locale.subscribe((value) => { } }); +const supportedLanguage = ['en', 'id', 'ru', 'ko', 'fr', 'zh', 'pt']; addMessages('en', en); addMessages('id', id); addMessages('ru', ru); @@ -34,11 +35,22 @@ addMessages('zh', zh); addMessages('pt', pt); export function startClient() { + let used = 'en'; const savedLocale = localStorage.getItem('locale'); + const detectedLocale = getLocaleFromNavigator().substring(0, 2); + if (savedLocale !== null) { + if (!supportedLanguage.includes(savedLocale)) { + localStorage.setItem('locale', 'en'); + } else { + used = savedLocale; + } + } else if (supportedLanguage.includes(detectedLocale)) { + used = detectedLocale; + } init({ ...INIT_OPTIONS, - initialLocale: savedLocale !== null ? savedLocale : getLocaleFromNavigator().substring(0, 2), + initialLocale: used, }); }