Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for xiaomi miui #217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
*
Expand All @@ -45,7 +90,7 @@ private String getCurrentLanguage() {
}

Locale current = getReactApplicationContext().getResources().getConfiguration().locale;
return current.getLanguage() + "-" + current.getCountry();
return current.getLanguage() + "-" + getRegionCode(current);
}


Expand Down Expand Up @@ -87,4 +132,4 @@ private SharedPreferences getPreferences() {
private SharedPreferences.Editor getEditor() {
return getPreferences().edit();
}
}
}