【问题标题】:I18next load translation with locally with bundled translation filesI18next 使用捆绑的翻译文件在本地加载翻译
【发布时间】:2020-11-24 11:36:54
【问题描述】:

有没有办法让我从与我的 js 文件捆绑的翻译 json 文件中加载翻译?

我只需要介绍几种语言,由于限制,无法设置后端端点。

我知道 addResourceBundle 函数,但没有成功。

我有翻译文件english.jsonspanish.json

i18next.init();
i18next.addResourceBundle('en', 'translation', english.json);
i18next.addResourceBundle('es', 'translation', spanish.json);

好像没用。

【问题讨论】:

    标签: i18next react-i18next i18next-http-backend


    【解决方案1】:

    为了将您的翻译文件与 js 代码捆绑在一起,您需要将它们添加到应用程序的依赖关系树中。

    我猜你使用的是 webpack,你需要为 json 导入定义一个加载器(json-loader)。

    i18next.init({
      resources: {
        'en-US': {translation: require('./en-us/translation.json')},
        // ---------^ namespace
        'nl-NL': {translation: require('./nl-nl/translation.json')},
      },
      fallbackLng: 'en-US',
      ns: ['translation'],
      defaultNS: 'translation',
    });
    
    

    【讨论】:

    • 您好,它可以加载资源,但是现在后端 api 不能工作? i18next.init( resources:{}, backend:{ loadpath: someEndPoint/{{lng}}/{{ns}}.json }} When selecting a language not available with the bundle, it should've called to the endpoint to retrieve the translations, but now no request is being sent and the UI is suspended.
    • 糟糕,抱歉。完全错过了partialBundledLanguages: "true", 它正在工作!很高兴知道,因为它打开了扩展的可能性
    猜你喜欢
    • 2013-07-21
    • 2019-03-03
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    相关资源
    最近更新 更多