Bug 597852 - Internationalize decimal separator in Download Manager. r=Mardak a=beltzner
authorPhilipp von Weitershausen <philipp@weitershausen.de>
Fri, 08 Oct 2010 17:57:04 +0200
changeset 55198 b7500203ad6dcccfb6bde0c2f02a7b6157078216
parent 55197 48791cc026c1dddfcc040b3f7f0e1fdeedda2936
child 55199 98a77908bac6d02603021ad91f146be7a8246d28
push idunknown
push userunknown
push dateunknown
reviewersMardak, beltzner
bugs597852
milestone2.0b8pre
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 597852 - Internationalize decimal separator in Download Manager. r=Mardak a=beltzner
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
@@ -51,33 +51,38 @@ var EXPORTED_SYMBOLS = [ "DownloadUtils"
  * getTransferTotal(int aCurrBytes, [optional] int aMaxBytes)
  *
  * [string timeLeft, double newLast]
  * getTimeLeft(double aSeconds, [optional] double aLastSec)
  *
  * [string displayHost, string fullHost]
  * getURIHost(string aURIString)
  *
- * [double convertedBytes, string units]
+ * [string convertedBytes, string units]
  * convertByteUnits(int aBytes)
  *
  * [int time, string units, int subTime, string subUnits]
  * convertTimeUnits(double aSecs)
  */
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
 __defineGetter__("PluralForm", function() {
   delete this.PluralForm;
   Cu.import("resource://gre/modules/PluralForm.jsm");
   return PluralForm;
 });
 
+__defineGetter__("gDecimalSymbol", function() {
+  delete this.gDecimalSymbol;
+  return this.gDecimalSymbol = Number(5.4).toLocaleString().match(/\D/);
+});
+
 const kDownloadProperties =
   "chrome://mozapps/locale/downloads/downloads.properties";
 
 // These strings will be converted to the corresponding ones from the string
 // bundle on use
 let kStrings = {
   statusFormat: "statusFormat2",
   transferSameUnits: "transferSameUnits",
@@ -219,17 +224,17 @@ let DownloadUtils = {
     if (aMaxBytes == null)
       aMaxBytes = -1;
 
     let [progress, progressUnits] = DownloadUtils.convertByteUnits(aCurrBytes);
     let [total, totalUnits] = DownloadUtils.convertByteUnits(aMaxBytes);
 
     // Figure out which byte progress string to display
     let transfer;
-    if (total < 0)
+    if (aMaxBytes < 0)
       transfer = gStr.transferNoTotal;
     else if (progressUnits == totalUnits)
       transfer = gStr.transferSameUnits;
     else
       transfer = gStr.transferDiffUnits;
 
     transfer = replaceInsert(transfer, 1, progress);
     transfer = replaceInsert(transfer, 2, progressUnits);
@@ -374,18 +379,18 @@ let DownloadUtils = {
       displayHost += port;
       fullHost += port;
     }
 
     return [displayHost, fullHost];
   },
 
   /**
-   * Converts a number of bytes to the appropriate unit that results in a
-   * number that needs fewer than 4 digits
+   * Converts a number of bytes to the appropriate unit that results in an
+   * internationalized number that needs fewer than 4 digits.
    *
    * @param aBytes
    *        Number of bytes to convert
    * @return A pair: [new value with 3 sig. figs., its unit]
    */
   convertByteUnits: function DU_convertByteUnits(aBytes)
   {
     let unitIndex = 0;
@@ -396,16 +401,18 @@ let DownloadUtils = {
       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
     aBytes = aBytes.toFixed((aBytes > 0) && (aBytes < 100) ? 1 : 0);
 
+    if (gDecimalSymbol != ".")
+      aBytes = aBytes.replace(".", gDecimalSymbol);
     return [aBytes, 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
--- a/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
+++ b/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
@@ -32,16 +32,34 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 let Cu = Components.utils;
 Cu.import("resource://gre/modules/DownloadUtils.jsm");
 
+const gDecimalSymbol = Number(5.4).toLocaleString().match(/\D/);
+function _(str) {
+  return str.replace(".", gDecimalSymbol);
+}
+
+function testConvertByteUnits(aBytes, aValue, aUnit)
+{
+  let [value, unit] = DownloadUtils.convertByteUnits(aBytes);
+  do_check_eq(value, aValue);
+  do_check_eq(unit, aUnit);
+}
+
+function testTransferTotal(aCurrBytes, aMaxBytes, aTransfer)
+{
+  let transfer = DownloadUtils.getTransferTotal(aCurrBytes, aMaxBytes);
+  do_check_eq(transfer, aTransfer);
+}
+
 // Get the em-dash character because typing it directly here doesn't work :(
 let gDash = DownloadUtils.getDownloadStatus(0)[0].match(/remaining (.) 0 bytes/)[1];
 
 let gVals = [0, 100, 2345, 55555, 982341, 23194134, 1482, 58, 9921949201, 13498132];
 
 function testStatus(aCurr, aMore, aRate, aTest)
 {
   dump("Status Test: " + [aCurr, aMore, aRate, aTest] + "\n");
@@ -75,16 +93,35 @@ function testURI(aURI, aDisp, aHost)
 
   // Make sure we have the right display host and full host
   do_check_eq(disp, aDisp);
   do_check_eq(host, aHost);
 }
 
 function run_test()
 {
+  testConvertByteUnits(-1, "-1", "bytes");
+  testConvertByteUnits(1, _("1.0"), "bytes");
+  testConvertByteUnits(42, _("42.0"), "bytes");
+  testConvertByteUnits(123, _("123"), "bytes");
+  testConvertByteUnits(1024, _("1.0"), "KB");
+  testConvertByteUnits(8888, _("8.7"), "KB");
+  testConvertByteUnits(59283, _("57.9"), "KB");
+  testConvertByteUnits(640000, _("625"), "KB");
+  testConvertByteUnits(1048576, _("1.0"), "MB");
+  testConvertByteUnits(307232768, _("293"), "MB");
+  testConvertByteUnits(1073741824, _("1.0"), "GB");
+
+  testTransferTotal(1, 1, _("1.0 of 1.0 bytes"));
+  testTransferTotal(234, 4924, _("234 bytes of 4.8 KB"));
+  testTransferTotal(94923, 233923, _("92.7 of 228 KB"));
+  testTransferTotal(2342, 294960345, _("2.3 KB of 281 MB"));
+  testTransferTotal(234, undefined, _("234 bytes"));
+  testTransferTotal(4889023, undefined, _("4.7 MB"));
+
   if (0) {
     // Help find some interesting test cases
     let r = function() Math.floor(Math.random() * 10);
     for (let i = 0; i < 100; i++) {
       testStatus(r(), r(), r());
     }
   }