Commit a14c6be5 authored by Greg Tatum's avatar Greg Tatum
Browse files

Bug 1761412 - Invalidate the L10nCache when the app locale changes r=platform-i18n-reviewers,dminor

parent d853a227
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
@@ -2110,6 +2110,11 @@ class L10nCache {
   */
  constructor(l10n) {
    this.l10n = Cu.getWeakReference(l10n);
    this.QueryInterface = ChromeUtils.generateQI([
      "nsIObserver",
      "nsISupportsWeakReference",
    ]);
    Services.obs.addObserver(this, "intl:app-locales-changed", true);
  }

  /**
@@ -2236,6 +2241,31 @@ class L10nCache {
    this._messagesByKey.clear();
  }

  /**
   * Returns the number of cached messages.
   *
   * @returns {number}
   */
  size() {
    return this._messagesByKey.size;
  }

  /**
   * Observer method from Services.obs.addObserver.
   * @param {nsISupports} subject
   * @param {string} topic
   * @param {string} data
   */
  async observe(subject, topic, data) {
    switch (topic) {
      case "intl:app-locales-changed": {
        await this.l10n.ready;
        this.clear();
        break;
      }
    }
  }

  /**
   * Cache keys => cached message objects
   */
+6 −0
Original line number Diff line number Diff line
@@ -325,6 +325,12 @@ add_task(async function comprehensive() {
      `Expected message for id=${id} args=${JSON.stringify(args)}`
    );
  }

  // Ensure the cache is cleared after the app locale changes
  Assert.greater(cache.size(), 0, "The cache has messages in it.");
  Services.obs.notifyObservers(null, "intl:app-locales-changed");
  await l10n.ready;
  Assert.equal(cache.size(), 0, "The cache is empty on app locale change");
});

/**