javascript - 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>
解决方案
resGetPath
来自旧的 1.xx 天......现在看看插件:https ://www.i18next.com/overview/plugins-and-utils#backends
在你的情况下https://github.com/i18next/i18next-xhr-backend ---> i18next.use(XHR)
您搜索的选项是backend.loadPath
推荐阅读
- javascript - 无法使用 datefns 解析日期
- reactjs - 如何在不重新渲染 Next.js 的情况下更改路线?
- python - 从python数据框中的文本列中获取唯一时间戳的计数
- javascript - 按每个列表有多少值对列表列表进行排序
- ios - UIButton 有阴影旋转没有阴影旋转
- arrays - 在 Fortran 中创建包含其他声明变量的数组
- python - 递归函数有问题(Python)
- git - 从本地签出的单个文件中查找提交哈希
- c# - 如何根据另一个表的另一行中的条件选择表中的一行?
- node.js - 带有 NATS 的 Nodejs Express API?