首页 > 解决方案 > i18next 'resGetPath' 未在插件初始化中实现

问题描述

我想在这个项目中存储 translation.json 文件,并让 in18next 访问它们。不幸的是,我似乎无法resGetPath正确实现该属性。当我用resources对象初始化插件时,它工作正常。我猜 json 文件的路径或结构有问题,但我似乎在 SO 或 i18next 的任何论坛上都找不到类似的问题(大多数人都有更大的项目,只是从服务器)。我想根据用户选择的任何语言环境简单地引用本地翻译文件。谢谢你的时间=)。

这是 translation.js 的代码,可以很好地加载到 index.html 中:

$(document).ready(function() {
  i18next.init({
    lng: 'en', 
    // resources: { 
    //   en: {
    //     translation: {
    //       nav: {
    //         newInvoice: 'Hakuna Matata'
    //       }
    //     }
    //   }
    // },
    resGetPath:'../locales/{{lng}}/translation.json',
  }, function (err, t) {
      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 diffrent then itself)
        optionsAttr: 'i18n-options', // data-() attribute that contains options, will load/set if useOptionsAttr = true
        useOptionsAttr: false, // see optionsAttr
        parseDefaultValueFromContent: true
      });

      $('#tempTarget').localize();
    })
});

这是 locales/en/translations.js:

{
  "nav": {
    "newInvoice": "somethaaaang"
  }
}

这是我的目录:

index.html
locales
   en
      translation.json

这是 html 中正确定位到resources对象而不是reGetPath属性的行:

<a href="/new-invoice/" data-i18n='nav.newInvoice' id='tempTarget' class="panel-close">New Invoice</a>

标签: javascriptjsonpathinternationalizationi18next

解决方案


resGetPath来自旧的 1.xx 天......现在看看插件:https ://www.i18next.com/overview/plugins-and-utils#backends

在你的情况下https://github.com/i18next/i18next-xhr-backend ---> i18next.use(XHR)

您搜索的选项是backend.loadPath


推荐阅读