mirror of
https://github.com/status-im/react-native-languages.git
synced 2026-08-27 10:01:11 +00:00
06f1eace48285273d49cc2c42cc6181603e48673
🌍 react-native-languages
Get the user preferred languages and use the library of your choice to translate your app !
Requirements
On Android
- Android SDK Build-tools 25.0.3
Installation
$ yarn add react-native-languages
# -- OR --
npm i react-native-languages --save
# - THEN -
$ react-native link react-native-languages
Setup using Cocoapods (iOS only)
Instead of using react-native link, you can use Cocoapods to manage your dependencies.
# Add this line in your Podfile
pod 'ReactNativeLanguages', :path => '../node_modules/react-native-languages'
$ pod install
Manual setup
On iOS
- In the XCode's "Project navigator", right click on your project's Libraries folder ➜
Add Files to <...> - Go to
node_modules➜react-native-languages➜ios➜ selectReactNativeLanguages.xcodeproj - Add
ReactNativeLanguages.atoBuild Phases -> Link Binary With Libraries
On Android
- 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')
- Add the compile line to the dependencies in
android/app/build.gradle:
dependencies {
// ...
compile project(':react-native-languages')
}
- 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 Languages from 'react-native-languages';
// Current device language
console.log('language', Languages.language);
// User preferred languages (in order)
console.log('languages', Languages.languages);
// Listening for languages changes (on Android)
Languages.addEventListener('change', ({ language, languages }) => {
// Do languages related things…
// Languages.language and Languages.languages will be correct too !
});
Add project's supported localizations (iOS)
Description
🌍 Get the user preferred languages and use the library of your choice to translate your app
759 KiB
Languages
Java
34.7%
JavaScript
20.7%
Objective-C
17.2%
C++
10.9%
Starlark
10.1%
Other
6.4%
