简体   繁体   English

在 object 数组中使用 I18n 反应本机问题

[英]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:但是当我从另一个文件导入这个数组时,它没有找到密钥,并且每个项目都有这个错误:

在此处输入图像描述
I don't understand why... an idea?我不明白为什么……一个想法?

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在我的实现中,我像这样存储密钥

  1. I have a js/ts file, called Strings where I store the keys我有一个名为 Strings 的 js/ts 文件,用于存储密钥

 import { translate } from "./I18n" const Strings = { SOME_WORD: translate("keyName.firstWord"), ANOTHER_WORD: translate("keyName.firstWord")... }

  1. In my l18n file I defined a translate function (in this way you won't have to import the i18n-js wherever you use it在我的 l18n 文件中,我定义了一个translate 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

  1. In a separate file, called en.ts I have the actual translations在一个名为 en.ts 的单独文件中,我有实际的翻译

 export default { keyName: { firstWord: "The actual translation", ... } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM