How to fix NativeModule.RNLocalize is null?

2.3k Views Asked by At

I am using react-native-localization library is my RN project.

My RN version is 0.59.4

I already get the project to work on android as expected, but the problem is with the IOS build.

I npm installed both react-native-localization and react-native-localize and linked them as described in their github manual using pod.

I did everything I could from linking to clean and building the project multiple times.

But I'm getting this error when running react-native-localize NativeModule.RNLocalize is null. To fix this issue try these steps and I did what the console told me but IN VAIN.

Can someone please tell me what I'm doing wrong?

1

There are 1 best solutions below

1
On

Create a mock file like this (in the root directory):

__mocks__/react-native-localize.js

Check that __mock__ has two underscores.

This is an example of the file:

const getLocales = () => [
  // you can choose / add the locales you want
  { countryCode: "US", languageTag: "en-US", languageCode: "en", isRTL: false },
  { countryCode: "FR", languageTag: "fr-FR", languageCode: "fr", isRTL: false },
];

// use a provided translation, or return undefined to test your fallback
const findBestAvailableLanguage = () => ({
  languageTag: "en-US",
  isRTL: false,
});

const getNumberFormatSettings = () => ({
  decimalSeparator: ".",
  groupingSeparator: ",",
});

const getCalendar = () => "gregorian"; // or "japanese", "buddhist"
const getCountry = () => "US"; // the country code you want
const getCurrencies = () => ["USD", "EUR"]; // can be empty array
const getTemperatureUnit = () => "celsius"; // or "fahrenheit"
const getTimeZone = () => "Europe/Paris"; // the timezone you want
const uses24HourClock = () => true;
const usesMetricSystem = () => true;

const addEventListener = jest.fn();
const removeEventListener = jest.fn();

export {
  findBestAvailableLanguage,
  getLocales,
  getNumberFormatSettings,
  getCalendar,
  getCountry,
  getCurrencies,
  getTemperatureUnit,
  getTimeZone,
  uses24HourClock,
  usesMetricSystem,
  addEventListener,
  removeEventListener,
};

You don't have to import the node_module of react-native-localization because each file under __mocks__ will be automatically mocked.

Try to run the test again and check if the error persist.

Edit: in my case, the only function that I needed from react-native-localize was uses24HourClock() so my mock file was very short:

const uses24HourClock = () => false;

export { uses24HourClock };

That was all for me.