From 86d0a76edae3196398aa0258819c7d8a24ff4834 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Thu, 30 Mar 2017 01:44:54 +0200 Subject: [PATCH] Improve android language detection --- .../alexanderzaytsev/rn18n/RNI18nModule.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/alexanderzaytsev/rn18n/RNI18nModule.java b/android/src/main/java/com/alexanderzaytsev/rn18n/RNI18nModule.java index eec3070..fca8b50 100644 --- a/android/src/main/java/com/alexanderzaytsev/rn18n/RNI18nModule.java +++ b/android/src/main/java/com/alexanderzaytsev/rn18n/RNI18nModule.java @@ -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