3.1.0 • Published 22 days ago

react-native-localize v3.1.0

Weekly downloads
114,952
License
MIT
Repository
github
Last release
22 days ago

🌍  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:

npm.io

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 import in your __mocks__ directory:

// __mocks__/react-native-localize.ts
export * from "react-native-localize/mock";

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.

@neur0base/app-sdk@borderfreefinancial/revoshowmobileui-test@borderfreefinancial/revoshowui-test@dededeck/dede-tablet@snooper/coresezzle-appiusify-module-client_managementiusify-module-tasksreact-native-wocode-frameworkreact-native-digitcamera-pireact-native-firebase-componentssas-app-core@infinitebrahmanuniverse/nolb-react-native-l@brandingbrand/fsengagement@everything-registry/sub-chunk-2583wallet-app@evenset/react-native-fundamental-components@ezviz/ys-rn-pub-mini@functionland/component-library@functionland/fx-components@eohjsc/react-native-personal-health@eohjsc/react-native-smart-city@eohjsc/react-native-smart-parking@fentest/components@fentest/compsvivanta-connect-react-native@inngageregistry/inngage-react@hinh-eoh/test_publishyns-rntemplate-appcitizenship_app_newsoak-frontend-baseraptorx-react-native-sdquec-panel-templatern-chat-matrixrn-btaskee-booking-sdkrn-matrixrn-wappsto-networksrn-vayvaysentral.rnsmart-rn-uistaging_loopcoderzy260-rn@digieggs/rn-country-code-pickersta-localizestorybook-compostorybook-components@kvell/kvell-app-ui@gaozh1024/rn-libs@ledvance/eziot-rn-pub@manishoo/rn-matrix-core@manishoo/rn-matrix-ui@kafudev/react-native-shellreact-native-kora-botsdknabta-translations@blacksakura013/th-datepickermobile-storybook@colorfy-software/localify@apparatus/effects-time-information-events@apparatus/meta-start-presetadalo-localizeadalo-multilanguageadalo-multilanguage-custommbco-react-native-common-uimbco-react-native-translationmatrix-reactnative-sdk@brandingbrand/fsenv@brandingbrand/fsapppalette-mobilebtaskee-sdk-src-booking-rnpoket-app-testreact-native-nationality-country-calling-code-pickerreact-native-calendar-snreact-native-dxmb-uireact-native-nilbuttonreact-native-matrix-uireact-native-kore-botsdkreact-native-language-translationreact-native-proximus-language-translationreact-native-device-info-utilsreact-native-get-ipreact-native-haitestpackagereact-native-btaskee-mini-appreact-native-graphql-boilerplatereact-native-digitcamera-sensorreact-native-region-country-pickerreact-native-template-xjbseckillreact-native-setlangreact-native-sdk-deltareact-native-sdk-metareact-native-sdk-portkeyreact-native-sdk-betareact-native-vpe-betareact-native-voicebox-speech-recreact-native-ui-xepcra-template-xujianbo-seckillcreate-rn-redux-toolkit-boilerplate@praella/klarify-utils@portkey/react-native-sdk@womasoft/rn-libs@wallet_framework/rn
3.1.0

22 days ago

3.0.6

3 months ago

3.0.5

4 months ago

3.0.4

5 months ago

3.0.3

6 months ago

3.0.2

10 months ago

3.0.1

10 months ago

3.0.0-beta.1

1 year ago

3.0.0-beta.0

1 year ago

2.2.5

1 year ago

2.2.6

1 year ago

3.0.0

12 months ago

2.2.4

2 years ago

2.2.3

2 years ago

2.2.2

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.1.6

2 years ago

2.1.8

2 years ago

2.1.7

2 years ago

2.1.9

2 years ago

2.1.5

3 years ago

2.1.4

3 years ago

2.1.2

3 years ago

2.1.3

3 years ago

2.1.1

3 years ago

2.0.3

3 years ago

2.1.0

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.4.3

3 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago