mirror of
https://github.com/status-im/react-native-i18n.git
synced 2025-02-07 17:55:59 +00:00
Added Android support
Added java code for bridging current locale in an Android device to javascript. Beware that Android communicates values to javascript through callbacks or events emitted, this means communication is asynchronous.
This commit is contained in:
parent
15261e8ffa
commit
80b421b669
@ -62,7 +62,14 @@ You can get the device's locale with the `RNI18n` native module:
|
||||
```js
|
||||
var deviceLocale = require('react-native').NativeModules.RNI18n.locale
|
||||
```
|
||||
Returns `en_US`.
|
||||
|
||||
Returns `en_US`. This equals to:
|
||||
|
||||
```js
|
||||
var I18n = require('react-native-i18n');
|
||||
var deviceLocale = I18n.locale;
|
||||
```
|
||||
|
||||
|
||||
### I18n.js documentation
|
||||
For more info about I18n.js methods (`localize`, `pluralize`, etc) and settings see [its documentation](https://github.com/fnando/i18n-js#setting-up).
|
||||
|
34
android/build.gradle
Executable file
34
android/build.gradle
Executable file
@ -0,0 +1,34 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:1.1.3'
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'com.android.library'
|
||||
|
||||
android {
|
||||
compileSdkVersion 23
|
||||
buildToolsVersion "23.0.1"
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 16
|
||||
targetSdkVersion 22
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
}
|
||||
lintOptions {
|
||||
abortOnError false
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile 'com.facebook.react:react-native:0.12.+'
|
||||
}
|
4
android/src/main/AndroidManifest.xml
Normal file
4
android/src/main/AndroidManifest.xml
Normal file
@ -0,0 +1,4 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.i18n.reactnativei18n">
|
||||
|
||||
</manifest>
|
@ -0,0 +1,32 @@
|
||||
package com.i18n.reactnativei18n;
|
||||
|
||||
import com.facebook.react.ReactPackage;
|
||||
import com.facebook.react.bridge.JavaScriptModule;
|
||||
import com.facebook.react.bridge.NativeModule;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.uimanager.ViewManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Collections;
|
||||
|
||||
|
||||
public class ReactNativeI18n implements ReactPackage {
|
||||
|
||||
@Override
|
||||
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
|
||||
List<NativeModule> modules = new ArrayList<>();
|
||||
modules.add(new ReactNativeI18nLocale(reactContext));
|
||||
return modules;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Class<? extends JavaScriptModule>> createJSModules() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.i18n.reactnativei18n;
|
||||
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
import com.facebook.react.bridge.Callback;
|
||||
|
||||
|
||||
public class ReactNativeI18nLocale extends ReactContextBaseJavaModule {
|
||||
|
||||
ReactContext reactContext;
|
||||
|
||||
public ReactNativeI18nLocale(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
this.reactContext = reactContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "RNI18n";
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void getCurrentLocale(
|
||||
Callback successCallback
|
||||
) {
|
||||
String current = reactContext.getResources().getConfiguration().locale.toString();
|
||||
successCallback.invoke(current);
|
||||
}
|
||||
}
|
11
index.js
11
index.js
@ -1,8 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
var Platform = require('Platform');
|
||||
var I18n = require('./vendor/i18n');
|
||||
var { RNI18n } = require('react-native').NativeModules;
|
||||
|
||||
I18n.locale = RNI18n.locale.replace(/_/, '-');
|
||||
if (Platform.OS === 'android') {
|
||||
RNI18n.getCurrentLocale( function(locale) {
|
||||
I18n.locale = locale.replace(/_/, '-');
|
||||
});
|
||||
} else {
|
||||
I18n.locale = RNI18n.locale.replace(/_/, '-');
|
||||
}
|
||||
|
||||
module.exports = I18n;
|
||||
module.exports = I18n;
|
||||
|
Loading…
x
Reference in New Issue
Block a user