i18next / i18next-browser-languageDetector

language detector used in browser environment for i18next

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Blank string values will get updated in localStorage, sessionStorage or cookie

trymeouteh opened this issue Β· comments

πŸ› Bug Report

A clear and concise description of what the bug is.

To Reproduce

  • Install latest version of i18next and i18nextBrowserLangiageDetector from npm.
  • Use this code below to create a index.html
  • Change the language to german
  • Try changing the language to the blank option which should change the language to English as English is the fallback language.
<script src="node_modules/i18next/dist/umd/i18next.min.js"></script>
<script src="node_modules/i18next-browser-languagedetector/dist/umd/i18nextBrowserLanguageDetector.min.js"></script>



<h1 id="myOutput">Hello World (Unmodified)</h1>

<select>
    <option></option>
    <option value="en">English</option>
    <option value="de">German</option>
</select>

<button onclick="changeLanguage();">Change Language</button>



<script>
    const i18next = window.i18next;
    const i18nextBrowserLanguageDetector = window.i18nextBrowserLanguageDetector;



    const i18nextOptions = {
        fallbackLng: 'en',
        resources: {
            en: {
                translation: {
                    'myTitle': 'Hello World'
                }
            },
            de: {
                translation: {
                    'myTitle': 'Hallo Welt'
                }
            }
        },
        detection: {
            order: ['localStorage']
        }
    };



    i18next.use(i18nextBrowserLanguageDetector).init(i18nextOptions);





    changeLanguage();



    function changeLanguage() {
        //Sets the language used
        i18next.changeLanguage(document.querySelector('select').value);

        document.getElementById('myOutput').innerHTML = i18next.t('myTitle');
    }
</script>

Expected behavior

By selecting the blank option when the current language set is German, English will be set as the fallback language and display "Hello World", not "Hallo Welt".

Your Environment

  • runtime version: firefox developer edition 123
  • i18next version: i.e. 23.8.1
  • os: Linux

Calling changeLanguage without lng uses the language detector to choose the language to set.: https://www.i18next.com/overview/api#changelanguage

this will manually re-detecting language... and since the last language was german, it will keep german