首页 > 解决方案 > 为什么我们要在 react-i18 中使用 http 加载翻译

问题描述

我正在阅读React-i18指令,他们分享了这个片段

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';

import Backend from 'i18next-http-backend';
import LanguageDetector from 'i18next-browser-languagedetector';
// not like to use this?
// have a look at the Quick start guide 
// for passing in lng and translations on init

i18n
  // load translation using http -> see /public/locales (i.e. https://github.com/i18next/react-i18next/tree/master/example/react/public/locales)
  // learn more: https://github.com/i18next/i18next-http-backend
  .use(Backend)
  // detect user language
  // learn more: https://github.com/i18next/i18next-browser-languageDetector
  .use(LanguageDetector)
  // pass the i18n instance to react-i18next.
  .use(initReactI18next)
  // init i18next
  // for all options read: https://www.i18next.com/overview/configuration-options
  .init({
    fallbackLng: 'en',
    debug: true,

    interpolation: {
      escapeValue: false, // not needed for react as it escapes by default
    }
  });


export default i18n;

从代码片段中,它说

  // load translation using http -> see /public/locales (i.e. https://github.com/i18next/react-i18next/tree/master/example/react/public/locales)
  // learn more: https://github.com/i18next/i18next-http-backend
  .use(Backend)

加载json优于http有什么好处?我们不能只导入 json 而不是发出 http 请求吗?

标签: reactjsrails-i18ni18next

解决方案


导入 JSON 文件意味着您的应用程序中已经拥有完整的文件(包含所有翻译)[BUILDTIME]。请求外部文件使您有机会使用新密钥更新该文件或修改现有密钥 [RUNTIME]

修改要在 ts/js 文件中导入的文件需要重建整个应用程序


推荐阅读