diff --git a/android/src/main/java/com/babisoft/ReactNativeLocalization/ReactNativeLocalization.java b/android/src/main/java/com/babisoft/ReactNativeLocalization/ReactNativeLocalization.java index f628a71..81af5bd 100644 --- a/android/src/main/java/com/babisoft/ReactNativeLocalization/ReactNativeLocalization.java +++ b/android/src/main/java/com/babisoft/ReactNativeLocalization/ReactNativeLocalization.java @@ -1,16 +1,26 @@ package com.babisoft.ReactNativeLocalization; + +import android.annotation.SuppressLint; + import android.content.Context; import android.content.SharedPreferences; +import android.text.TextUtils; + +import androidx.annotation.NonNull; + import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; +import java.lang.reflect.Method; + import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.Objects; /** * Created by stefano on 20/09/15. @@ -31,6 +41,41 @@ public String getName() { return "ReactLocalization"; } + private @NonNull String getSystemProperty(String key) { + try { + @SuppressLint("PrivateApi") + Class systemProperties = Class.forName("android.os.SystemProperties"); + Method get = systemProperties.getMethod("get", String.class); + return (String) Objects.requireNonNull(get.invoke(systemProperties, key)); + } catch (Exception ignored) { + return ""; + } + } + + private @NonNull String getCountryCode(@NonNull Locale locale) { + try { + String country = locale.getCountry(); + + if (country.equals("419")) { + return "UN"; + } + + return TextUtils.isEmpty(country) ? "" : country; + } catch (Exception ignored) { + return ""; + } + } + + private @NonNull String getRegionCode(@NonNull Locale locale) { + String miuiRegion = getSystemProperty("ro.miui.region"); + + if (!TextUtils.isEmpty(miuiRegion)) { + return miuiRegion; + } + + return getCountryCode(locale); + } + /** * Return the current language * @@ -45,7 +90,7 @@ private String getCurrentLanguage() { } Locale current = getReactApplicationContext().getResources().getConfiguration().locale; - return current.getLanguage() + "-" + current.getCountry(); + return current.getLanguage() + "-" + getRegionCode(current); } @@ -87,4 +132,4 @@ private SharedPreferences getPreferences() { private SharedPreferences.Editor getEditor() { return getPreferences().edit(); } -} +} \ No newline at end of file