admin管理员组文章数量:1277268
I am using i18n-js within my expo project to translate my app.
This is how I configure it:
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;
}
I pass this to my AppContext
and try to use setLocale
within my view:
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>
);
}
The function is called, but the text is still in english, what am I doing wrong ?
I am using i18n-js within my expo project to translate my app.
This is how I configure it:
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;
}
I pass this to my AppContext
and try to use setLocale
within my view:
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>
);
}
The function is called, but the text is still in english, what am I doing wrong ?
Share Improve this question asked Jan 28, 2020 at 13:20 Dimitri KopriwaDimitri Kopriwa 14.4k33 gold badges116 silver badges231 bronze badges2 Answers
Reset to default 7You need to setup the translation in your top level ponent, like App.js. Then, you have to create 2 json files: fr.json
and en.json
in /src/locales/
.
Finally, in any screen, you have to import i18n
and use the t()
function to translate strings.
In App.js
import React, { useEffect, useState } from 'react'
import { loadLocale } from './locales/i18n'
export default function App() {
const [theme, setTheme] = useState(null)
useEffect(() => {
init()
}, [])
const init = async () => {
await loadLocale()
}
return (
<AppContainer />
)
}
In i18n.js
import * as Localization from 'expo-localization'
import i18n from 'i18n-js'
i18n.defaultLocale = 'fr'
i18n.locale = 'fr'
i18n.fallbacks = true
export const loadLocale = async () => {
for (const locale of Localization.locales) {
if (i18n.translations[locale.languageCode] !== null) {
i18n.locale = locale.languageCode
switch (locale.languageCode) {
case 'en':
import('./en.json').then(en => {
i18n.translations = { en }
})
break
default:
case 'fr':
import('./fr.json').then(fr => {
i18n.translations = { fr }
})
break
}
break
}
}
}
export default i18n
In HomeView.js
import React from 'react'
import i18n from '../locales/i18n'
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1 }}>
<Text>{i18n.t('home.wele')}</Text>
<Text>{i18n.t('home.content')}</Text>
</View>
)
}
export default HomeView
In fr.json
{
"home": {
"wele": "Bienvenue",
"content": "Du contenu ici"
}
}
In order to change between languages and avoid getting [missing "X.string" translation] error you can add a function like this "changeLanguage" function below:
// Imagine you have spanish and english languages support
import es from './locales/es';
import en from './locales/en';
const availableTranslations = {
es,
en
};
/* This function is useful to load spanish or english language translations and set the corresponding locale */
const changeLanguage = (languageCode) => {
I18n.translations = {
[languageCode]: availableTranslations[languageCode]
};
I18n.locale = languageCode;
};
本文标签: javascriptHow to use setLocale within i18njsStack Overflow
版权声明:本文标题:javascript - How to use setLocale within i18n-js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741236685a2363155.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论