我查看了React-i18说明,他们共享了this snippet
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请求吗?
发布于 2020-07-30 19:57:08
导入JSON文件意味着你的应用程序中已经有了完整的文件(包括所有的翻译) BUILDTIME。请求外部文件为您提供了使用新密钥更新该文件或修改现有密钥运行时的机会
修改想要在ts/js文件中导入的文件需要重新构建整个应用程序
发布于 2020-07-31 04:18:56
除了@user0101所说的,您可以延迟加载仅用于必要页面的翻译。这也可以通过分块来完成,但这比仅仅从后端获得翻译要困难得多。
https://stackoverflow.com/questions/63172612
复制相似问题