i18next 即时更改 resGetPath

i18next changing resGetPath on the fly

我有一个使用 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,或者告诉我哪里做错了?

我想通了。

你只需要 运行 i18n.sync.resStore = {} 在你 运行 init 之前,然后它会强制重新加载。