在React Native中进行国际化和本地化可以使用第三方库(如react-native-localize、i18n-js等)或自定义方案。以下是自定义方案的基本步骤:
const [locale, setLocale] = useState('en');
const t = (key) => {
switch (locale) {
case 'en':
return translations.en[key];
case 'zh':
return translations.zh[key];
// 其他语言的翻译
default:
return key;
}
};
<Text>{t('hello_world')}</Text>
<Picker
selectedValue={locale}
onValueChange={(value) => setLocale(value)}
>
<Picker.Item label="English" value="en" />
<Picker.Item label="中文" value="zh" />
// 其他语言选项
</Picker>