【发布时间】: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