Bug 1009795 - Use toLocalString to format download size instead of the decimalSymbol hook; r=mak
authorEhsan Akhgari <ehsan@mozilla.com>
Sun, 23 Aug 2015 20:17:22 -0400
changeset 259645 cb7008d633835fa982492646de3cc191293fc28a
parent 259644 10a4c5fbcca7d515fa287f89892fdd4c5e9a243e
child 259646 053d760eb1cf9fd1b7c93f42188b57c985dceb67
push id64300
push usereakhgari@mozilla.com
push dateThu, 27 Aug 2015 20:44:52 +0000
treeherdermozilla-inbound@cb7008d63383 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs1009795
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1009795 - Use toLocalString to format download size instead of the decimalSymbol hook; r=mak Original Patch by Simon Montagu <smontagu@smontagu.org>
toolkit/mozapps/downloads/DownloadUtils.jsm
--- a/toolkit/mozapps/downloads/DownloadUtils.jsm
+++ b/toolkit/mozapps/downloads/DownloadUtils.jsm
@@ -41,20 +41,30 @@ 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/);
-});
+let 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,
+      Intl.NumberFormat(locale,
+                        { maximumFractionDigits: fractionDigits,
+                          minimumFractionDigits: fractionDigits }));
+  }
+  return localeNumberFormatCache.get(key);
+}
 
 const kDownloadProperties =
   "chrome://mozapps/locale/downloads/downloads.properties";
 
 let gStr = {
   statusFormat: "statusFormat3",
   statusFormatInfiniteRate: "statusFormatInfiniteRate",
   statusFormatNoRate: "statusFormatNoRate",
@@ -456,21 +466,27 @@ this.DownloadUtils = {
     // we know the name of the next unit
     while ((aBytes >= 999.5) && (unitIndex < gStr.units.length - 1)) {
       aBytes /= 1024;
       unitIndex++;
     }
 
     // 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 
-    aBytes = aBytes.toFixed((aBytes > 0) && (aBytes < 100) && (unitIndex != 0) ? 1 : 0);
+    // 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;
 
-    if (gDecimalSymbol != ".")
-      aBytes = aBytes.replace(".", gDecimalSymbol);
+    // Don't try to format Infinity values using NumberFormat.
+    if (aBytes === Infinity) {
+      aBytes = "Infinity";
+    } else {
+      aBytes = getLocaleNumberFormat(fractionDigits)
+                 .format(aBytes);
+    }
+
     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.
    *
    * @param aSecs