【问题标题】:missing translation in react native expo i18nreact native expo i18n 中缺少翻译
【发布时间】:2021-04-15 07:19:52
【问题描述】:

所以在我的应用程序中,我的文本需要使用西班牙语和英语,具体取决于手机本身的配置方式。无论如何,经过大量研究,我所做的每一次尝试都得出了相同的结论,即[缺少'en.Messages'翻译]。谁能告诉我我错过了什么或做错了什么?

APP.JS

  import { loadLocale } from "./i18n";

  const init = async () => {
    await loadLocale();
  };

  useEffect(() => {
    init();
  }, []);

i18n.js

import * as Localization from "expo-localization";
import i18n from "i18n-js";

i18n.defaultLocale = "en";
i18n.locale = "en";
i18n.fallbacks = true;

export const loadLocale = async () => {
  for (const locale of Localization.locales) {
    if (i18n.translations[locale.languageCode] !== null) {
      i18n.locale = locale.languageCode;
      switch (locale.languageCode) {
        case "en":
          import("./translations/en.json").then((en) => {
            i18n.translations = { en };
          });
          break;
        default:
        case "es":
          import("./translations/es.json").then((es) => {
            i18n.translations = { es };
          });
          break;
      }
      break;
    }
  }
};

export default i18n;

翻译文件夹

inside the translations folder are 2 files call en.json / es.json
{
    "Messages": "Messages!",
}

Messages.js

import i18n from "../i18n";

<Text>{i18n.t("Messages")}</Text>

【问题讨论】:

    标签: reactjs react-native localization translation i18next


    【解决方案1】:

    在加载语言环境之前尝试设置 defaultLocale。

    import { loadLocale } from "./i18n";
    
         i18n.defaultLocale = "en";
         i18n.locale = "en";
         i18n.fallbacks = true;
    
      const init = async () => {
        await loadLocale();
      };
    
      useEffect(() => {
        init();
      }, []);
    

    【讨论】:

    • 在 app.js 中?我试过了,但它没有改变任何东西
    • 能否请您在沙箱上创建一个示例,这样会更容易调试。
    • 所以现在如果我将 i18n.js 文件更改为 i18n.defaultLocale = "es";而不是“en”它的工作。所以问题似乎与英文翻译有关
    • 您的本地化。 locales 返回一个空数组,这是我能够复制和修复它的链接。 codesandbox.io/s/epic-zhukovsky-fwev7?file=/src/i18n.ts:334-354
    猜你喜欢
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 2018-11-25
    • 2018-11-03
    • 1970-01-01
    相关资源
    最近更新 更多