i18next / i18next-browser-languageDetector

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

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

Closed trymeouteh closed 10 months ago

trymeouteh commented 10 months ago

🐛 Bug Report

A clear and concise description of what the bug is.

To Reproduce

<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

adrai commented 10 months ago

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