Browse Source

fix: detect browser language on first visit

- Add browser language detection before i18n initialization
- Set localStorage with detected browser language if not already set
- Support Hungarian (hu), German (de), and English (en)
- Extract language code from browser locale (e.g., 'hu' from 'hu-HU')
- Only applies on first visit, respects user's manual language selection afterward

This fixes the issue where Hungarian browsers were showing English by default
Fszontagh 4 months ago
parent
commit
292355647b
1 changed files with 13 additions and 2 deletions
  1. 13 2
      shopcall.ai-main/src/i18n/config.ts

+ 13 - 2
shopcall.ai-main/src/i18n/config.ts

@@ -6,6 +6,16 @@ import en from './locales/en.json';
 import hu from './locales/hu.json';
 import de from './locales/de.json';
 
+// Detect browser language on first visit
+const storedLanguage = localStorage.getItem('i18nextLng');
+if (!storedLanguage) {
+  const browserLang = navigator.language.split('-')[0]; // Get 'hu' from 'hu-HU'
+  const supportedLanguages = ['en', 'hu', 'de'];
+  if (supportedLanguages.includes(browserLang)) {
+    localStorage.setItem('i18nextLng', browserLang);
+  }
+}
+
 i18n
   .use(LanguageDetector) // Detects user language
   .use(initReactI18next) // Passes i18n down to react-i18next
@@ -23,9 +33,10 @@ i18n
     },
 
     detection: {
-      order: ['localStorage', 'navigator', 'htmlTag'],
+      order: ['querystring', 'localStorage', 'navigator', 'htmlTag'],
       caches: ['localStorage'],
-      lookupLocalStorage: 'i18nextLng'
+      lookupLocalStorage: 'i18nextLng',
+      lookupQuerystring: 'lng'
     }
   });