首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我们要在react-i18中使用http加载翻译?

为什么我们要在react-i18中使用http加载翻译?
EN

Stack Overflow用户
提问于 2020-07-30 19:53:22
回答 2查看 215关注 0票数 0

我查看了React-i18说明,他们共享了this snippet

代码语言:javascript
复制
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;

从代码片段中,它写道

代码语言:javascript
复制
  // 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请求吗?

EN

回答 2

Stack Overflow用户

发布于 2020-07-30 19:57:08

导入JSON文件意味着你的应用程序中已经有了完整的文件(包括所有的翻译) BUILDTIME。请求外部文件为您提供了使用新密钥更新该文件或修改现有密钥运行时的机会

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

票数 2
EN

Stack Overflow用户

发布于 2020-07-31 04:18:56

除了@user0101所说的,您可以延迟加载仅用于必要页面的翻译。这也可以通过分块来完成,但这比仅仅从后端获得翻译要困难得多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63172612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档