Bug 1449505 - Migrate FormAutofill to use mozIntl.getLocaleDisplayNames. r?mattn draft
authorZibi Braniecki <zbraniecki@mozilla.com>
Sun, 01 Apr 2018 15:18:25 +0200
changeset 780086 d0344e68d1ca83f103ef02c958ff0077f2b3e875
parent 780085 bbad963421986a36a24b1eda540a8f2d5510ab22
child 780488 d868462f88df392181481c801e2d9d3c8b495489
push id105961
push userbmo:gandalf@aviary.pl
push dateWed, 11 Apr 2018 07:08:45 +0000
reviewersmattn
bugs1449505
milestone61.0a1
Bug 1449505 - Migrate FormAutofill to use mozIntl.getLocaleDisplayNames. r?mattn MozReview-Commit-ID: FuQnIB2Tp5I
browser/extensions/formautofill/FormAutofillStorage.jsm
browser/extensions/formautofill/FormAutofillUtils.jsm
--- a/browser/extensions/formautofill/FormAutofillStorage.jsm
+++ b/browser/extensions/formautofill/FormAutofillStorage.jsm
@@ -137,26 +137,16 @@ ChromeUtils.defineModuleGetter(this, "Ma
                                "resource://formautofill/MasterPassword.jsm");
 ChromeUtils.defineModuleGetter(this, "PhoneNumber",
                                "resource://formautofill/phonenumberutils/PhoneNumber.jsm");
 
 XPCOMUtils.defineLazyServiceGetter(this, "gUUIDGenerator",
                                    "@mozilla.org/uuid-generator;1",
                                    "nsIUUIDGenerator");
 
-XPCOMUtils.defineLazyGetter(this, "REGION_NAMES", function() {
-  let regionNames = {};
-  let countries = Services.strings.createBundle("chrome://global/locale/regionNames.properties").getSimpleEnumeration();
-  while (countries.hasMoreElements()) {
-    let country = countries.getNext().QueryInterface(Ci.nsIPropertyElement);
-    regionNames[country.key.toUpperCase()] = country.value;
-  }
-  return regionNames;
-});
-
 const CryptoHash = Components.Constructor("@mozilla.org/security/hash;1",
                                           "nsICryptoHash", "initWithString");
 
 const PROFILE_JSON_FILE_NAME = "autofill-profiles.json";
 
 const STORAGE_SCHEMA_VERSION = 1;
 const ADDRESS_SCHEMA_VERSION = 1;
 const CREDIT_CARD_SCHEMA_VERSION = 1;
@@ -1267,18 +1257,22 @@ class Addresses extends AutofillRecords 
           streetAddress.splice(2)
         );
       }
       hasNewComputedFields = true;
     }
 
     // Compute country name
     if (!("country-name" in address)) {
-      if (address.country && REGION_NAMES[address.country]) {
-        address["country-name"] = REGION_NAMES[address.country];
+      if (address.country) {
+        try {
+          address["country-name"] = Services.intl.getRegionDisplayNames(undefined, [address.country]);
+        } catch (e) {
+          address["country-name"] = "";
+        }
       } else {
         address["country-name"] = "";
       }
       hasNewComputedFields = true;
     }
 
     // Compute tel
     if (!("tel-national" in address)) {
@@ -1365,17 +1359,23 @@ class Addresses extends AutofillRecords 
 
     if (address.country) {
       country = address.country.toUpperCase();
     } else if (address["country-name"]) {
       country = FormAutofillUtils.identifyCountryCode(address["country-name"]);
     }
 
     // Only values included in the region list will be saved.
-    if (country && REGION_NAMES[country]) {
+    let hasLocalizedName = false;
+    try {
+      let localizedName = Services.intl.getRegionDisplayNames(undefined, [country]);
+      hasLocalizedName = localizedName != country;
+    } catch (e) {}
+
+    if (country && hasLocalizedName) {
       address.country = country;
     } else {
       delete address.country;
     }
 
     delete address["country-name"];
   }
 
--- a/browser/extensions/formautofill/FormAutofillUtils.jsm
+++ b/browser/extensions/formautofill/FormAutofillUtils.jsm
@@ -828,32 +828,32 @@ this.FormAutofillUtils = {
   },
 
   /**
    * Localize "data-localization" or "data-localization-region" attributes.
    * @param {Element} element
    * @param {string} attributeName
    */
   localizeAttributeForElement(element, attributeName) {
-    let bundle = null;
     switch (attributeName) {
       case "data-localization": {
-        bundle = this.stringBundle;
+        element.textContent =
+          this.stringBundle.GetStringFromName(element.getAttribute(attributeName));
+        element.removeAttribute(attributeName);
         break;
       }
       case "data-localization-region": {
-        bundle = this.regionsBundle;
-        break;
+        let regionCode = element.getAttribute(attributeName);
+        element.textContent = Services.intl.getRegionDisplayNames(undefined, [regionCode]);
+        element.removeAttribute(attributeName);
+        return;
       }
       default:
         throw new Error("Unexpected attributeName");
     }
-
-    element.textContent = bundle.GetStringFromName(element.getAttribute(attributeName));
-    element.removeAttribute(attributeName);
   },
 
   /**
    * Localize elements with "data-localization" or "data-localization-region" attributes.
    * @param {Element} root
    */
   localizeMarkup(root) {
     let elements = root.querySelectorAll("[data-localization]");
@@ -870,20 +870,16 @@ this.FormAutofillUtils = {
 
 this.log = null;
 this.FormAutofillUtils.defineLazyLogGetter(this, EXPORTED_SYMBOLS[0]);
 
 XPCOMUtils.defineLazyGetter(FormAutofillUtils, "stringBundle", function() {
   return Services.strings.createBundle("chrome://formautofill/locale/formautofill.properties");
 });
 
-XPCOMUtils.defineLazyGetter(FormAutofillUtils, "regionsBundle", function() {
-  return Services.strings.createBundle("chrome://global/locale/regionNames.properties");
-});
-
 XPCOMUtils.defineLazyGetter(FormAutofillUtils, "brandBundle", function() {
   return Services.strings.createBundle("chrome://branding/locale/brand.properties");
 });
 
 XPCOMUtils.defineLazyPreferenceGetter(this.FormAutofillUtils,
                                       "DEFAULT_REGION", DEFAULT_REGION_PREF, "US");
 XPCOMUtils.defineLazyPreferenceGetter(this.FormAutofillUtils,
                                       "isAutofillAddressesEnabled", ENABLED_AUTOFILL_ADDRESSES_PREF);