简体   繁体   中英

i18next Displayed key instead of value

I have translation.json file in /locales/en/

{
    "app": {
        "name": "Example App"
    }
}

In html , I have:

<a href="/" data-i18n="app.name">

In js :

$(document).ready(function() {

    var language_complete = navigator.language.split("-");
    var language = (language_complete[0]);

    console.log('language', language);

    $.i18n.init({
        lng: language,
        fallbackLng: false,
        debug: false
    }, function() {
        $('a').i18n();
    });
});

It displayed app.name instead of Example App . What i missed in my code? Thanks

您必须将useLocalStorageuseDataAttrOptions设置为 true

$.i18n.init({useLocalStorage: true , useDataAttrOptions:true, ....});

as of i18next V2, the backend is no longer provided out of the box ( see the migration guide ), so you're required to define a backend configuration in the init block:

backend: {
    loadPath: '/locales/{{lng}}/{{ns}}.json'
},

if you don't do that, your resources will not be loaded, and translation values will fallback to their respective keys ( see the source code ).

In js it's because $(document).ready doesn't wait for external contents to be loaded. You need to use $(window).on('load',function(){...}) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM