🌍 Get the user preferred languages and use the library of your choice to translate your app
Go to file
Mathieu Acthernoene 1f2b3d8d29 Use .prettierignore / .prettierrc files 2017-09-12 01:13:17 +02:00
android Initial release 2017-08-01 18:44:14 +02:00
docs Initial release 2017-08-01 18:44:14 +02:00
example Update example RN version to 0.48.2 2017-09-12 01:06:47 +02:00
ios Initial release 2017-08-01 18:44:14 +02:00
.editorconfig Initial release 2017-08-01 18:44:14 +02:00
.flowconfig Update project dependencies 2017-09-12 01:07:04 +02:00
.gitignore Initial release 2017-08-01 18:44:14 +02:00
.npmignore Initial release 2017-08-01 18:44:14 +02:00
.prettierignore Use .prettierignore / .prettierrc files 2017-09-12 01:13:17 +02:00
.prettierrc Use .prettierignore / .prettierrc files 2017-09-12 01:13:17 +02:00
README.md Fix some typos 2017-08-01 19:06:54 +02:00
ReactNativeLanguages.podspec Add react dependency in podspec 2017-09-12 00:54:30 +02:00
index.js Initial release 2017-08-01 18:44:14 +02:00
package-lock.json Bump version number 2017-08-01 19:07:29 +02:00
package.json Use .prettierignore / .prettierrc files 2017-09-12 01:13:17 +02:00
yarn.lock Initial release 2017-08-01 18:44:14 +02:00

README.md

🌐 react-native-languages

React Native properties and methods related to the language of the device.

npm version npm Platform - Android and iOS MIT styled with prettier

Requirements

On Android

  • Android SDK Build-tools 25.0.3

Installation

Using yarn

$ yarn add react-native-languages

Using npm

$ npm i react-native-languages --save

Setup

Automatic setup

$ react-native link react-native-languages

Using Cocoapods (iOS only)

# Add this line in your Podfile
pod 'ReactNativeLanguages', :path => '../node_modules/react-native-languages'
$ pod install

Manual setup

On iOS

  1. In the XCode's "Project navigator", right click on your project's Libraries folder ➜ Add Files to <...>
  2. Go to node_modulesreact-native-languagesios ➜ select ReactNativeLanguages.xcodeproj
  3. Add ReactNativeLanguages.a to Build Phases -> Link Binary With Libraries

On Android

  1. Add the following lines to android/settings.gradle:
include ':react-native-languages'
project(':react-native-languages').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-languages/android')
  1. Add the compile line to the dependencies in android/app/build.gradle:
dependencies {
  // ...
  compile project(':react-native-languages')
}
  1. Add the import and link the package in MainApplication.java:
import com.reactcommunity.reactnativelanguages.ReactNativeLanguagesPackage; // <-- Add the ReactNativeLanguages import

public class MainApplication extends Application implements ReactApplication {

  // ...

  @Override
  protected List<ReactPackage> getPackages() {
    return Arrays.<ReactPackage>asList(
      new MainReactPackage(),
      // ...
      new ReactNativeLanguagesPackage() // <-- Add it to the packages list
    );
  }

  // ...
}

Usage

import ReactNativeLanguages from 'react-native-languages';

// Current device language
console.log('language', ReactNativeLanguages.language);

// User preferred languages (in order)
console.log('languages', ReactNativeLanguages.languages);

Add project's supported localizations (iOS)

Listening for languages changes (Android)

import ReactNativeLanguages from 'react-native-languages';

ReactNativeLanguages.addEventListener('change', ({ language, languages }) => {
  // Do languages related things…
  // ReactNativeLanguages.language and ReactNativeLanguages.languages will be correct too !
});

⚠️ Note

As iOS reloads your application on languages change, there is no need to perform this step on this platform.