Bug 1347798 - Create a small per-document bindings for DOMLocalization. r=mossop

MozReview-Commit-ID: DfxIYVxyt9C

--HG--
extra : rebase_source : 8b7b31f268408dea46af6202c8943ff11a2c165e
This commit is contained in:
Zibi Braniecki 2017-09-09 19:23:03 -07:00
parent 6bba3e6d42
commit 3ca67e913e
6 changed files with 77 additions and 1 deletions

View File

@ -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?)

View File

@ -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");
},

View File

@ -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());
}
}
}
/**

2
intl/l10n/jar.mn Normal file
View File

@ -0,0 +1,2 @@
toolkit.jar:
content/global/l10n.js

51
intl/l10n/l10n.js Normal file
View File

@ -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<string>}
*/
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();
});
}

View File

@ -15,4 +15,6 @@ XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell.ini']
MOCHITEST_CHROME_MANIFESTS += ['test/chrome.ini']
JAR_MANIFESTS += ['jar.mn']
FINAL_LIBRARY = 'xul'