Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native and Intl polyfill required on Android device

I recently updated my Android Studio and many components/sdk and since then React-Intl complains about intl library missing, even though it was working fine before.

I have installed the intl polyfill and I import it at the very top of my main file App.js. I also import the localeData from react-intl and add it. Then, I render my view within the IntlProvider specifying the locale with no messages (I only use FormattedNumber for now)

This is a simplified version of my code:

import 'intl'; import { IntlProvider, FormattedNumber, addLocaleData } from 'react-intl'; import en from 'react-intl/locale-data/en';  addLocaleData(en);  [...]  render() {     return (         <IntlProvider locale="en">             <Text>                 <FormattedNumber value={123} />             </Text>         </IntlProvider>     ) } 

I get the following error:

[React Intl] Error formatting number. ReferenceError: No locale data has been provided for this object yet.

enter image description here

I don't understand what's going on. Anyone encounter the same issue?

Thanks

like image 859
alexmngn Avatar asked Jan 19 '17 08:01

alexmngn


People also ask

Can I use INTL IN react native?

React Native​ If you're using react-intl in React Native, make sure your runtime has built-in Intl support (similar to JSC International variant).

What is INTL IN react native?

Anil. , March 24, 2022. 15 min read. Internationalization or i18n is the design and development of a product, application, or document content that enables easy localization for target audiences that vary in culture, region, or language.

How to debug on real device React Native?

To enable USB debugging on your device, you will first need to enable the "Developer options" menu by going to Settings → About phone → Software information and then tapping the Build number row at the bottom seven times. You can then go back to Settings → Developer options to enable "USB debugging".


2 Answers

Instead of importing locale-data from react-intl, I have resolved the issue importing the polyfill and the locale data from intl

Install intl

npm install intl 

Add this at the very top of your app:

import 'intl'; import 'intl/locale-data/jsonp/en'; 
like image 126
alexmngn Avatar answered Sep 17 '22 14:09

alexmngn


Modifying the "build.gradle"

On android, you can modify the "build.gradle" file inside /android/app/build.gradle. Remember, it is NOT the file in /android/app/gradle/build.gradle.

then, go to the sited file and search for:

        /**      * The preferred build flavor of JavaScriptCore.      *      * For example, to use the international variant, you can use:      * `def jscFlavor = 'org.webkit:android-jsc-intl:+'`      *      * The international variant includes ICU i18n library and necessary data      * allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that      * give correct results when using with locales other than en-US.  Note that      * this variant is about 6MiB larger per architecture than default.      */      def jscFlavor = 'org.webkit:android-jsc:+'  

now, modify the last line, or simply comment it out and copy and paste the similar one above. And the result will be this:

        /**      * The preferred build flavor of JavaScriptCore.      *      * For example, to use the international variant, you can use:      * `def jscFlavor = 'org.webkit:android-jsc-intl:+'`      *      * The international variant includes ICU i18n library and necessary data      * allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that      * give correct results when using with locales other than en-US.  Note that      * this variant is about 6MiB larger per architecture than default.      */      //def jscFlavor = 'org.webkit:android-jsc:+'      def jscFlavor = 'org.webkit:android-jsc-intl:+'  
like image 27
Gerson Dantas Avatar answered Sep 20 '22 14:09

Gerson Dantas