From 80b421b6696c597754ad8d0c3cb42c844c5e5a1c Mon Sep 17 00:00:00 2001 From: Miguel Araujo Perez Date: Thu, 22 Oct 2015 17:35:22 +0200 Subject: [PATCH] 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. --- README.md | 9 ++++- android/build.gradle | 34 +++++++++++++++++++ android/src/main/AndroidManifest.xml | 4 +++ .../i18n/reactnativei18n/ReactNativeI18n.java | 32 +++++++++++++++++ .../ReactNativeI18nLocale.java | 31 +++++++++++++++++ index.js | 11 ++++-- 6 files changed, 118 insertions(+), 3 deletions(-) create mode 100755 android/build.gradle create mode 100644 android/src/main/AndroidManifest.xml create mode 100644 android/src/main/java/com/i18n/reactnativei18n/ReactNativeI18n.java create mode 100644 android/src/main/java/com/i18n/reactnativei18n/ReactNativeI18nLocale.java diff --git a/README.md b/README.md index 26b25a1..d4056c6 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/android/build.gradle b/android/build.gradle new file mode 100755 index 0000000..1f9f4e0 --- /dev/null +++ b/android/build.gradle @@ -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.+' +} \ No newline at end of file diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml new file mode 100644 index 0000000..17a0dbd --- /dev/null +++ b/android/src/main/AndroidManifest.xml @@ -0,0 +1,4 @@ + + + diff --git a/android/src/main/java/com/i18n/reactnativei18n/ReactNativeI18n.java b/android/src/main/java/com/i18n/reactnativei18n/ReactNativeI18n.java new file mode 100644 index 0000000..2a1b608 --- /dev/null +++ b/android/src/main/java/com/i18n/reactnativei18n/ReactNativeI18n.java @@ -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 createNativeModules(ReactApplicationContext reactContext) { + List modules = new ArrayList<>(); + modules.add(new ReactNativeI18nLocale(reactContext)); + return modules; + } + + @Override + public List> createJSModules() { + return Collections.emptyList(); + } + + @Override + public List createViewManagers(ReactApplicationContext reactContext) { + return Collections.emptyList(); + } +} diff --git a/android/src/main/java/com/i18n/reactnativei18n/ReactNativeI18nLocale.java b/android/src/main/java/com/i18n/reactnativei18n/ReactNativeI18nLocale.java new file mode 100644 index 0000000..bb2daa8 --- /dev/null +++ b/android/src/main/java/com/i18n/reactnativei18n/ReactNativeI18nLocale.java @@ -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); + } +} diff --git a/index.js b/index.js index 30db660..36c0217 100644 --- a/index.js +++ b/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; \ No newline at end of file +module.exports = I18n;