A toolbox for your React Native app localization.
This project was known as react-native-languages and has been renamed to reflect new APIs possibilities.
Find more informations about this change here.
package name | version | react-native version |
---|---|---|
react-native-localize | 1.0.0+ | 0.56.0+ |
react-native-languages | 2.0.1 | 0.48.0 - 0.55.4 |
$ npm install --save react-native-localize
# --- or ---
$ yarn add react-native-localize
$ react-native link react-native-localize
- In the XCode's "Project navigator", right click on your project's Libraries folder ➜
Add Files to <...>
- Go to
node_modules
➜react-native-localize
➜ios
➜ selectRNLocalize.xcodeproj
- Add
RNLocalize.a
toBuild Phases -> Link Binary With Libraries
- Add the following lines to
android/settings.gradle
:
include ':react-native-localize'
project(':react-native-localize').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-localize/android')
- Add the compile line to the dependencies in
android/app/build.gradle
:
dependencies {
// ...
compile project(':react-native-localize')
}
- Add the import and link the package in
MainApplication.java
:
import com.reactcommunity.rnlocalize.RNLocalizePackage; // <-- Add the RNLocalize import
public class MainApplication extends Application implements ReactApplication {
// …
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
// …
new RNLocalizePackage() // <-- Add it to the packages list
);
}
// …
}
import * as RNLocalize from "react-native-localize";
console.log(RNLocalize.getLocales());
console.log(RNLocalize.getCurrencies());
RNLocalize.addEventListener("change", () => {
// do localization related stuff…
});
Returns the user preferred locales, in order.
type getLocales = () => Array<{
languageCode: string;
scriptCode?: string;
countryCode: string;
languageTag: string;
isRTL: boolean;
}>;
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 },
] */
Returns the user preferred currency codes, in order.
type getCurrencies = () => Array<string>;
console.log(RNLocalize.getCurrencies());
// -> ["EUR", "GBP", "USD"]
Returns the user current country code (based on its device locale, not on its position).
type getCountry = () => string;
console.log(RNLocalize.getCountry());
// -> "FR"
Returns the user preferred calendar format.
type getCalendar = () => "gregorian" | "japanese" | "buddhist";
console.log(RNLocalize.getCalendar());
// -> "gregorian"
Returns the user preferred temperature unit.
type getTemperatureUnit = () => "celsius" | "fahrenheit";
console.log(RNLocalize.getTemperatureUnit());
// -> "celsius"
Returns the user preferred timezone (based on its device settings, not on its position).
type getTimeZone = () => string;
console.log(RNLocalize.getTimeZone());
// -> "Europe/Paris"
Returns true
if the user prefers 24h clock format, false
if he prefers 12h clock format.
type uses24HourClock = () => boolean;
console.log(RNLocalize.uses24HourClock());
// -> true
Returns true
if the user prefers metric measure system, false
if he prefers imperial.
type usesMetricSystem = () => boolean;
console.log(RNLocalize.usesMetricSystem());
// -> true
Allows you to listen for any localization change.
type addEventListener = (type: "change", handler: Function) => void;
type removeEventListener = (type: "change", handler: Function) => void;
function handleLocalizationChange() {
console.log(RNLocalize.getLocales());
}
RNLocalize.addEventListener("change", handleLocalizationChange);
// …later (ex: component unmount)
RNLocalize.removeEventListener("change", handleLocalizationChange);
Returns the best language tag possible and its direction (if it can find one). Useful to choose the best translation available.
type findBestAvailableLanguage = (
languageTags: Array<string>,
) => { languageTag: string; isRTL: boolean } | void;
console.log(RNLocalize.findBestAvailableLanguage(["en-US", "en", "fr"]));
// -> { languageTag: "en-US", isRTL: false }
Examples with i18n-js
Browse the files in the /example directory.