Improve android language detection

This commit is contained in:
Mathieu Acthernoene 2017-03-30 01:44:54 +02:00
parent 540a61a348
commit 86d0a76eda
1 changed files with 19 additions and 1 deletions

View File

@ -1,5 +1,7 @@
package com.alexanderzaytsev.rn18n;
import android.os.Build;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.module.annotations.ReactModule;
@ -20,7 +22,23 @@ public class RNI18nModule extends ReactContextBaseJavaModule {
}
private String getCurrentLocale() {
return getReactApplicationContext().getResources().getConfiguration().locale.toString();
Locale currentLocale = (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) {
return currentLocale.toLanguageTag();
}
StringBuilder builder = new StringBuilder();
builder.append(currentLocale.getLanguage());
if (currentLocale.getCountry() != null) {
builder.append("-");
builder.append(currentLocale.getCountry());
}
return builder.toString();
}
@Override