Bug 1167300 - Create a way to get strings from multiple localization files, r=jsantell, a=sledru
authorVictor Porof <vporof@mozilla.com>
Fri, 22 May 2015 16:25:52 -0400
changeset 274869 b6618222aa95979961ec741c231354170b1d0bc0
parent 274868 f0fa2388c1e8515e5fb9b0ae2048eac82ccf0ad1
child 274870 980d943b262c84ba3fa0e1a15428b7f88d93e2ae
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjsantell, sledru
bugs1167300
milestone40.0a2
Bug 1167300 - Create a way to get strings from multiple localization files, r=jsantell, a=sledru
browser/devtools/performance/modules/global.js
browser/devtools/shared/widgets/ViewHelpers.jsm
--- a/browser/devtools/performance/modules/global.js
+++ b/browser/devtools/performance/modules/global.js
@@ -10,20 +10,20 @@ loader.lazyRequireGetter(this, "Services
 
 // String used to fill in platform data when it should be hidden.
 const GECKO_SYMBOL = "(Gecko)";
 
 /**
  * Localization convenience methods.
  + TODO: merge these into a single file: Bug 1082695.
  */
-const TIMELINE_STRINGS_URI = "chrome://browser/locale/devtools/timeline.properties";
-const PROFILER_STRINGS_URI = "chrome://browser/locale/devtools/profiler.properties";
-
-const L10N = new ViewHelpers.L10N(STRINGS_URI);
+const L10N = new ViewHelpers.MultiL10N([
+  "chrome://browser/locale/devtools/timeline.properties",
+  "chrome://browser/locale/devtools/profiler.properties"
+]);
 
 /**
  * A list of preferences for this tool. The values automatically update
  * if somebody edits edits about:config or the prefs change somewhere else.
  */
 const Prefs = new ViewHelpers.Prefs("devtools.performance.ui", {
   showPlatformData: ["Bool", "show-platform-data"]
 }, {
--- a/browser/devtools/shared/widgets/ViewHelpers.jsm
+++ b/browser/devtools/shared/widgets/ViewHelpers.jsm
@@ -380,16 +380,39 @@ ViewHelpers.L10N.prototype = {
     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"],
  *     myFloatPref: ["Float", "leaf.path.to.my-float-pref"]
  *     ...