我发现这篇简单的文章https://medium.com/@jamuhl/translate-your-expo-io-react-native-mobile-application-aa220b2362d2在 RN 中实现本地化(在 Expo 中)。
我的用例有点不同:我的应用程序有数百个要翻译的术语列表(每种语言约 1MB 的 js 对象,乘以 6-7 种语言)。
该应用程序完全脱机(没有机会从服务器加载语言环境文件),所以我正在寻找延迟加载我想要的 json/js 语言环境对象的最佳方法。
我来这里可能是为了听取 RN/i18next 专家的建议。
编辑: 我几乎复制了这个https://github.com/i18next/react-i18next/blob/master/example/react-native-expo/js/i18n.js的 i18n 配置
真正的数据库在现场data:DB
。Obiouvsly 这不是加载繁重文件的最佳方式,我总是加载所有语言的所有数据库..我可以保持相同的简单结构,但延迟加载我需要的语言吗?否则,在 i18next 和 react-native 中有本地延迟加载(来自设备文件系统)的示例吗?
import i18n from 'i18next';
import {reactI18nextModule} from 'react-i18next';
i18n
.use(languageDetector)
.use(reactI18nextModule)
.init({
fallbackLng: 'en',
resources: {
en: {
home: {
title: 'Welcome',
introduction: 'This text comes from i18next and is provided in english.',
},
data: { DB:require("./locales/it.json") },
},
de: {
home: {
title: 'Willkommen',
introduction: 'Dieser Text ist von i18next und ist in deutsch.',
},
data: { DB:require("./locales/de.json") },
}
},
// have a common namespace used around the full app
ns: ['common'],
defaultNS: 'home',
debug: true,
interpolation: {
escapeValue: false, // not needed for react as it does escape per default to prevent xss!
}
});