diff --git a/android/build.gradle b/android/build.gradle index 8651cbd..40165ac 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,33 +1,18 @@ -buildscript { - repositories { - jcenter() - } - - dependencies { - classpath 'com.android.tools.build:gradle:2.2.3' - } -} - apply plugin: 'com.android.library' android { - compileSdkVersion 23 - buildToolsVersion "23.0.1" + compileSdkVersion 25 + buildToolsVersion "25.0.2" defaultConfig { minSdkVersion 16 targetSdkVersion 22 versionCode 1 versionName "1.0" + ndk { + abiFilters "armeabi-v7a", "x86" + } } - - lintOptions { - abortOnError false - } -} - -repositories { - mavenCentral() } dependencies { diff --git a/android/src/main/java/com/alexanderzaytsev/rn18n/RNI18nModule.java b/android/src/main/java/com/alexanderzaytsev/rn18n/RNI18nModule.java index fca8b50..cb4e7de 100644 --- a/android/src/main/java/com/alexanderzaytsev/rn18n/RNI18nModule.java +++ b/android/src/main/java/com/alexanderzaytsev/rn18n/RNI18nModule.java @@ -1,15 +1,17 @@ package com.alexanderzaytsev.rn18n; import android.os.Build; +import android.os.LocaleList; +import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.WritableArray; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; -import com.facebook.react.module.annotations.ReactModule; import java.util.HashMap; +import java.util.Locale; import java.util.Map; -@ReactModule(name = "RNI18n") public class RNI18nModule extends ReactContextBaseJavaModule { public RNI18nModule(ReactApplicationContext reactContext) { @@ -21,21 +23,27 @@ public class RNI18nModule extends ReactContextBaseJavaModule { return "RNI18n"; } - private String getCurrentLocale() { - Locale currentLocale = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) - ? getReactApplicationContext().getResources().getConfiguration().getLocales().get(0) - : getReactApplicationContext().getResources().getConfiguration().locale; + private boolean isSupportingPreferredLanguages() { + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N; + } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - return currentLocale.toLanguageTag(); + private LocaleList getLocales() { + return this.isSupportingPreferredLanguages() + ? getReactApplicationContext().getResources().getConfiguration().getLocales() + : new LocaleList(getReactApplicationContext().getResources().getConfiguration().locale); + } + + private String toLanguageTag(Locale locale) { + if (this.isSupportingPreferredLanguages()) { + return locale.toLanguageTag(); } StringBuilder builder = new StringBuilder(); - builder.append(currentLocale.getLanguage()); + builder.append(locale.getLanguage()); - if (currentLocale.getCountry() != null) { + if (locale.getCountry() != null) { builder.append("-"); - builder.append(currentLocale.getCountry()); + builder.append(locale.getCountry()); } return builder.toString(); @@ -44,7 +52,16 @@ public class RNI18nModule extends ReactContextBaseJavaModule { @Override public Map getConstants() { HashMap constants = new HashMap(); - constants.put("locale", this.getCurrentLocale()); + LocaleList locales = this.getLocales(); + WritableArray languages = Arguments.createArray(); + + for (int i = 0; i < locales.size(); i++) { + languages.pushString(this.toLanguageTag(locales.get(i))); + } + + constants.put("language", languages.getString(0)); + constants.put("languages", languages); + return constants; } } diff --git a/android/src/main/res/values/styles.xml b/android/src/main/res/values/styles.xml new file mode 100644 index 0000000..319eb0c --- /dev/null +++ b/android/src/main/res/values/styles.xml @@ -0,0 +1,8 @@ + + + + + +