reactjs - 为什么我们要在 react-i18 中使用 http 加载翻译
问题描述
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 请求吗?
解决方案
导入 JSON 文件意味着您的应用程序中已经拥有完整的文件(包含所有翻译)[BUILDTIME]。请求外部文件使您有机会使用新密钥更新该文件或修改现有密钥 [RUNTIME]
修改要在 ts/js 文件中导入的文件需要重建整个应用程序
推荐阅读
- macos - 如何允许 Github Action 的 Mac 屏幕录制 (Testcafe)
- mongodb - 我需要帮助从 mongodb 获取聚合数据 - 有些在数组中,有些不在数组中
- jmeter - jmeter restful api在报告中获取连接关闭、错误请求和警告
- wordpress - 在产品页面上输入姓名和电子邮件,并在结帐页面上保留输入数据
- libgdx - Libgdx 将对象绘制为黑色而不是彩色
- linux - 了解内核恐慌(带有内核的 RHEL (2.6.32-754.23.1.el6.x86_64))
- php - $FILES 数组不是多个
- pandas - Pandas 数据框中的分组条目,其中行具有相同的值
- r - 从另一帧中减去一帧的部分内容(并匹配标题)
- javascript - 是否可以从另一个 AJV 定义中引用枚举中的属性名称?