[英]React Native issue with I18n in an array of object
I use I18n in my app with a config:我在我的应用程序中使用 I18n 配置:
I18n.translations = {
en: en,
fr: fr,
};
if (!__DEV__) {
I18n.fallbacks = true;
}
I18n.defaultLocale = 'en';
I18n.locale = localeLanguageTag as ELanguagesKeys;
Then I export it and use it as usual...然后我导出它并像往常一样使用它......
In a component, I have an object array with some translation key:在一个组件中,我有一个带有一些翻译键的 object 数组:
const onBordingSteps: ISteps = [
{
label: I18n.t('auth.heading.steps.onboarding.step1.title'),
isCompleted: true,
icon: GGValidation,
iconHeight: 49.36,
iconWidth: 28.48,
},
{
label: I18n.t('auth.heading.steps.onboarding.step2.title'),
isCompleted: false,
.....
];
I use this array directly after its creation in my component我在组件中创建此数组后直接使用它
return (
<PreOnboarding
steps={onBordingSteps}
...
But when I import this array from another file, it didn't find the key and I have this error on each items:但是当我从另一个文件导入这个数组时,它没有找到密钥,并且每个项目都有这个错误:
Maybe this will help you, because I think it might be an issue with the export method of the keys since they cannot be found也许这会对您有所帮助,因为我认为密钥的导出方法可能存在问题,因为找不到它们
In my implementation I store the keys like this在我的实现中,我像这样存储密钥
import { translate } from "./I18n" const Strings = { SOME_WORD: translate("keyName.firstWord"), ANOTHER_WORD: translate("keyName.firstWord")... }
translate
function (in this way you won't have to import the i18n-js
wherever you use ittranslate
function (这样你就不必在任何地方导入i18n-js
import * as RNLocalize from "react-native-localize" import i18n from "i18n-js" import en from "./en" const translations = { en } const { languageTag } = RNLocalize.findBestAvailableLanguage(Object.keys(translations)) || { languageTag: "en", } i18n.locale = languageTag i18n.fallbacks = true i18n.translations = translations export function translate(key: string) { return key? i18n.t(key): "" } export default i18n
export default { keyName: { firstWord: "The actual translation", ... } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.