Bug 1344143 - Backing out changeset e07ec12de6c0 (Bug 1200494). r?mak draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Sun, 05 Mar 2017 12:43:42 +0100
changeset 498460 2518d2b635249c13d8d8090410b1988cd5c2ab2d
parent 498459 f27891ffc1a05e1e8c02e3cfcaec86d6e9a35f06
child 549155 0304328775532e331e25b24147dd60392778c6af
push id49190
push usermozilla@buttercookie.de
push dateTue, 14 Mar 2017 19:36:53 +0000
reviewersmak
bugs1344143, 1200494
milestone55.0a1
Bug 1344143 - Backing out changeset e07ec12de6c0 (Bug 1200494). r?mak The Intl API is not being immediately enabled on Android Beta/Release after all, so we have to keep these fallbacks for the time being. MozReview-Commit-ID: 1uWfTy58x3I
toolkit/mozapps/downloads/DownloadUtils.jsm
toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
--- a/toolkit/mozapps/downloads/DownloadUtils.jsm
+++ b/toolkit/mozapps/downloads/DownloadUtils.jsm
@@ -41,16 +41,21 @@ const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "PluralForm",
                                   "resource://gre/modules/PluralForm.jsm");
 
+this.__defineGetter__("gDecimalSymbol", function() {
+    delete this.gDecimalSymbol;
+      return this.gDecimalSymbol = Number(5.4).toLocaleString().match(/\D/);
+});
+
 var localeNumberFormatCache = new Map();
 function getLocaleNumberFormat(fractionDigits) {
   // Backward compatibility: don't use localized digits
   let locale = Intl.NumberFormat().resolvedOptions().locale +
                "-u-nu-latn";
   let key = locale + "_" + fractionDigits;
   if (!localeNumberFormatCache.has(key)) {
     localeNumberFormatCache.set(key,
@@ -339,38 +344,45 @@ this.DownloadUtils = {
 
     let dts = Cc["@mozilla.org/intl/scriptabledateformat;1"]
               .getService(Ci.nsIScriptableDateFormat);
 
     // Figure out when today begins
     let today = new Date(aNow.getFullYear(), aNow.getMonth(), aNow.getDate());
 
     // Get locale to use for date/time formatting
-    const locale = Cc["@mozilla.org/chrome/chrome-registry;1"]
-                     .getService(Ci.nsIXULChromeRegistry)
-                     .getSelectedLocale("global", true);
+    // TODO: Remove Intl fallback when no longer needed (bug 1344543).
+    const locale = typeof Intl === "undefined"
+                   ? undefined
+                   : Cc["@mozilla.org/chrome/chrome-registry;1"]
+                       .getService(Ci.nsIXULChromeRegistry)
+                       .getSelectedLocale("global", true);
 
     // Figure out if the time is from today, yesterday, this week, etc.
     let dateTimeCompact;
     if (aDate >= today) {
       // After today started, show the time
       dateTimeCompact = dts.FormatTime("",
                                        dts.timeFormatNoSeconds,
                                        aDate.getHours(),
                                        aDate.getMinutes(),
                                        0);
     } else if (today - aDate < (24 * 60 * 60 * 1000)) {
       // After yesterday started, show yesterday
       dateTimeCompact = gBundle.GetStringFromName(gStr.yesterday);
     } else if (today - aDate < (6 * 24 * 60 * 60 * 1000)) {
       // After last week started, show day of week
-      dateTimeCompact = aDate.toLocaleDateString(locale, { weekday: "long" });
+      dateTimeCompact = typeof Intl === "undefined"
+                        ? aDate.toLocaleFormat("%A")
+                        : aDate.toLocaleDateString(locale, { weekday: "long" });
     } else {
       // Show month/day
-      let month = aDate.toLocaleDateString(locale, { month: "long" });
+      let month = typeof Intl === "undefined"
+                  ? aDate.toLocaleFormat("%B")
+                  : aDate.toLocaleDateString(locale, { month: "long" });
       let date = aDate.getDate();
       dateTimeCompact = gBundle.formatStringFromName(gStr.monthDate, [month, date], 2);
     }
 
     let dateTimeFull = dts.FormatDateTime("",
                                           dts.dateFormatLong,
                                           dts.timeFormatNoSeconds,
                                           aDate.getFullYear(),
@@ -472,18 +484,25 @@ this.DownloadUtils = {
     // Get rid of insignificant bits by truncating to 1 or 0 decimal points
     // 0 -> 0; 1.2 -> 1.2; 12.3 -> 12.3; 123.4 -> 123; 234.5 -> 235
     // added in bug 462064: (unitIndex != 0) makes sure that no decimal digit for bytes appears when aBytes < 100
     let fractionDigits = (aBytes > 0) && (aBytes < 100) && (unitIndex != 0) ? 1 : 0;
 
     // Don't try to format Infinity values using NumberFormat.
     if (aBytes === Infinity) {
       aBytes = "Infinity";
+    } else if (typeof Intl != "undefined") {
+      aBytes = getLocaleNumberFormat(fractionDigits)
+                 .format(aBytes);
     } else {
-      aBytes = getLocaleNumberFormat(fractionDigits).format(aBytes);
+      // FIXME: Fall back to the old hack, will be fixed in bug 1344543.
+      aBytes = aBytes.toFixed(fractionDigits);
+      if (gDecimalSymbol != ".") {
+        aBytes = aBytes.replace(".", gDecimalSymbol);
+      }
     }
 
     return [aBytes, gBundle.GetStringFromName(gStr.units[unitIndex])];
   },
 
   /**
    * Converts a number of seconds to the two largest units. Time values are
    * whole numbers, and units have the correct plural/singular form.
--- a/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
+++ b/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
@@ -73,35 +73,44 @@ function testAllGetReadableDates() {
   const today_11_30     = new Date(2000, 11, 31, 11, 30, 15);
   const today_12_30     = new Date(2000, 11, 31, 12, 30, 15);
   const yesterday_11_30 = new Date(2000, 11, 30, 11, 30, 15);
   const yesterday_12_30 = new Date(2000, 11, 30, 12, 30, 15);
   const twodaysago      = new Date(2000, 11, 29, 11, 30, 15);
   const sixdaysago      = new Date(2000, 11, 25, 11, 30, 15);
   const sevendaysago    = new Date(2000, 11, 24, 11, 30, 15);
 
-  const locale = Components.classes["@mozilla.org/chrome/chrome-registry;1"]
-                                   .getService(Components.interfaces.nsIXULChromeRegistry)
-                                   .getSelectedLocale("global", true);
+  // TODO: Remove Intl fallback when no longer needed (bug 1344543).
+  const locale = typeof Intl === "undefined"
+                 ? undefined
+                 : Components.classes["@mozilla.org/chrome/chrome-registry;1"]
+                                     .getService(Components.interfaces.nsIXULChromeRegistry)
+                                     .getSelectedLocale("global", true);
 
   let dts = Components.classes["@mozilla.org/intl/scriptabledateformat;1"].
             getService(Components.interfaces.nsIScriptableDateFormat);
 
   testGetReadableDates(today_11_30, dts.FormatTime("", dts.timeFormatNoSeconds,
                                                    11, 30, 0));
   testGetReadableDates(today_12_30, dts.FormatTime("", dts.timeFormatNoSeconds,
                                                    12, 30, 0));
   testGetReadableDates(yesterday_11_30, "Yesterday");
   testGetReadableDates(yesterday_12_30, "Yesterday");
   testGetReadableDates(twodaysago,
-                       twodaysago.toLocaleDateString(locale, { weekday: "long" }));
+                       typeof Intl === "undefined"
+                       ? twodaysago.toLocaleFormat("%A")
+                       : twodaysago.toLocaleDateString(locale, { weekday: "long" }));
   testGetReadableDates(sixdaysago,
-                       sixdaysago.toLocaleDateString(locale, { weekday: "long" }));
+                       typeof Intl === "undefined"
+                       ? sixdaysago.toLocaleFormat("%A")
+                       : sixdaysago.toLocaleDateString(locale, { weekday: "long" }));
   testGetReadableDates(sevendaysago,
-                       sevendaysago.toLocaleDateString(locale, { month: "long" }) + " " +
+                       (typeof Intl === "undefined"
+                        ? sevendaysago.toLocaleFormat("%B")
+                        : sevendaysago.toLocaleDateString(locale, { month: "long" })) + " " +
                        sevendaysago.getDate().toString().padStart(2, "0"));
 
   let [, dateTimeFull] = DownloadUtils.getReadableDates(today_11_30);
   do_check_eq(dateTimeFull, dts.FormatDateTime("", dts.dateFormatLong,
                                                    dts.timeFormatNoSeconds,
                                                    2000, 12, 31, 11, 30, 0));
 }