Android should return BCP47 tag, not localized string
diff --git a/doc/index.md b/doc/index.md
index 74bf9ee..18afafd 100644
--- a/doc/index.md
+++ b/doc/index.md
@@ -435,8 +435,8 @@
### Description
-Returns the language identifier string to the `successCallback` with a
-`properties` object as a parameter. That object should have a `value`
+Returns the BCP-47 compliant language identifier tag to the `successCallback`
+with a `properties` object as a parameter. That object should have a `value`
property with a `String` value.
If there is an error getting the language, then the `errorCallback`
@@ -453,13 +453,17 @@
### Example
When the browser is set to the `en\_US` locale, this should display a
-popup dialog with the text `language: English`:
+popup dialog with the text `language: en_US`:
navigator.globalization.getPreferredLanguage(
function (language) {alert('language: ' + language.value + '\n');},
function () {alert('Error getting language\n');}
);
+### Android Quirks
+
+- Returns the ISO 639-1 two-letter language code, upper case ISO 3166-1
+country code and variant separated by underscores. Examples: "en", "en_US", "_US"
### Windows Phone 8 Quirks
diff --git a/src/android/Globalization.java b/src/android/Globalization.java
index 588fe4a..612d731 100644
--- a/src/android/Globalization.java
+++ b/src/android/Globalization.java
@@ -148,6 +148,56 @@
throw new GlobalizationError(GlobalizationError.UNKNOWN_ERROR);
}
}
+ /*
+ * @Description: Returns a well-formed ITEF BCP 47 language tag representing this localestring identifier for the client's current locale
+ *
+ * @Return: String: The BCP 47 language tag for the current locale
+ */
+ private String toBcp47Language(Locale loc){
+ final char SEP = '-'; // we will use a dash as per BCP 47
+ String language = loc.getLanguage();
+ String region = loc.getCountry();
+ String variant = loc.getVariant();
+
+ // special case for Norwegian Nynorsk since "NY" cannot be a variant as per BCP 47
+ // this goes before the string matching since "NY" wont pass the variant checks
+ if( language.equals("no") && region.equals("NO") && variant.equals("NY")){
+ language = "nn";
+ region = "NO";
+ variant = "";
+ }
+
+ if( language.isEmpty() || !language.matches("\\p{Alpha}{2,8}")){
+ language = "und"; // Follow the Locale#toLanguageTag() implementation
+ // which says to return "und" for Undetermined
+ }else if(language.equals("iw")){
+ language = "he"; // correct deprecated "Hebrew"
+ }else if(language.equals("in")){
+ language = "id"; // correct deprecated "Indonesian"
+ }else if(language.equals("ji")){
+ language = "yi"; // correct deprecated "Yiddish"
+ }
+
+ // ensure valid country code, if not well formed, it's omitted
+ if (!region.matches("\\p{Alpha}{2}|\\p{Digit}{3}")) {
+ region = "";
+ }
+
+ // variant subtags that begin with a letter must be at least 5 characters long
+ if (!variant.matches("\\p{Alnum}{5,8}|\\p{Digit}\\p{Alnum}{3}")) {
+ variant = "";
+ }
+
+ StringBuilder bcp47Tag = new StringBuilder(language);
+ if (!region.isEmpty()) {
+ bcp47Tag.append(SEP).append(region);
+ }
+ if (!variant.isEmpty()) {
+ bcp47Tag.append(SEP).append(variant);
+ }
+
+ return bcp47Tag.toString();
+ }
/*
* @Description: Returns the string identifier for the client's current language
*
@@ -159,7 +209,7 @@
private JSONObject getPreferredLanguage() throws GlobalizationError {
JSONObject obj = new JSONObject();
try {
- obj.put("value", Locale.getDefault().getDisplayLanguage().toString());
+ obj.put("value", toBcp47Language(Locale.getDefault()));
return obj;
} catch (Exception e) {
throw new GlobalizationError(GlobalizationError.UNKNOWN_ERROR);