我有一个 Node 应用程序,它使用 i18next 模块来处理 i18n,但我遇到了一个在服务器端似乎无法解决的问题。
该应用程序是在 node
中构建的,并使用 express
框架。
在作为 ap 入口点的主 app.js
文件中,我需要 i18n
模块并运行 init
方法。
这是为了将其设置为在客户端使用。
稍后在另一个文件中,我尝试重置 resGetPath
属性,以便在与正常文件不同的位置加载翻译文件(某些文件位于默认的 locales/__lng__/__ns__.json
路径,其他位于自己单独的文件夹中)。
我似乎无法在 i18n 对象上找到执行此操作的特定方法,因此我一直尝试使用回调再次运行 init
方法。
但是,即使我这样做,resGetPath
属性也不会更改,并且不会从新文件中获取翻译字符串。
如果我在 app.js
中的第一个 init
调用中设置 resGetPath
属性,尽管它按预期工作(尽管它会丢失locales/__lng__/__ns__.json
中的正常翻译)
基本上这就是我想要做的:
app.js
i18n.init({
detectLngQS: 'lang',
debug: false,
useCookie: false,
detectLngFromHeaders: true,
fallbackLng: ['en-GB', 'dev']
});
after-app.js
i18n.init({
resGetPath: path.join(__dirname, '../new-path.json')
}, function(t) {
// Here `t` does not get the strings in the new resGetPath
});
有人可以告诉我如何即时更改 resGetPath
,或者告诉我我做错了什么吗?
最佳答案
我明白了。
您只需在运行 init
之前运行 i18n.sync.resStore = {}
,然后它就会强制重新加载。
关于javascript - i18next 动态更改 resGetPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30455146/