3

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 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

13 Comments

Hi, thank you for getting back to me, will give this a try. Do you suggest i use import i18n from 'i18n-js' like the code above or this library import I18n from 'ex-react-native-i18n'?
I personally use i18next
is there a difference between them?
if i decide to use i18next do i need make alot of changes to my code?
I work in ejected-projects ... which is kinda different than expo-projects (I mean ejected-projects give u more flexibility over what modules to choose) ... I'd say go with the one that 'd work smoothly with expo..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.