gecko-dev/intl/l10n/l10n.js
Zibi Braniecki 6ab04fc7a2 Bug 1474786 - Update Fluent to master. r=stas
MozReview-Commit-ID: 5PMKF46TPVM

--HG--
extra : rebase_source : cb1efb34498c6d067cde90e5f999267ce91b08e9
2018-07-10 17:49:02 -07:00

58 lines
1.5 KiB
JavaScript

{
const { DOMLocalization } =
ChromeUtils.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() {
if (document.contentType === "application/vnd.mozilla.xul+xml") {
// XUL
return new Promise(
resolve => document.addEventListener(
"MozBeforeInitialXULLayout", resolve, { once: true }
)
);
}
// HTML
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(resourceIds);
// Trigger the first two contexts to be loaded eagerly.
document.l10n.ctxs.touchNext(2);
document.l10n.ready = documentReady().then(() => {
document.l10n.registerObservers();
document.l10n.connectRoot(document.documentElement);
return document.l10n.translateRoots();
});
}