我的 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")}
/>
最佳答案
如果您使用的是 i18next
您可以使用 i18next.changeLanguage
来更改您的语言
export const changeLaguage = (languageKey) => {
i18next.changeLanguage(lng, callback) // -> returns a Promise
}
import { changeLanguage } from 'services/translation';
<Button onPress={() => {changeLanguage(languageKey)}} />
但我建议更改为使用 react-native-localization 而不是
i18next
由 simply in implementation
和 native performance
如何使用
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.js
、 fr.js
、 kr.js
是包含 key
value
格式的翻译的文件。例如:
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)}} />
Notes: Do not declare key same at default methods like
setLanguage
查看更多关于
react-native-localization
here
关于function - 更改整个 native 应用程序的语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60633766/