Skip to content

Repository files navigation

🌍 react-native-localize

A toolbox for your React Native app localization.

npm versionnpmPlatform - Android, iOS and WebMITstyled with prettier

Support

package nameversionreact-native version
react-native-localize1.0.0+0.56.0+
react-native-languages2.0.10.48.0 - 0.55.4

Setup

$ npm install --save react-native-localize
# --- or ---
$ yarn add react-native-localize

Don't forget to run pod install after thatΒ !

πŸ†˜ Manual linking

Because this package targets React Native 0.60.0+, you will probably don't need to link it manually. Otherwise if it's not the case, follow this additional instructions:

πŸ‘€ See manual linking instructions

iOS

Add this line to your ios/Podfile file, then run pod install.

target 'YourAwesomeProject'do# …
pod 'RNLocalize', :path =>'../node_modules/react-native-localize'
end

Android

  1. Add the following lines to android/settings.gradle:
include ':react-native-localize'
project(':react-native-localize').projectDir =newFile(rootProject.projectDir, '../node_modules/react-native-localize/android')
  1. Add the implementation line to the dependencies in android/app/build.gradle:
dependencies {
// ...
implementation project(':react-native-localize')
}
  1. Add the import and link the package in MainApplication.java:
importcom.reactcommunity.rnlocalize.RNLocalizePackage; // <- add the RNLocalizePackage importpublicclassMainApplicationextendsApplicationimplementsReactApplication {
// …@OverrideprotectedList<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = newPackageList(this).getPackages();
// …packages.add(newRNLocalizePackage());
returnpackages;
}
// …
}

Web support

This package supports react-native-web. Follow their official guide to configure webpack.

Basic usage example

import*asRNLocalizefrom"react-native-localize";console.log(RNLocalize.getLocales());console.log(RNLocalize.getCurrencies());RNLocalize.addEventListener("change",()=>{// do localization related stuff…});

API

getLocales()

Returns the user preferred locales, in order.

Method type

typegetLocales=()=>Array<{languageCode: string;scriptCode?: string;countryCode: string;languageTag: string;isRTL: boolean;}>;

Usage example

console.log(RNLocalize.getLocales());/* -> [ { countryCode: "GB", languageTag: "en-GB", languageCode: "en", isRTL: false }, { countryCode: "US", languageTag: "en-US", languageCode: "en", isRTL: false }, { countryCode: "FR", languageTag: "fr-FR", languageCode: "fr", isRTL: false },] */

getNumberFormatSettings()

Returns number formatting settings.

Method type

typegetNumberFormatSettings=()=>{decimalSeparator: string;groupingSeparator: string;};

Usage example

console.log(RNLocalize.getNumberFormatSettings());/* -> { decimalSeparator: ".", groupingSeparator: ",",} */

getCurrencies()

Returns the user preferred currency codes, in order.

Method type

typegetCurrencies=()=>Array<string>;

Usage example

console.log(RNLocalize.getCurrencies());// -> ["EUR", "GBP", "USD"]

getCountry()

Returns the user current country code (based on its device locale, not on its position).

Method type

typegetCountry=()=>string;

Usage example

console.log(RNLocalize.getCountry());// -> "FR"

getCalendar()

Returns the user preferred calendar format.

Method type

typegetCalendar=()=>"gregorian"|"japanese"|"buddhist";

Usage example

console.log(RNLocalize.getCalendar());// -> "gregorian"

getTemperatureUnit()

Returns the user preferred temperature unit.

Method type

typegetTemperatureUnit=()=>"celsius"|"fahrenheit";

Usage example

console.log(RNLocalize.getTemperatureUnit());// -> "celsius"

getTimeZone()

Returns the user preferred timezone (based on its device settings, not on its position).

Method type

typegetTimeZone=()=>string;

Usage example

console.log(RNLocalize.getTimeZone());// -> "Europe/Paris"

uses24HourClock()

Returns true if the user prefers 24h clock format, false if he prefers 12h clock format.

Method type

typeuses24HourClock=()=>boolean;

Usage example

console.log(RNLocalize.uses24HourClock());// -> true

usesMetricSystem()

Returns true if the user prefers metric measure system, false if he prefers imperial.

Method type

typeusesMetricSystem=()=>boolean;

Usage example

console.log(RNLocalize.usesMetricSystem());// -> true

usesAutoDateAndTime()

Tells if the automatic date & time setting is enabled on the phone. Android only

Method type

typeOption<T>=T|undefined;typeusesAutoDateAndTime=()=>Option<boolean>;

Usage example

console.log(RNLocalize.usesAutoDateAndTime());// true or false

usesAutoTimeZone()

Tells if the automatic time zone setting is enabled on the phone. Android only

Method type

typeOption<T>=T|undefined;typeusesAutoTimeZone=()=>Option<boolean>;

Usage example

console.log(RNLocalize.usesAutoTimeZone());

addEventListener() / removeEventListener()

Allows you to listen for any localization change.

Methods type

typeaddEventListener=(type: "change",handler: Function)=>void;typeremoveEventListener=(type: "change",handler: Function)=>void;

Usage example

functionhandleLocalizationChange(){console.log(RNLocalize.getLocales());}RNLocalize.addEventListener("change",handleLocalizationChange);// …later (ex: component unmount)RNLocalize.removeEventListener("change",handleLocalizationChange);

findBestAvailableLanguage()

Returns the best language tag possible and its reading direction (⚠️it respects the user preferred languages list order, see explanations). Useful to pick the best translation available.

Method type

typefindBestAvailableLanguage=(languageTags: Array<string>,)=>{languageTag: string;isRTL: boolean}|void;

Usage example

console.log(RNLocalize.findBestAvailableLanguage(["en-US","en","fr"]));// -> { languageTag: "en-US", isRTL: false }

Examples with i18n-js

Browse the files in the /example directory.

How to test your code

Because it's a native module, you might need to mock this package to run your tests flawlessly.
Here is an example for Jest, adapt it to your needsΒ :

// __mocks__/react-native-localize.jsconstgetLocales=()=>[// 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 fallbackconstfindBestAvailableLanguage=()=>({languageTag: "en-US",isRTL: false,});constgetNumberFormatSettings=()=>({decimalSeparator: ".",groupingSeparator: ",",});constgetCalendar=()=>"gregorian";// or "japanese", "buddhist"constgetCountry=()=>"US";// the country code you wantconstgetCurrencies=()=>["USD","EUR"];// can be empty arrayconstgetTemperatureUnit=()=>"celsius";// or "fahrenheit"constgetTimeZone=()=>"Europe/Paris";// the timezone you wantconstuses24HourClock=()=>true;constusesMetricSystem=()=>true;constaddEventListener=jest.fn();constremoveEventListener=jest.fn();export{findBestAvailableLanguage,getLocales,getNumberFormatSettings,getCalendar,getCountry,getCurrencies,getTemperatureUnit,getTimeZone,uses24HourClock,usesMetricSystem,addEventListener,removeEventListener,};

Add project's supported localizations (iOS)

About

🌍 A toolbox for your React Native app localization (formerly react-native-languages)

Resources

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages