Commit ee681b2c authored by Rebecca Hughes's avatar Rebecca Hughes

Merge pull request #15 from Bhullnatik/master

Use getLanguageTag to retrieve device locale
parents c89872de cb06d2ba
package com.learnium.RNDeviceInfo; package com.learnium.RNDeviceInfo;
import javax.annotation.Nullable; import android.bluetooth.BluetoothAdapter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings.Secure;
import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.Callback;
import android.os.Build;
import android.provider.Settings.Secure;
import android.content.pm.PackageManager;
import android.content.pm.PackageInfo;
import android.bluetooth.BluetoothAdapter;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import javax.annotation.Nullable;
public class RNDeviceModule extends ReactContextBaseJavaModule { public class RNDeviceModule extends ReactContextBaseJavaModule {
ReactApplicationContext reactContext; ReactApplicationContext reactContext;
...@@ -32,6 +29,21 @@ public class RNDeviceModule extends ReactContextBaseJavaModule { ...@@ -32,6 +29,21 @@ public class RNDeviceModule extends ReactContextBaseJavaModule {
return "RNDeviceInfo"; return "RNDeviceInfo";
} }
private String getCurrentLanguage() {
Locale current = getReactApplicationContext().getResources().getConfiguration().locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return current.toLanguageTag();
} else {
StringBuilder builder = new StringBuilder();
builder.append(current.getLanguage());
if (current.getCountry() != null) {
builder.append("-");
builder.append(current.getCountry());
}
return builder.toString();
}
}
@Override @Override
public @Nullable Map<String, Object> getConstants() { public @Nullable Map<String, Object> getConstants() {
HashMap<String, Object> constants = new HashMap<String, Object>(); HashMap<String, Object> constants = new HashMap<String, Object>();
...@@ -64,7 +76,7 @@ public class RNDeviceModule extends ReactContextBaseJavaModule { ...@@ -64,7 +76,7 @@ public class RNDeviceModule extends ReactContextBaseJavaModule {
constants.put("systemVersion", Build.VERSION.RELEASE); constants.put("systemVersion", Build.VERSION.RELEASE);
constants.put("model", Build.MODEL); constants.put("model", Build.MODEL);
constants.put("deviceId", Build.BOARD); constants.put("deviceId", Build.BOARD);
constants.put("deviceLocale", this.reactContext.getResources().getConfiguration().locale.toString()); constants.put("deviceLocale", this.getCurrentLanguage());
constants.put("uniqueId", Secure.getString(this.reactContext.getContentResolver(), Secure.ANDROID_ID)); constants.put("uniqueId", Secure.getString(this.reactContext.getContentResolver(), Secure.ANDROID_ID));
constants.put("systemManufacturer", Build.MANUFACTURER); constants.put("systemManufacturer", Build.MANUFACTURER);
constants.put("bundleId", packageName); constants.put("bundleId", packageName);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment