• Stars
    star
    2,259
  • Rank 20,387 (Top 0.5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐ŸŒ A toolbox for your React Native app localization

๐ŸŒ ย react-native-localize

A toolbox for your React Native app localization.

mit licence npm version npm downloads
platform - android platform - ios platform - macos platform - web

Support

This library follows the React Native releases support policy.
It is supporting the latest version, and the two previous minor series.

Setup

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

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

Web support

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

Debugging

As this library uses synchronous native methods, remote debugging (e.g. with Chrome) is no longer possible.
Instead, you should use Flipper ๐Ÿฌ.

Basic usage example

import { getCurrencies, getLocales } from "react-native-localize";

console.log(getLocales());
console.log(getCurrencies());

API

getLocales()

Returns the user preferred locales, in order.

Method type

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

Usage example

import { getLocales } from "react-native-localize";

console.log(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

type getNumberFormatSettings = () => {
  decimalSeparator: string;
  groupingSeparator: string;
};

Usage example

import { getNumberFormatSettings } from "react-native-localize";

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

getCurrencies()

Returns the user preferred currency codes, in order.

Method type

type getCurrencies = () => string[];

Usage example

import { getCurrencies } from "react-native-localize";

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

getCountry()

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

Method type

type getCountry = () => string;

Usage example

import { getCountry } from "react-native-localize";

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

Note

Devices using Latin American regional settings will return "UN" instead of "419", as the latter is not a standard country code.


getCalendar()

Returns the user preferred calendar format.

Method type

type getCalendar = () =>
  | "gregorian"
  | "buddhist"
  | "coptic"
  | "ethiopic"
  | "ethiopic-amete-alem"
  | "hebrew"
  | "indian"
  | "islamic"
  | "islamic-umm-al-qura"
  | "islamic-civil"
  | "islamic-tabular"
  | "iso8601"
  | "japanese"
  | "persian";

Usage example

import { getCalendar } from "react-native-localize";

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

getTemperatureUnit()

Returns the user preferred temperature unit.

Method type

type getTemperatureUnit = () => "celsius" | "fahrenheit";

Usage example

import { getTemperatureUnit } from "react-native-localize";

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

getTimeZone()

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

Method type

type getTimeZone = () => string;

Usage example

import { getTimeZone } from "react-native-localize";

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

uses24HourClock()

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

Method type

type uses24HourClock = () => boolean;

Usage example

import { uses24HourClock } from "react-native-localize";

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

usesMetricSystem()

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

Method type

type usesMetricSystem = () => boolean;

Usage example

import { usesMetricSystem } from "react-native-localize";

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

usesAutoDateAndTime()

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

Method type

type usesAutoDateAndTime = () => boolean | undefined;

Usage example

import { usesAutoDateAndTime } from "react-native-localize";

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

usesAutoTimeZone()

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

Method type

type usesAutoTimeZone = () => boolean | undefined;

Usage example

import { usesAutoTimeZone } from "react-native-localize";

console.log(usesAutoTimeZone());

findBestLanguageTag()

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

type findBestLanguageTag = (
  languageTags: string[],
) => { languageTag: string; isRTL: boolean } | void;

Usage example

import { findBestLanguageTag } from "react-native-localize";

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

Examples with @formatjs/intl

Browse the files in the /example directory.

How to update supported localizations (iOS)

You can add / remove supported localizations in your Xcode project infos:

How to test your code

Because it's a native module, you need to mock this package.
The package provides a default mock you may use in your __mocks__/react-native-localize.js or jest.setup.js.

import localizeMock from "react-native-localize/mock";

jest.mock("react-native-localize", () => localizeMock);

Sponsors

This module is provided as is, I work on it in my free time.

If you or your company uses it in a production app, consider sponsoring this project ๐Ÿ’ฐ. You also can contact me for premium enterprise support: help with issues, prioritize bugfixes, feature requests, etc.

Sponsors list

More Repositories

1

react-native-permissions

An unified permissions API for React Native on iOS, Android and Windows.
Objective-C++
4,010
star
2

react-native-bootsplash

๐Ÿš€ Show a splash screen during app startup. Hide it when you are ready.
TypeScript
3,620
star
3

react-native-bars

Components to control your app status and navigation bars.
TypeScript
288
star
4

react-native-dev-menu

Add custom items to the React Native dev menu.
Java
204
star
5

react-native-bootsplash-lottie-example

Java
53
star
6

react-atomic-state

Dead simple React global state management based on use-sync-external-store.
TypeScript
47
star
7

valienv

A simple environment variables validator for Node.js, web browsers and React Native โœ“
TypeScript
32
star
8

prisma-cursor-pagination

Relay cursor pagination helpers for prisma
TypeScript
15
star
9

purestead

Use Laravel Homestead the Vagrant way.
Shell
14
star
10

vagrant-rethinkdb

Try RethinkDB with Vagrant
JavaScript
6
star
11

react-router-with-chicane

4
star
12

react-native-module-template

Java
3
star
13

sound-mind.css

Default HTML elements style for modern daysย ย ๐Ÿ‘€
CSS
3
star
14

lillefp15-demo

A reason-react-native app to check Paris piscines pools opening hours
Reason
3
star
15

types-testing-library-vitest-dom

TypeScript definitions for @testing-library/jest-dom (with vitest)
3
star
16

PSD-Browser-Preview

Sync Photoshop and your browser
JavaScript
3
star
17

next-type-routes

An experiment to make next.js routes usage safer.
TypeScript
2
star
18

twitter-eraser

A simple script to purge old tweets
JavaScript
2
star
19

personal-website

Reason
1
star
20

ScrollViewIssue

JavaScript
1
star
21

punk-api-exercise

HTML
1
star
22

sign-machine-poc

TypeScript
1
star
23

rn-location-permission-example

Java
1
star
24

wff

An extend based semantic grid for Sass
CSS
1
star
25

bump-app-package-version

A GitHub action to bump your app package.json version
TypeScript
1
star