【问题标题】:Loading translation files on demand按需加载翻译文件
【发布时间】:2020-04-05 04:27:09
【问题描述】:

使用 i18next 进行翻译的标准方法通常涉及在加载 Web 应用程序后立即加载所有翻译文件。

i18n
    .use(XHR)
    .use(LanguageDetector)
    .init({
        fallbackLng: 'en',
        debug: false,

        keySeparator: false,

        interpolation: {
            escapeValue: false,
            formatSeparator: ','
        },

        resources: {
            en: {
                translations: en
            },
            ru: {
                translations: ru
            },
            es: {
                translations: es
            }
        },

        ns: ['translations'],
        defaultNS: 'translations',

        react: {
            wait: true
        }
});

我发现这种方法效率很低,并希望根据需要(即客户端切换语言时)从服务器请求翻译文件。不幸的是,我在官方文档中没有找到任何参考,但肯定应该有办法做到这一点。

我想要实现的架构:

1) Web 应用与默认翻译文件一起加载(例如english.json

2) 如果用户切换语言——比如说西班牙语——spanish.json 正在从服务器加载并调整整个翻译。

【问题讨论】:

    标签: reactjs i18next


    【解决方案1】:

    API documentation 获取.init() 方法的配置选项:

    partialBundledLanguages — 允许在初始化时设置一些资源,而其他资源可以使用后端连接器加载

    因此,将选项设置为 true 将实现您的目标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      • 2019-03-03
      • 2021-09-24
      • 1970-01-01
      • 2021-04-11
      • 2020-11-24
      • 1970-01-01
      相关资源
      最近更新 更多