Switch to getLanguages Promise on android

This commit is contained in:
Mathieu Acthernoene 2017-05-18 10:34:53 +02:00
parent 958ae660e6
commit 0ca088ec8f
5 changed files with 64 additions and 70 deletions

View File

@ -1,3 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alexanderzaytsev.rn18n">
package="com.AlexanderZaytsev.RNI18n">
</manifest>

View File

@ -0,0 +1,60 @@
package com.AlexanderZaytsev.RNI18n;
import android.os.Build;
import android.os.LocaleList;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableArray;
import java.util.Locale;
public class RNI18nModule extends ReactContextBaseJavaModule {
public RNI18nModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "RNI18n";
}
private String toLanguageTag(Locale locale) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return locale.toLanguageTag();
}
StringBuilder builder = new StringBuilder();
builder.append(locale.getLanguage());
if (locale.getCountry() != null) {
builder.append("-");
builder.append(locale.getCountry());
}
return builder.toString();
}
@ReactMethod
public void getLanguages(Promise promise) {
try {
LocaleList locales = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
? getReactApplicationContext().getResources().getConfiguration().getLocales()
: new LocaleList(getReactApplicationContext().getResources().getConfiguration().locale);
WritableArray languages = Arguments.createArray();
for (int i = 0; i < locales.size(); i++) {
languages.pushString(this.toLanguageTag(locales.get(i)));
}
promise.resolve(languages);
} catch (Exception e) {
promise.reject(e);
}
}
}

View File

@ -1,4 +1,4 @@
package com.alexanderzaytsev.rn18n;
package com.AlexanderZaytsev.RNI18n;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;

View File

@ -1,67 +0,0 @@
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 java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class RNI18nModule extends ReactContextBaseJavaModule {
public RNI18nModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "RNI18n";
}
private boolean isSupportingPreferredLanguages() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
}
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(locale.getLanguage());
if (locale.getCountry() != null) {
builder.append("-");
builder.append(locale.getCountry());
}
return builder.toString();
}
@Override
public Map<String, Object> getConstants() {
HashMap<String,Object> constants = new HashMap<String,Object>();
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

@ -7,7 +7,8 @@ import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import com.alexanderzaytsev.rn18n.RNI18nPackage;
import com.AlexanderZaytsev.RNI18n.RNI18nPackage;
import java.util.Arrays;
import java.util.List;