【问题标题】:i18next - Loading translations from a JSON filei18next - 从 JSON 文件加载翻译
【发布时间】:2021-09-24 07:24:19
【问题描述】:

我正在尝试使用 Node.js 上的 i18next 库从 JSON 文件加载翻译。 JSON 文件的路径指向正确的位置。

我收到以下错误: i18next::translator: missingKey en translation test test

import i18next from 'i18next';
import Backend from 'i18next-fs-backend';

const instance = i18next.use(Backend).createInstance({
    lng: config.language,
    debug: true,
    fallbackLng: 'en',
    initImmediate: false,
    backend: {
        loadPath: join(__dirname, `${config.language}.json`),
    },
}, (error, t) => {
    console.log(t('foo'));
});

JSON 文件:

{
      "foo": "bar"
}

使用resources 属性直接在createInstance 中指定翻译效果很好。

我尝试了所有我能想到的方法,但没有成功。

【问题讨论】:

    标签: node.js json typescript i18next


    【解决方案1】:

    找到了解决办法!

    import i18next from 'i18next';
    import Backend from 'i18next-fs-backend';
    
    const instance = i18next.use(Backend).createInstance();
    instance.init({
        lng: config.language,
        debug: true,
        fallbackLng: 'en',
        initImmediate: false,
        backend: {
            loadPath: join(__dirname, `${config.language}.json`),
        },
    }, (error, t) => {
        console.log(t('foo'));
    });
    

    【讨论】:

      猜你喜欢
      • 2019-03-03
      • 2013-07-21
      • 2022-10-13
      • 1970-01-01
      • 2014-08-01
      • 2019-10-04
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多