问题
i am currently trying to implement localization depending on the system language of the device but something is not working as required.
I followed the exact same code on expo documentation, for eg. for the button i have i keep getting [MISSING "EN-GB.LOGIN" TRANSLATION]
instead of LOGIN
.
Here is my code:
welcomeScreen.js
import * as Localization from 'expo-localization';
import i18n from 'i18n-js';
i18n.translations = {
en: { login: 'LOGIN'},
ar: { login: 'تسجيل الدخول'},
};
i18n.locale = Localization.locale;
i18n.fallbacks = true;
function WelcomeScreen() {
return (
<Button
title={i18n.t('login')}
/>
)}
So instead of the code above i decided to go with this:
i18n.js
import i18n from 'i18n-js';
import * as Localization from 'expo-localization';
import ar from './locales/ar';
import en from './locales/en';
i18n.translations = {
'en': en,
'ar': ar,
};
i18n.locale = Localization.locale.search(/-|_/) !== -1?
Localization.locale.slice(0, 2): Localization.locale;
i18n.fallbacks = true;
export default i18n;
en.js
const en = {
'SignUp':{
SignUp:"Sign Up"
}}
ar.js
const ar = {
'SignUp':{
SignUp:"الاشتراك"
}}
SignUpScreen.js
import I18n from '../config/i18n';
function RegisterScreen(props) {
return(
<Button title={I18n.t('SignUp.SignUp')}
)}
回答1:
If you try to console.log(Localization.locale)
... it's not gonna be just en
(the key in i18n.translations
expected by i18n
) ... it'd in the form of en_countryCode
... so you have to slice that part
i18n.locale = Localization.locale.search(/-|_/) !== -1
? Localization.locale.slice(0, 2)
: Localization.locale;
来源:https://stackoverflow.com/questions/65561657/react-native-project-with-expo-localization-and-i18n-js