mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-25 05:41:12 +00:00
Bug 1347798 - Create a small per-document bindings for DOMLocalization. r=mossop
MozReview-Commit-ID: DfxIYVxyt9C --HG-- extra : rebase_source : 946084f272be3aee55c6690200a51bf78a141fa8
This commit is contained in:
parent
74fb7fe693
commit
c89f208415
@ -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");
|
||||
},
|
||||
|
||||
|
@ -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
2
intl/l10n/jar.mn
Normal file
@ -0,0 +1,2 @@
|
||||
toolkit.jar:
|
||||
content/global/l10n.js
|
51
intl/l10n/l10n.js
Normal file
51
intl/l10n/l10n.js
Normal 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();
|
||||
});
|
||||
}
|
@ -15,4 +15,6 @@ XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell.ini']
|
||||
|
||||
MOCHITEST_CHROME_MANIFESTS += ['test/chrome.ini']
|
||||
|
||||
JAR_MANIFESTS += ['jar.mn']
|
||||
|
||||
FINAL_LIBRARY = 'xul'
|
||||
|
Loading…
Reference in New Issue
Block a user