React native project with expo-localization and i18n-js

我的梦境 提交于 2021-01-13 09:42:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!