From 3ca67e913ed46969874c64689ab5db6578f511b0 Mon Sep 17 00:00:00 2001 From: Zibi Braniecki Date: Sat, 9 Sep 2017 19:23:03 -0700 Subject: [PATCH] Bug 1347798 - Create a small per-document bindings for DOMLocalization. r=mossop MozReview-Commit-ID: DfxIYVxyt9C --HG-- extra : rebase_source : 8b7b31f268408dea46af6202c8943ff11a2c165e --- .../static/browser_all_files_referenced.js | 2 +- browser/components/nsBrowserGlue.js | 10 ++++ intl/l10n/Localization.jsm | 11 ++++ intl/l10n/jar.mn | 2 + intl/l10n/l10n.js | 51 +++++++++++++++++++ intl/l10n/moz.build | 2 + 6 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 intl/l10n/jar.mn create mode 100644 intl/l10n/l10n.js diff --git a/browser/base/content/test/static/browser_all_files_referenced.js b/browser/base/content/test/static/browser_all_files_referenced.js index 8b32dfdb5b0c..32bb43426380 100644 --- a/browser/base/content/test/static/browser_all_files_referenced.js +++ b/browser/base/content/test/static/browser_all_files_referenced.js @@ -126,7 +126,7 @@ var whitelist = [ {file: "resource://shield-recipe-client-content/shield-content-process.js"}, // New L10n API that is not yet used in production - {file: "resource://gre/modules/DOMLocalization.jsm"}, + {file: "chrome://global/content/l10n.js"}, // Starting from here, files in the whitelist are bugs that need fixing. // Bug 1339424 (wontfix?) diff --git a/browser/components/nsBrowserGlue.js b/browser/components/nsBrowserGlue.js index e865481cbbc3..cd786acf81f1 100644 --- a/browser/components/nsBrowserGlue.js +++ b/browser/components/nsBrowserGlue.js @@ -37,8 +37,10 @@ XPCOMUtils.defineLazyModuleGetters(this, { ExtensionsUI: "resource:///modules/ExtensionsUI.jsm", Feeds: "resource:///modules/Feeds.jsm", FileUtils: "resource://gre/modules/FileUtils.jsm", + FileSource: "resource://gre/modules/L10nRegistry.jsm", FormValidationHandler: "resource:///modules/FormValidationHandler.jsm", Integration: "resource://gre/modules/Integration.jsm", + L10nRegistry: "resource://gre/modules/L10nRegistry.jsm", LightweightThemeManager: "resource://gre/modules/LightweightThemeManager.jsm", LoginHelper: "resource://gre/modules/LoginHelper.jsm", LoginManagerParent: "resource://gre/modules/LoginManagerParent.jsm", @@ -630,6 +632,14 @@ BrowserGlue.prototype = { }); } + + // Initialize the default l10n resource sources for L10nRegistry. + const locales = [AppConstants.INSTALL_LOCALE]; + const toolkitSource = new FileSource("toolkit", locales, "resource://gre/localization/{locale}/"); + L10nRegistry.registerSource(toolkitSource); + const appSource = new FileSource("app", locales, "resource://app/localization/{locale}/"); + L10nRegistry.registerSource(appSource); + Services.obs.notifyObservers(null, "browser-ui-startup-complete"); }, diff --git a/intl/l10n/Localization.jsm b/intl/l10n/Localization.jsm index 0b02e8172904..f12af89d601f 100644 --- a/intl/l10n/Localization.jsm +++ b/intl/l10n/Localization.jsm @@ -57,6 +57,17 @@ class CachedIterable { } }; } + + /** + * This method allows user to consume the next element from the iterator + * into the cache. + */ + touchNext() { + const { seen, iterator } = this; + if (seen.length === 0 || seen[seen.length - 1].done === false) { + seen.push(iterator.next()); + } + } } /** diff --git a/intl/l10n/jar.mn b/intl/l10n/jar.mn new file mode 100644 index 000000000000..2bd7ce1df14a --- /dev/null +++ b/intl/l10n/jar.mn @@ -0,0 +1,2 @@ +toolkit.jar: + content/global/l10n.js diff --git a/intl/l10n/l10n.js b/intl/l10n/l10n.js new file mode 100644 index 000000000000..a4e2f0a608b8 --- /dev/null +++ b/intl/l10n/l10n.js @@ -0,0 +1,51 @@ +{ + const { DOMLocalization } = + Components.utils.import("resource://gre/modules/DOMLocalization.jsm"); + + /** + * Polyfill for document.ready polyfill. + * See: https://github.com/whatwg/html/issues/127 for details. + * + * @returns {Promise} + */ + function documentReady() { + const rs = document.readyState; + if (rs === 'interactive' || rs === 'completed') { + return Promise.resolve(); + } + + return new Promise( + resolve => document.addEventListener( + 'readystatechange', resolve, { once: true } + ) + ); + } + + /** + * Scans the `elem` for links with localization resources. + * + * @param {Element} elem + * @returns {Array} + */ + function getResourceLinks(elem) { + return Array.from(elem.querySelectorAll('link[rel="localization"]')).map( + el => el.getAttribute('href') + ); + } + + const resourceIds = getResourceLinks(document.head || document); + + document.l10n = new DOMLocalization(window, resourceIds); + + // trigger first context to be fetched eagerly + document.l10n.ctxs.touchNext(); + + document.l10n.ready = documentReady().then(() => { + document.l10n.registerObservers(); + window.addEventListener('unload', () => { + document.l10n.unregisterObservers(); + }); + document.l10n.connectRoot(document.documentElement); + return document.l10n.translateRoots(); + }); +} diff --git a/intl/l10n/moz.build b/intl/l10n/moz.build index ee1a8279c8c6..344ceb46a30b 100644 --- a/intl/l10n/moz.build +++ b/intl/l10n/moz.build @@ -15,4 +15,6 @@ XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell.ini'] MOCHITEST_CHROME_MANIFESTS += ['test/chrome.ini'] +JAR_MANIFESTS += ['jar.mn'] + FINAL_LIBRARY = 'xul'