14.8.4 • Published 5 days ago

expo-localization v14.8.4

Weekly downloads
27,147
License
MIT
Repository
github
Last release
5 days ago

expo-localization

expo-localization enables you to interface with the native device locale.

Installation

Now, you need to install the package from npm registry.

npm install expo-localization or yarn add expo-localization

iOS (Cocoapods)

If you're using Cocoapods, add the dependency to your Podfile:

pod 'EXLocalization', path: '../node_modules/expo-localization/ios'

and if not already included

pod 'EXCore', path: '../node_modules/expo-core/ios'

and run pod install.

Android

  1. Append the following lines to android/settings.gradle:

    include ':expo-localization'
    project(':expo-localization').projectDir = new File(rootProject.projectDir, '../node_modules/expo-localization/android')

    and if not already included

    include ':expo-core'
    project(':expo-core').projectDir = new File(rootProject.projectDir, '../node_modules/expo-core/android')
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:

    api project(':expo-localization')

    and if not already included

    api project(':expo-core')

Some Unimodules are not included in the default ExpoKit suite, these modules will needed to be added manually. If your Android build cannot find the Native Modules, you can add them like this:

./android/app/src/main/java/host/exp/exponent/MainActivity.java

@Override
public List<Package> expoPackages() {
  // Here you can add your own packages.
  return Arrays.<Package>asList(
    new LocalizationPackage() // Include this.
  );
}

Usage

import React from 'react';
import { Text } from 'react-native';
import { Localization } from 'expo-localization';

import i18n from 'i18n-js';

const en = {
  foo: 'Foo',
  bar: 'Bar {{someValue}}',
};

const fr = {
  foo: 'como telle fous',
  bar: 'chatouiller {{someValue}}',
};

i18n.fallbacks = true;
i18n.translations = { fr, en };
i18n.locale = Localization.locale;

export default class LitView extends React.Component {
  componentWillMount() {
    this._subscription = Localization.addListener(({ locale }) => {
      i18n.locale = locale;
    });
  }

  componentWillUnmount() {
    if (!!this._subscription) {
      this._subscription.remove();
    }
  }

  render() {
    return (
      <Text>
        {i18n.t('foo')} {i18n.t('bar', { someValue: Date.now() })}
      </Text>
    );
  }
}

API

Constants

This API is mostly synchronous and driven by constants.

Localization.locale: string

Native device language, returned in standard format. ex: en-US, es-US.

Localization.locales: Array<string>

List of all the native languages provided by the user settings. These are returned in the order the user defines in their native settings.

Localization.country: ?string

Country code for your device.

Localization.isoCurrencyCodes: ?Array<string>

A list of all the supported ISO codes.

Localization.timezone: string

The current time zone in display format. ex: America/Los_Angeles

Localization.isRTL: boolean

This will return true if the current language is Right-to-Left.

Methods

Callbacks are Android only, changing the native locale on iOS will cause all the apps to reset.

Localization.addListener(listener: Listener): ?Subscription

Observe when a language is added or moved in the Android settings.

Localization.removeAllListeners(): void

Clear all language observers.

Localization.removeSubscription(subscription: Subscription): void

Stop observing when the native languages are edited.

15.0.1

5 days ago

15.0.0

9 days ago

14.8.4

13 days ago

14.8.3

3 months ago

14.8.2

4 months ago

14.8.1

4 months ago

14.8.0

5 months ago

14.6.0

6 months ago

14.7.0

6 months ago

14.5.0

8 months ago

14.3.0

10 months ago

14.4.0

9 months ago

14.2.0

12 months ago

14.1.0

1 year ago

14.1.1

1 year ago

14.0.0

2 years ago

13.1.0

2 years ago

13.0.0

2 years ago

12.0.0

2 years ago

12.0.1

2 years ago

11.0.0

3 years ago

10.2.0

3 years ago

10.1.0

3 years ago

10.0.0

3 years ago

9.1.0

3 years ago

9.0.0

4 years ago

8.2.1

4 years ago

8.2.0

4 years ago

8.1.0

4 years ago

8.0.0

4 years ago

7.0.0

5 years ago

7.0.0-rc.0

5 years ago

6.0.0

5 years ago

6.0.0-rc.0

5 years ago

5.0.1

5 years ago

5.0.0

5 years ago

5.0.0-rc.0

5 years ago

4.0.0

5 years ago

3.0.0

5 years ago

2.0.0

5 years ago

2.0.0-rc.2

5 years ago

2.0.0-rc.1

5 years ago

2.0.0-rc.0

5 years ago

1.0.0

5 years ago

1.0.0-rc.0

6 years ago