【问题标题】:Change language of whole react-native app更改整个 react-native 应用程序的语言
【发布时间】:2020-06-23 07:16:54
【问题描述】:

我的 react-native 应用有 2 种语言:英语和法语。我已经为英语和法语完成了一个 i18n.js 文件和 2 个“translation.js”文件。它工作得很好(我试过在我的手机上设置法语,在模拟器上设置英语)。

现在,我想在应用程序的设置中创建一个按钮来为用户更改整个应用程序的语言,但我不知道什么函数调用以及如何创建它。

你能帮帮我吗? 非常感谢您的时间和帮助。

我的 i18n.js 文件;

import i18n from "i18next";
import detector from "i18next-browser-languagedetector";
import { reactI18nextModule } from "react-i18next";
import { NativeModules, Platform } from "react-native";

import en from "../public/locales/en/translation.js";
import fr from "../public/locales/fr/translation.js";

// the translations
const resources = {
  en: {
    translation: en
  },
  fr: {
    translation: fr
  }
};
const locale =
  Platform.OS === "ios"
    ? NativeModules.SettingsManager.settings.AppleLocale
    : NativeModules.I18nManager.localeIdentifier;

i18n.locale = locale;

i18n
  .use(detector)
  .use(reactI18nextModule) // passes i18n down to react-i18next
  .init({
    resources,
    lng: locale.substring(0, 2),
    fallbackLng: "en", // use en if detected lng is not available

    keySeparator: ".", // we do not use keys in form messages.welcome

    interpolation: {
      escapeValue: false // react already safes from xss
    }
  });

export default i18n;

我想从我的 Settings.js 文件中更改语言:

<SettingsList.Item
              // icon={<Image style={styles.image} source={require('../../assets/images/icon_vol-mute.png')}/>}
              title={i18n.t("settings.action.languages")}
              hasNavArrow={false}
              switchState={this.state.activeSwitch === 3}
              switchOnValueChange={this.switchThree}
              hasSwitch={true}
              onPress={() => Alert.alert("French / English")}
            />

【问题讨论】:

    标签: function react-native translate i18next


    【解决方案1】:

    如果您使用的是i18next

    您可以使用i18next.changeLanguage 更改您的语言

    export const changeLaguage = (languageKey) => {
    i18next.changeLanguage(lng, callback) // -> returns a Promise
    }
    
    import { changeLanguage } from 'services/translation';
    
    <Button onPress={() => {changeLanguage(languageKey)}} />
    

    但我建议通过simply in implementationnative performance 改用react-native-localization 而不是i18next

    react-native-localization如何使用?

    安装

    yarn add react-native-localization --save
    
    #react-native >= 0.60
    cd ios && pod install && cd ..
    
    #react-native < 0.60
    react-native link react-native-localization
    

    声明您的翻译

    import LocalizedStrings from 'react-native-localization';
    import english from './en.js'
    import french from './fr.js'
    import korean from './kr.js'
    
    const strings = new LocalizedStrings({
     en: english,
     fr: french,
     kr: korean,
    });
    

    en.jsfr.jskr.js 是包含keyvalue 格式的翻译的文件。

    例如:fr.js的内容

    export default {
    ok: 'D'accord',
    no: 'non'
    }
    

    您也可以使用json文件类型进行声明。

    用法

    import strings from 'services/translation';
    
    <Text style={styles.title}>
      {strings.ok}
    </Text>
    

    更改语言

    编写一个函数来更改您的应用语言并在您想要的任何地方使用它。

    const strings = new LocalizedStrings({
     en: english,
     fr: french,
     kr: korean,
    });
    
    
    export const changeLaguage = (languageKey) => {
    strings.setLanguage(languageKey)
    }
    
    import { changeLaguage } from 'services/translation'
    
    
    <Button onPress={() => {changeLanguage(languageKey)}} />
    

    注意:不要在setLanguage等默认方法中声明key相同

    查看更多关于react-native-localizationhere

    【讨论】:

    • 多么丰富的答案!感谢您花时间如此精确!当我尝试使用 i18next 的解决方案时,我可以问什么是 'LanguageKey',它显示错误:'找不到变量'LanguageKey'(我应该用'fr'还是'en'替换它)?我想在第一步中让它以这种方式工作,但我想我会考虑使用您提供的其他解决方案。再次感谢您。
    • 当然是language-code。您可以使用enfr 或任何有效的语言代码
    • 完美,有效!!非常感谢您的帮助和时间!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 2017-05-09
    • 2015-05-09
    相关资源
    最近更新 更多