我想根据用户选择的语言更改RN应用程序的语言。我该如何使用react-native做到这一点? react-native-localize我应该使用的库吗?
答案 0 :(得分:0)
是的,您可以使用react-native-localize来获取用户的语言环境。
使用findBestAvailableLanguage
方法可以从可用子集中获得最佳语言:
console.log(RNLocalize.findBestAvailableLanguage(["en-US", "en", "fr"]));
答案 1 :(得分:0)
您可以使用i18n-js
和react-native-localize
安装模块
yarn add react-native-localize
yarn add i18n-js
OR
npm install --save react-native-localize
npm install --save i18n-js
和
react-native链接react-native-localize
Example.js
import React from 'react';
import { Text } from 'react-native';
import * as RNLocalize from "react-native-localize";
import i18n from 'i18n-js';
const en = {
foo: 'Foo',
bar: 'Bar {{someValue}}',
};
const fr = {
foo: 'como telle fous',
bar: 'chatouiller {{someValue}}',
};
i18n.fallbacks = true;
i18n.translations = { fr, en };
i18n.locale = RNLocalize.locale;
export default class Example extends React.Component {
render() {
return (
<Text style={{ flex: 1, paddingTop: 50, alignSelf: 'center' }}>
{i18n.t('foo')} {i18n.t('bar', { someValue: Date.now() })}
</Text>
);
}
}