i18next / i18next-browser-languageDetector

language detector used in browser environment for i18next
MIT License
873 stars 90 forks source link

LanguageDetector does not respect browser's preferred language list #253

Closed NashBaughn closed 2 years ago

NashBaughn commented 2 years ago

🐛 Bug Report

LanguageDetector does not respect browser's preferred language list. I know this seems like a duplicate, but issue persists (or I am doing something wrong!).

To Reproduce

The browser's ordered language list is de-AT, es-AR. Screen Shot 2022-02-09 at 8 28 19 PM

In the app, i18n.options.supportedLng = ["en", "es"] and i18n.options.fallbackLng = "en". See below:

i18next
    .use(ChainedBackend)
    .use(LanguageDetector)
    .use(initReactI18next)
    .init({
       fallbackLng: "en",
       supportedLngs: ["es", "en"],        
...

Expected behavior

The app should render with i18n.language == "es-AR" and i18n.resolvedLanguage == "es", instead, the result is i18n.language == "de-AT and i18n.resolvedLanguage == "en". However, if "Spanish (Argentina)" is at the top of the browser's preferred language list, then the language is detected correctly (with i18n.language == "es-AR" and i18n.resolvedLanguage == "es").

After some choice log statements in LanguageDetector.detect() I found it puzzling that services.languageUtils.getBestMatchFromCodes(detected) returns "es-AR", but this value does not seemed to be used downstream.

Environment

browser: Version 96.0.4664.55 i18next: Version 21.6.11 os: macOS BigSur

NashBaughn commented 2 years ago

Closing, this was due to user (me) error

rodriguesgm commented 1 year ago

hey @NashBaughn what was your error? Can you explain what you did wrong? thanks