i18next / i18next-browser-languageDetector

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

LanguageDetector does not automatically detect language from browser #260

Closed pmichelfeit closed 2 years ago

pmichelfeit commented 2 years ago

šŸ› Bug Report

LanguageDetector does not automatically detect language from browser ( Mozilla Firefox 101, Google Chrome 102 )

To Reproduce

Use Mozilla Firefox 101 or Google Chrome 102 and change language in browser

Expected behavior

That it automatically switches languages according to the browsers settings

Your Environment

adrai commented 2 years ago

Seems to work... Pleqse provide a reproducible example.

pmichelfeit commented 2 years ago

This is the content of my i18n.js file:

import i18n from "i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import XHR from "i18next-xhr-backend";
import { initReactI18next } from "react-i18next";

import translationEN from "./i18n/locales/en/translations.json";
import translationDE from "./i18n/locales/de/translations.json";

i18n
  .use(XHR)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    debug: false,

    // lng: "de", // automatically detected by browser
    fallbackLng: "en", // use en if detected lng is not available

    keySeparator: false, // we do not use keys in form messages.welcome

    interpolation: {
      escapeValue: false // react already safes from xss
    },

    resources: {
      en: translationEN,
      de: translationDE
    },
    // have a common namespace used around the full app
    ns: ["translations"],
    defaultNS: "translations"
  });

export function changeLanguage(lng) {
  return i18next.changeLanguage(lng);
}

export default i18n;
adrai commented 2 years ago

You just pasted some code snippets... this is not a reproducible example.

Try something like this: https://codesandbox.io/embed/react-i18next-http-example-8guj0d?codemirror=1

pmichelfeit commented 2 years ago

In the above example, if you switch the language in the browser, the language is not changed too.

adrai commented 2 years ago

You may describe a bit with more detail on what you exactly mean. Seems to work here. Provide a video or screenshots

pmichelfeit commented 2 years ago

It should switch language automatically if you change the language in the browser

jamuhl commented 2 years ago

do you mean it should switch when you change it. without reloading?

also check if lng is already persisted in localstorage

adrai commented 2 years ago

check your local storage:

image

also check what navigator.languages is returning...

image
jamuhl commented 2 years ago

it does only detect on init...changing the language in browser does not change after init...if stored in localstorage it will also not change on reload

do you really expect the language to be changed and your content rerendered if you change browser language - without reloading the page?

pmichelfeit commented 2 years ago

Yeah, I expect that

pmichelfeit commented 2 years ago

This is how the local storage looks like:

Fehler

And this is what navigator.languages is returning:

Fehler
adrai commented 2 years ago

Yeah, I expect that

sorry, but this is not possible with this module...

scenario 1: the caches option is set to empty array => caches: []:

  1. make sure the cache (localStorage) is empty (i18nextLng is not present)
  2. the language is detected and used as language within i18next.
  3. the user changes the browser language (navigator.languages changes)
  4. on the next refresh of the page this plugin detects the new language

scenario 2: the caches option is NOT set to empty array:

  1. make sure the cache (localStorage) is empty (i18nextLng is not present)
  2. the language is detected and used as language within i18next AND cached (based on the config i.e. in localStorage).
  3. the user changes the browser language (navigator.languages changes)
  4. on the next refresh of the page this plugin detects the new language in the cache (the last used language)
jamuhl commented 2 years ago

Browser language is a rather static setting. Iā€˜m not aware having changed that once in my livetime (beside during development).

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.