Repository: cordova-plugin-globalization Updated Branches: refs/heads/CB-4602 [created] 69562f4d7
Android should return BCP47 tag, not localized string Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/commit/69562f4d Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/tree/69562f4d Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/diff/69562f4d Branch: refs/heads/CB-4602 Commit: 69562f4d726f2ddcca4d82c6cb6a4b02402e2edd Parents: e15742a Author: mbillau <[email protected]> Authored: Mon Apr 28 15:02:39 2014 -0400 Committer: mbillau <[email protected]> Committed: Tue Apr 29 12:59:31 2014 -0400 ---------------------------------------------------------------------- doc/index.md | 10 ++++--- src/android/Globalization.java | 52 ++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/blob/69562f4d/doc/index.md ---------------------------------------------------------------------- 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 @@ Get the string identifier for the client's current language. ### 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 @@ error's expected code is `GlobalizationError.UNKNOWN\_ERROR`. ### 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 http://git-wip-us.apache.org/repos/asf/cordova-plugin-globalization/blob/69562f4d/src/android/Globalization.java ---------------------------------------------------------------------- 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 @@ public class Globalization extends CordovaPlugin { 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 @@ public class Globalization extends CordovaPlugin { 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);
