react-native-localize: RNLocalize gets an undefined object on RNLocalize.initialContants

Hello !

We used react-native-languages with i18n-js in our app to easly manage the translations. Now that it has been updated and that we’ve done the migration, we faced a strange exception as described below.

Describe your environment

  • react-native-localize v 1.0.1
  • react-native v 0.57.7
  • Target platform: both
  • Device : Android Virtual Device
  • OS version: Android 6.0
  • DevTools: Android Studio v 3.1.4 with build tools v 27.0.3

How to repeat issue and example

Before the update to RNLocalize, everything worked fine, I did nothing more than copying the example. After the update, I followed the steps described here.

  1. Unlink then uninstall react-native-languages with npm
  2. Install then link react-native-localize
  3. Edited my i18n.js file to be the exact same as in the link above
  4. react-native run-android now instantly throws me this exception undefined is not an object (evaluating 'RNLocalize.initialContants') even in the console it’s the only thing printed.

I’m not really confident with react-native and I don’t have any clue about what’s happening. I created a new project with only react-native-localize and i18n-js installed, but I got the same result except the value was nulland not undefined.

Basically just using import * as RNLocalize from 'react-native-localize throws this exception. Have you any clues about what could cause this issue ?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 8
  • Comments: 44 (12 by maintainers)

Most upvoted comments

I did have the same errors on iOS. (did not test android yet). Manual linking did work, adding the RNLocalize.xcodeproj to the libraries on the project navigator AND (missing in the docs) adding libRNLocalize.a in the linked frameworks and libraries in the general tab of the target app.

So it seems the automatic linking is not working as advertised.

@duluong This package will not works with Expo (you can’t install native packages in Expo without exjecting to Expokit). Check the Expo documentation to achieve the same effect: https://docs.expo.io/versions/v33.0.0/sdk/localization/

just link the package with project… autolink is not working for this package.

react-native link react-native-localize

(root) -> cd .\android\ -> .\gradlew clean -> cd.. -> react-native run-android

I’m having the same issues with 0.60-rc1 version on iOS. Android works fine

I just tried using:

react-native init TestRNLocalize && cd TestRNLocalize
yarn add react-native-localize
react-native link react-native-localize
react-native run-ios
react-native run-android
import * as RNLocalize from "react-native-localize";

console.log(RNLocalize.getLocales());

Without any issues. It’s probably due to react-native link, which version of react-native-cli and react-native do you use? Do a react-native -v in your project and give me the output please 🙂

Ignore it, you’re right, I forgot to update the mock file 😃 Works for me now.

I’m experiencing the same issue, making at least our jest tests fail:

● Test suite failed to run

    TypeError: Cannot read property 'initialConstants' of undefined

    > 1 | import * as RNLocalize from 'react-native-localize';
        |                                                     ^
      2 | import I18n from 'i18n-js';
      3 | 
      4 | import en from '/src/locales/en';

      at Object.<anonymous> (node_modules/react-native-localize/index.js:19:28)
      at Object.<anonymous> (src/config/i18n.js:1:341)
      ...

@duluong This package will not works with Expo (you can’t install native packages in Expo without exjecting to Expokit). Check the Expo documentation to achieve the same effect: https://docs.expo.io/versions/v33.0.0/sdk/localization/

Oh my god, thank you so much for clarifying this! I’ve tried for hours to solve this unsolvable problem because I was trying to use it with my managed Expo project…

There’s a react native example called RocketChat: https://github.com/RocketChat/Rocket.Chat.ReactNative This example uses both i18n and React-Native-Localize, but they have an iOS and android folder, which means they ejected their project or something? How does this work and how could I make my expo project compatible with react-native-localize?

I’m having the same issue on Android devices. Is there a known fix? I tried both the manual configuration and react-native link.

@plahteenlahti On React Native 0.60, you have nothing more to do than yarn add react-native localize and pod install. Autolinking will do the rest. Check https://github.com/react-native-community/react-native-localize/tree/master/example

I’ve upgraded my app to the latest react-native version (use react-native-git-upgrade!), solving many issues that popped up along the way. These issues may be related to some other issue in my project that I am not aware of, but just in case someone else encounters these problems as well, I will share my experiences and the solution that worked in my case.

Building on iOS requires some changing of a script that installs third-party elements (see this issue and this issue). Fix the path of glogs in the ios-install-third-party.sh script to: "\"../../$SCRIPTDIR/ios-configure-glog.sh\"" (for more info see my comment)

Anyway, returning to this issue: after upgrading and solving all the other issues, this error disappeared. I do not know what exactly fixed the issue, but upgrading to the latest react-native version seems to do the trick.

I just need to rebuild native module and then it works

Sorry, how did you do that I am stuck in this error after migrating from RN 0.59.10 to 0.60.0

I had this issue on android. It’s because the linking didn’t happen correctly in the MainApplication.java. I am using react-native-navigation, where I had to change my MainApplication.java So key to the solution is to manually link it.

can you please give an example of how you changed this?

Sure, It is documented here on how to do it manually: https://github.com/react-native-community/react-native-localize#manual-android

I had this issue on android. It’s because the linking didn’t happen correctly in the MainApplication.java. I am using react-native-navigation, where I had to change my MainApplication.java So key to the solution is to manually link it.

can you please give an example of how you changed this?

I solved it by adding RNLocalize.xcodeproj to Libraries manually.