Bug 1362225 - Speed up i18n.getMessage. r?aswan draft
authorKris Maglione <maglione.k@gmail.com>
Thu, 04 May 2017 16:38:18 -0700
changeset 572918 d8df6db26abea1c0f06f78828baf3f4679cb86e7
parent 572524 fb4fca2b150a3715192e60e4dde593052fb563e2
child 572933 b77101bbdce6c30c617ee1a86ab60c8b9a6c435d
push id57225
push usermaglione.k@gmail.com
push dateThu, 04 May 2017 23:39:36 +0000
reviewersaswan
bugs1362225
milestone55.0a1
Bug 1362225 - Speed up i18n.getMessage. r?aswan MozReview-Commit-ID: EcadiurI7tu
toolkit/components/extensions/ExtensionCommon.jsm
--- a/toolkit/components/extensions/ExtensionCommon.jsm
+++ b/toolkit/components/extensions/ExtensionCommon.jsm
@@ -32,16 +32,17 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 Cu.import("resource://gre/modules/ExtensionUtils.jsm");
 
 var {
   DefaultMap,
   DefaultWeakMap,
   EventEmitter,
   ExtensionError,
   SpreadArgs,
+  defineLazyGetter,
   getConsole,
   getInnerWindowID,
   getUniqueId,
   runSafeSync,
   runSafeSyncWithoutClone,
   instanceOf,
 } = ExtensionUtils;
 
@@ -1155,33 +1156,39 @@ LocaleData.prototype = {
 
   has(locale) {
     return this.messages.has(locale);
   },
 
   // https://developer.chrome.com/extensions/i18n
   localizeMessage(message, substitutions = [], options = {}) {
     let defaultOptions = {
-      locale: this.selectedLocale,
       defaultValue: "",
       cloneScope: null,
     };
 
-    options = Object.assign(defaultOptions, options);
+    let locales = this.availableLocales;
+    if (options.locale) {
+      locales = new Set([this.BUILTIN, options.locale, this.defaultLocale]
+                        .filter(locale => this.messages.has(locale)));
+    }
 
-    let locales = new Set([this.BUILTIN, options.locale, this.defaultLocale]
-                          .filter(locale => this.messages.has(locale)));
+    options = Object.assign(defaultOptions, options);
 
     // Message names are case-insensitive, so normalize them to lower-case.
     message = message.toLowerCase();
     for (let locale of locales) {
       let messages = this.messages.get(locale);
       if (messages.has(message)) {
         let str = messages.get(message);
 
+        if (!str.includes("$")) {
+          return str;
+        }
+
         if (!Array.isArray(substitutions)) {
           substitutions = [substitutions];
         }
 
         let replacer = (matched, index, dollarSigns) => {
           if (index) {
             // This is not quite Chrome-compatible. Chrome consumes any number
             // of digits following the $, but only accepts 9 substitutions. We
@@ -1306,16 +1313,21 @@ LocaleData.prototype = {
 
   get uiLocale() {
     // Return the browser locale, but convert it to a Chrome-style
     // locale code.
     return Locale.getLocale().replace(/-/g, "_");
   },
 };
 
+defineLazyGetter(LocaleData.prototype, "availableLocales", function() {
+  return new Set([this.BUILTIN, this.selectedLocale, this.defaultLocale]
+                 .filter(locale => this.messages.has(locale)));
+});
+
 // This is a generic class for managing event listeners. Example usage:
 //
 // new SingletonEventManager(context, "api.subAPI", fire => {
 //   let listener = (...) => {
 //     // Fire any listeners registered with addListener.
 //     fire.async(arg1, arg2);
 //   };
 //   // Register the listener.