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,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 {

View File

@ -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<String, Object> getConstants() {
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;
}
}

View File

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