Handle preferred languages on android

This commit is contained in:
Mathieu Acthernoene 2017-04-06 20:07:16 +02:00
parent f12c039271
commit 0a9afe87c2
3 changed files with 42 additions and 32 deletions

View File

@ -1,35 +1,20 @@
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
}
}
apply plugin: 'com.android.library' apply plugin: 'com.android.library'
android { android {
compileSdkVersion 23 compileSdkVersion 25
buildToolsVersion "23.0.1" buildToolsVersion "25.0.2"
defaultConfig { defaultConfig {
minSdkVersion 16 minSdkVersion 16
targetSdkVersion 22 targetSdkVersion 22
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"
ndk {
abiFilters "armeabi-v7a", "x86"
} }
lintOptions {
abortOnError false
} }
} }
repositories {
mavenCentral()
}
dependencies { dependencies {
compile 'com.facebook.react:react-native:+' compile 'com.facebook.react:react-native:+'
} }

View File

@ -1,15 +1,17 @@
package com.alexanderzaytsev.rn18n; package com.alexanderzaytsev.rn18n;
import android.os.Build; 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.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.module.annotations.ReactModule;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale;
import java.util.Map; import java.util.Map;
@ReactModule(name = "RNI18n")
public class RNI18nModule extends ReactContextBaseJavaModule { public class RNI18nModule extends ReactContextBaseJavaModule {
public RNI18nModule(ReactApplicationContext reactContext) { public RNI18nModule(ReactApplicationContext reactContext) {
@ -21,21 +23,27 @@ public class RNI18nModule extends ReactContextBaseJavaModule {
return "RNI18n"; return "RNI18n";
} }
private String getCurrentLocale() { private boolean isSupportingPreferredLanguages() {
Locale currentLocale = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
? getReactApplicationContext().getResources().getConfiguration().getLocales().get(0) }
: getReactApplicationContext().getResources().getConfiguration().locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { private LocaleList getLocales() {
return currentLocale.toLanguageTag(); 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(); StringBuilder builder = new StringBuilder();
builder.append(currentLocale.getLanguage()); builder.append(locale.getLanguage());
if (currentLocale.getCountry() != null) { if (locale.getCountry() != null) {
builder.append("-"); builder.append("-");
builder.append(currentLocale.getCountry()); builder.append(locale.getCountry());
} }
return builder.toString(); return builder.toString();
@ -44,7 +52,16 @@ public class RNI18nModule extends ReactContextBaseJavaModule {
@Override @Override
public Map<String, Object> getConstants() { public Map<String, Object> getConstants() {
HashMap<String,Object> constants = new HashMap<String,Object>(); HashMap<String,Object> constants = new HashMap<String,Object>();
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; return constants;
} }
} }

View File

@ -0,0 +1,8 @@
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
</resources>