Skip to content

Commit

Permalink
Fix: handle language-region extraction and add navigator.languages to…
Browse files Browse the repository at this point in the history
… locale detection priority
  • Loading branch information
gkatrakazas committed Jan 27, 2025
1 parent 024667e commit bbb5a0a
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import { initReactI18next } from 'react-i18next';
import enTranslation from './locales/en.json';
import elTranslation from './locales/el.json';

// Helper function to get only the language part and check if it exists
const getLanguage = (locale) => {
const language = locale.includes('-') ? locale.split('-')[0] : locale;
return language;
};

// Get the preferred language
const preferredLanguage =
localStorage.getItem('locale') || // Check localStorage first
getLanguage(navigator.language) || // Check navigator.language next
getLanguage(navigator.languages ? navigator.languages[0] : null) || // Check navigator.languages[0] last
null;

i18n
.use(initReactI18next)
.init({
Expand All @@ -13,7 +26,7 @@ i18n
el: { translation: elTranslation },
},
fallbackLng: 'en',
lng: localStorage.getItem('locale') || navigator.language,
lng: preferredLanguage,
interpolation: {
escapeValue: false,
},
Expand Down

0 comments on commit bbb5a0a

Please sign in to comment.