Jim*_*Lin 7 javascript internationalization i18next
我使用 I18Next 作为基于 Javascript 的翻译解决方案,以下是需要发生的事情:
i18n.inits ns.namespace。基本上,i18next 有没有办法在调用名称空间时自动加载名称空间?保证所调用的命名空间t("[SomeNamespace]Key.Key2");有效且确实存在。问题很简单,i18next 无法“自动加载”,并且我无法找到一种方法让 i18n在调用 i18n.init 后“手动”加载资源文件。
这是我当前的代码。
$.i18n.init(
{
lng: "en",
fallbackLng: "en",
useCookie: false,
resGetPath: "System/i18n/__ns__/__lng__.json",
ns: "Core"
},
function(t) {
System.I18N = t;
alert(System.I18N("LoginUI:Messages.Name"));
}
);
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,它只是向我显示LoginUI:Messages.Name而不是翻译System/i18n/LoginUI/en.json:
{
"Messages": {
"Name": "Logon Interface"
}
}
Run Code Online (Sandbox Code Playgroud)
(Core/en.json 在这种情况下是无关紧要的。我当前需要的是自动加载“LoginUI/en.json”,或者我可以强制手动加载。)
在深入研究源代码之后,我创建了一个可行的解决方案,但从长远来看肯定需要改进。
在 的定义中i18n.addjQueryFunct(),添加以下内容以访问 resStore(翻译存储变量):
$.i18n._getResStore = _getResStore;
$.i18n._writeResStore = _writeResStore;
function _getResStore() {
return resStore;
}
function _writeResStore(r) {
resStore = r;
}
Run Code Online (Sandbox Code Playgroud)
当你想加载额外的命名空间时,只需执行以下操作:
// define options, run $.i18n.init, etc...
// suppose ns = "namespace_foobar_new"
options.ns.namespaces.push(ns);
$.i18n.sync._fetchOne(lang, ns, $.extend({}, $.i18n.options, options),
function(err, data) {
store = {};
store[lang] = {}; store[lang][ns] = data;
newResStore = $.extend(true, {}, $.i18n._getResStore(), store);
$.i18n._writeResStore(newResStore);
});
Run Code Online (Sandbox Code Playgroud)
唷。
| 归档时间: |
|
| 查看次数: |
5197 次 |
| 最近记录: |