【问题标题】:Big Locale DBs in React-native (Expo)React-native (Expo) 中的大语言环境数据库
【发布时间】:2018-05-09 21:42:01
【问题描述】:

我发现这篇简单的文章 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中有locallazy-load(从设备文件系统)的例子吗?

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!
        }
    });

【问题讨论】:

  • 您找到解决方案了吗?我想知道同样的事情
  • @CharlieHarding 我的解决方案:从i18next 你只需要获取活动语言。从那里您可以编写自己的模块并在活动语言上使用switch 并执行require("activelanguage.json")之类的操作
  • @alfredpacino 你确定 Metro 捆绑器不会在 switch 语句中包含所有语言吗?我的印象是每个require('….json’) 都被逐字替换为相关对象。

标签: react-native i18next react-i18next


【解决方案1】:

使用 i18next,创建自己的后端实现相当简单...https://www.i18next.com/misc/creating-own-plugins#backend

我建议将您的翻译与应用程序捆绑在一起,而不是从任何外部资源加载。您也可以使用https://www.i18next.com/overview/api#resource-handling直接即时添加翻译

【讨论】:

  • 我用我想出的第一个解决方案编辑了这个问题
猜你喜欢
  • 2020-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-18
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
相关资源
最近更新 更多