Bug 1258305 - Pull ViewHelpers.L10N, MultiL10N into their own module, r=jsantell
authorVictor Porof <vporof@mozilla.com>
Tue, 22 Mar 2016 10:17:20 +0100
changeset 289874 cc08584692775437121919c69adcd4fc0cfb97f7
parent 289873 cd48f1c046341fd93c1e396644bfc4ae2d732f75
child 289875 b8b21810b2b3b42a90dfda07c3ff0ecf1a030727
push id74027
push userkwierso@gmail.com
push dateTue, 22 Mar 2016 23:57:42 +0000
treeherdermozilla-inbound@eb528d042c85 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjsantell
bugs1258305
milestone48.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 1258305 - Pull ViewHelpers.L10N, MultiL10N into their own module, r=jsantell
devtools/client/shared/l10n.js
devtools/client/shared/moz.build
devtools/client/shared/widgets/ViewHelpers.jsm
new file mode 100644
--- /dev/null
+++ b/devtools/client/shared/l10n.js
@@ -0,0 +1,122 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Ci } = require("chrome");
+const Services = require("Services");
+
+/**
+ * Localization convenience methods.
+ *
+ * @param string stringBundleName
+ *        The desired string bundle's name.
+ */
+function LocalizationHelper(stringBundleName) {
+  loader.lazyGetter(this, "stringBundle", () =>
+    Services.strings.createBundle(stringBundleName));
+  loader.lazyGetter(this, "ellipsis", () =>
+    Services.prefs.getComplexValue("intl.ellipsis", Ci.nsIPrefLocalizedString).data);
+}
+
+LocalizationHelper.prototype = {
+  /**
+   * L10N shortcut function.
+   *
+   * @param string name
+   * @return string
+   */
+  getStr: function(name) {
+    return this.stringBundle.GetStringFromName(name);
+  },
+
+  /**
+   * L10N shortcut function.
+   *
+   * @param string name
+   * @param array args
+   * @return string
+   */
+  getFormatStr: function(name, ...args) {
+    return this.stringBundle.formatStringFromName(name, args, args.length);
+  },
+
+  /**
+   * L10N shortcut function for numeric arguments that need to be formatted.
+   * All numeric arguments will be fixed to 2 decimals and given a localized
+   * decimal separator. Other arguments will be left alone.
+   *
+   * @param string name
+   * @param array args
+   * @return string
+   */
+  getFormatStrWithNumbers: function(name, ...args) {
+    let newArgs = args.map(x => typeof x == "number" ? this.numberWithDecimals(x, 2) : x);
+    return this.stringBundle.formatStringFromName(name, newArgs, newArgs.length);
+  },
+
+  /**
+   * Converts a number to a locale-aware string format and keeps a certain
+   * number of decimals.
+   *
+   * @param number number
+   *        The number to convert.
+   * @param number decimals [optional]
+   *        Total decimals to keep.
+   * @return string
+   *         The localized number as a string.
+   */
+  numberWithDecimals: function(number, decimals = 0) {
+    // If this is an integer, don't do anything special.
+    if (number === (number|0)) {
+      return number;
+    }
+    // If this isn't a number (and yes, `isNaN(null)` is false), return zero.
+    if (isNaN(number) || number === null) {
+      return "0";
+    }
+
+    let localized = number.toLocaleString();
+
+    // If no grouping or decimal separators are available, bail out, because
+    // padding with zeros at the end of the string won't make sense anymore.
+    if (!localized.match(/[^\d]/)) {
+      return localized;
+    }
+
+    return number.toLocaleString(undefined, {
+      maximumFractionDigits: decimals,
+      minimumFractionDigits: decimals
+    });
+  }
+};
+
+/**
+ * A helper for having the same interface as LocalizationHelper, but for more than
+ * one file. Useful for abstracting l10n string locations.
+ */
+function MultiLocalizationHelper(...stringBundleNames) {
+  let instances = stringBundleNames.map(bundle => new LocalizationHelper(bundle));
+
+  // Get all function members of the LocalizationHelper class, making sure we're not
+  // executing any potential getters while doing so, and wrap all the
+  // methods we've found to work on all given string bundles.
+  Object.getOwnPropertyNames(LocalizationHelper.prototype)
+    .map(name => ({
+      name: name,
+      descriptor: Object.getOwnPropertyDescriptor(LocalizationHelper.prototype, name)
+    }))
+    .filter(({ descriptor }) => descriptor.value instanceof Function)
+    .forEach(method => {
+      this[method.name] = (...args) => {
+        for (let l10n of instances) {
+          try {
+            return method.descriptor.value.apply(l10n, args);
+          } catch (e) {}
+        }
+      };
+    });
+}
+
+exports.LocalizationHelper = LocalizationHelper;
+exports.MultiLocalizationHelper = MultiLocalizationHelper;
--- a/devtools/client/shared/moz.build
+++ b/devtools/client/shared/moz.build
@@ -27,16 +27,17 @@ DevToolsModules(
     'DOMHelpers.jsm',
     'doorhanger.js',
     'file-watcher-worker.js',
     'file-watcher.js',
     'frame-script-utils.js',
     'getjson.js',
     'inplace-editor.js',
     'Jsbeautify.jsm',
+    'l10n.js',
     'node-attribute-parser.js',
     'options-view.js',
     'output-parser.js',
     'poller.js',
     'source-utils.js',
     'SplitView.jsm',
     'telemetry.js',
     'theme-switching.js',
--- a/devtools/client/shared/widgets/ViewHelpers.jsm
+++ b/devtools/client/shared/widgets/ViewHelpers.jsm
@@ -301,124 +301,17 @@ this.ViewHelpers = {
     if (aFlags.delayed) {
       aPane.ownerDocument.defaultView.setTimeout(doToggle, PANE_APPEARANCE_DELAY);
     } else {
       doToggle();
     }
   }
 };
 
-/**
- * Localization convenience methods.
- *
- * @param string aStringBundleName
- *        The desired string bundle's name.
- */
-ViewHelpers.L10N = function(aStringBundleName) {
-  XPCOMUtils.defineLazyGetter(this, "stringBundle", () =>
-    Services.strings.createBundle(aStringBundleName));
 
-  XPCOMUtils.defineLazyGetter(this, "ellipsis", () =>
-    Services.prefs.getComplexValue("intl.ellipsis", Ci.nsIPrefLocalizedString).data);
-};
-
-ViewHelpers.L10N.prototype = {
-  stringBundle: null,
-
-  /**
-   * L10N shortcut function.
-   *
-   * @param string aName
-   * @return string
-   */
-  getStr: function(aName) {
-    return this.stringBundle.GetStringFromName(aName);
-  },
-
-  /**
-   * L10N shortcut function.
-   *
-   * @param string aName
-   * @param array aArgs
-   * @return string
-   */
-  getFormatStr: function(aName, ...aArgs) {
-    return this.stringBundle.formatStringFromName(aName, aArgs, aArgs.length);
-  },
-
-  /**
-   * L10N shortcut function for numeric arguments that need to be formatted.
-   * All numeric arguments will be fixed to 2 decimals and given a localized
-   * decimal separator. Other arguments will be left alone.
-   *
-   * @param string aName
-   * @param array aArgs
-   * @return string
-   */
-  getFormatStrWithNumbers: function(aName, ...aArgs) {
-    let newArgs = aArgs.map(x => typeof x == "number" ? this.numberWithDecimals(x, 2) : x);
-    return this.stringBundle.formatStringFromName(aName, newArgs, newArgs.length);
-  },
-
-  /**
-   * Converts a number to a locale-aware string format and keeps a certain
-   * number of decimals.
-   *
-   * @param number aNumber
-   *        The number to convert.
-   * @param number aDecimals [optional]
-   *        Total decimals to keep.
-   * @return string
-   *         The localized number as a string.
-   */
-  numberWithDecimals: function(aNumber, aDecimals = 0) {
-    // If this is an integer, don't do anything special.
-    if (aNumber == (aNumber | 0)) {
-      return aNumber;
-    }
-    if (isNaN(aNumber) || aNumber == null) {
-      return "0";
-    }
-    let localized = aNumber.toLocaleString(); // localize
-
-    // If no grouping or decimal separators are available, bail out, because
-    // padding with zeros at the end of the string won't make sense anymore.
-    if (!localized.match(/[^\d]/)) {
-      return localized;
-    }
-
-    return aNumber.toLocaleString(undefined, {
-      maximumFractionDigits: aDecimals,
-      minimumFractionDigits: aDecimals
-    });
-  }
-};
-
-/**
- * A helper for having the same interface as ViewHelpers.L10N, but for
- * more than one file. Useful for abstracting l10n string locations.
- */
-ViewHelpers.MultiL10N = function(aStringBundleNames) {
-  let l10ns = aStringBundleNames.map(bundle => new ViewHelpers.L10N(bundle));
-  let proto = ViewHelpers.L10N.prototype;
-
-  Object.getOwnPropertyNames(proto)
-    .map(name => ({
-      name: name,
-      desc: Object.getOwnPropertyDescriptor(proto, name)
-    }))
-    .filter(property => property.desc.value instanceof Function)
-    .forEach(method => {
-      this[method.name] = function(...args) {
-        for (let l10n of l10ns) {
-          try { return method.desc.value.apply(l10n, args) } catch (e) {}
-        }
-      };
-    });
-};
 
 /**
  * Shortcuts for lazily accessing and setting various preferences.
  * Usage:
  *   let prefs = new ViewHelpers.Prefs("root.path.to.branch", {
  *     myIntPref: ["Int", "leaf.path.to.my-int-pref"],
  *     myCharPref: ["Char", "leaf.path.to.my-char-pref"],
  *     myJsonPref: ["Json", "leaf.path.to.my-json-pref"],