繁体   English   中英

在客户端javascript中将i18next与XHR后端一起使用

[英]Use i18next with XHR backend in client-side javascript

i18next-xhr-backend上的文档告诉我使用import加载其模块。 但是,当我使用import -statement时,什么也没发生,Firefox在开发人员控制台中给了我SyntaxError

SyntaxError: import declarations may only appear at top level of a module

那么,如何在XHR后端上使用i18next库? 如果注释了Warning: i18next::backendConnector: No backend was added via i18next.use. Will not load resources. .use(XHR)和相应的导入,则以下代码示例Warning: i18next::backendConnector: No backend was added via i18next.use. Will not load resources.Warning: i18next::backendConnector: No backend was added via i18next.use. Will not load resources. )。 但是,如果没有,它将失败: ReferenceError: XHR is not defined

//import Fetch from 'i18next-fetch-backend';

let t = null;

    i18next
      .use(XHR)
      .init({
        debug: true,
        fallbackLng: ['en'],
        preload: ['en'],
        ns: 'translation',
        defaultNS: 'translation',
        keySeparator: false, // Allow usage of dots in keys
        nsSeparator: false,
        backend: {
          loadPath: '/locales/{{lng}}/{{ns}}.json',
        },
      }, (err, _t) => {
        if (err) {
          reject(err);
          return;
        }

        t = _t;
        //resolve();
      });

jqueryI18next.init(i18next, $, {
    tName: 't', // --> appends $.t = i18next.t
    i18nName: 'i18n', // --> appends $.i18n = i18next
    handleName: 'localize', // --> appends $(selector).localize(opts);
    selectorAttr: 'data-i18n', // selector for translating elements
    targetAttr: 'i18n-target', // data-() attribute to grab target element to translate (if different than itself)
    optionsAttr: 'i18n-options', // data-() attribute that contains options, will load/set if useOptionsAttr = true
    useOptionsAttr: false, // see optionsAttr
    parseDefaultValueFromContent: true // parses default values from content ele.val or ele.text
}); 
$(".nav").localize();

我需要使用i18nextXHRBackend而不是XHR ,因为这是类被加载的名称,就好像没有使用任何加载器一样。 README.md所说:

如果您不使用模块加载器,它将被添加到window.i18nextXHRBackend

我以前没有看到过,我也不知道这种情况会自动发生,但是如果不使用模块加载器,您似乎必须自己找出来。 吸取了教训,希望这将有助于其他一些新手陷入如何使用javascript模块的困境。 因此,我完整的localisation.js如下所示:

$(document).ready(function() {
    i18next
        .use(i18nextXHRBackend)
        .use(i18nextBrowserLanguageDetector)
        .init({
            debug: true,
            backend: {
                loadPath: 'locales/{{lng}}/{{ns}}.json',
                addPath: 'locales/add/{{lng}}/{{ns}}'
            }
        }, function(err, t) {
            jqueryI18next.init(i18next, $);
            $('.translatable').localize();
            $('.language-button').click(function() {
                i18next.changeLanguage(this.firstElementChild.alt).then(function(t) {
                $('.translatable').localize(); 
                $('#signupPassword').pwstrength("forceUpdate");
                $('#signupPasswordConfirm').pwstrength("forceUpdate");
            });
        });
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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