【发布时间】:2020-05-13 22:13:10
【问题描述】:
我在我的 expo 项目中使用 i18n-js 来翻译我的应用程序。
这是我的配置方式:
import React from 'react';
import * as Localization from 'expo-localization';
import i18n from 'i18n-js';
export default function configureI18n(translations) {
i18n.fallbacks = true;
i18n.translations = translations;
i18n.locale = Localization.locale;
const [locale, setLocale] = React.useState(Localization.locale);
const localizationContext = React.useMemo(() => ({
t: (scope, options) => i18n.t(scope, { locale, ...options }),
locale,
setLocale,
}), [locale]);
return localizationContext;
}
我将此传递给我的AppContext 并尝试在我的视图中使用setLocale:
function HomeView(props) {
const { locale, setLocale } = useContext(AppContext);
return (
<View>
<Button
style={{ marginTop: 4 }}
icon="translate"
mode="contained"
title="toggle navigation"
onPress={() => setLocale(locale.includes('en') ? 'fr' : 'en')}
>
{locale.includes('en') ? 'FR' : 'EN'}
</Button>
</View>
);
}
调用了函数,但文字还是英文,我做错了什么?
【问题讨论】:
标签: javascript reactjs react-native expo i18n-js