From 8a980a720060b91abbdfa5a4c5c2527d1a40474b Mon Sep 17 00:00:00 2001 From: thewh1teagle <61390950+thewh1teagle@users.noreply.github.com> Date: Wed, 7 Aug 2024 19:10:43 +0300 Subject: [PATCH] fix language detection on init --- desktop/src/lib/i18n.ts | 12 +++++++++--- desktop/src/providers/Preference.tsx | 4 ++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/desktop/src/lib/i18n.ts b/desktop/src/lib/i18n.ts index a3f22890..1833728d 100644 --- a/desktop/src/lib/i18n.ts +++ b/desktop/src/lib/i18n.ts @@ -14,7 +14,7 @@ export const supportedLanguages: { [key: string]: string } = { 'fr-FR': 'french', 'pl-PL': 'polish', 'it-IT': 'italian', - 'hi-IN': 'hindi' + 'hi-IN': 'hindi', } export const supportedLanguageKeys = Object.keys(supportedLanguages) export const supportedLanguageValues = Object.values(supportedLanguages) @@ -29,8 +29,14 @@ const LanguageDetector: LanguageDetectorAsyncModule = { async: true, // If this is set to true, your detect function receives a callback function that you should call with your language, useful to retrieve your language stored in AsyncStorage for example detect: (callback) => { locale().then((detectedLocale) => { - if (detectedLocale) { - callback(detectedLocale) + const prefs_language = localStorage.getItem('prefs_display_language') + if (prefs_language) { + const locale = JSON.parse(prefs_language) + callback(locale) + } else { + if (detectedLocale) { + callback(detectedLocale) + } } }) }, diff --git a/desktop/src/providers/Preference.tsx b/desktop/src/providers/Preference.tsx index b1ff2e98..7455d277 100644 --- a/desktop/src/providers/Preference.tsx +++ b/desktop/src/providers/Preference.tsx @@ -111,6 +111,10 @@ export function PreferenceProvider({ children }: { children: ReactNode }) { } } useEffect(() => { + if (!isMounted.current) { + isMounted.current = true + return + } changeLanguage() }, [language])