skip to Main Content

I am trying to implement localization for Expo React Native app.
My i18n.js file looks like this:

import { getLocales } from 'expo-localization';
import { I18n } from 'i18n-js';

// Set the key-value pairs for the different languages you want to support.
const i18n = new I18n({
  en: { welcome: 'Hello' },
  lt: { welcome: 'Labas' },
});

// Set the locale once at the beginning of your app.
i18n.locale = getLocales()[0].languageCode;

console.log(i18n.t('welcome'));

I did

npm install expo-localization
npx expo install i18n-js

And I launch with

npx expo start

I get error:

Cannot find native module 'ExpoLocalization', js engine: hermes

My versions in package-lock.json:

    "i18n-js": "^4.4.3",

    "expo-localization": "~15.0.3",

I also tried setting jsEngine in app.json:

  "expo": {
    "jsEngine": "hermes",

None of this really helped. Any suggestions?

2

Answers


  1. Chosen as BEST ANSWER

    Something was wrong with my project. I also needed maps but it didnt allowed me to use maps either. So I created new project using

    npx create-expo-app react-native-maps-demo
    cd react-native-maps-demo
    

    And then it worked - I was able to add localization as well as maps.


  2. The error mentions "ExpoLocalization", but the correct package name is expo-localization. There’s clearly a difference, and this could be causing the issue. We need more details on the error to be sure.

    In the meantime, check your app.json to make sure it includes the plugin:

     {
      "expo": {
        "plugins": ["expo-localization"]
      }
    }
    

    Try deleting node_modules and package-lock.json may be you used npm and yarn.

    Update :
    This how i use getLocales :

    import { getLocales } from "expo-localization";  
    import { I18n } from "i18n-js";  
      
    import translations from "../app/locales/translations.json";  
    const i18n = new I18n(translations);  
    export const locale = getLocales()[0].languageCode || "en";  
    i18n.locale = locale;  
    export { i18n };.
    

    Try this and give me feedback.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search