Skip to content

Commit

Permalink
Bug 1347798 - Create a small per-document bindings for DOMLocalizatio…
Browse files Browse the repository at this point in the history
…n. r=mossop

MozReview-Commit-ID: DfxIYVxyt9C
  • Loading branch information
Zibi Braniecki committed Sep 10, 2017
1 parent cbc0a71 commit 7e897ed
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand Down
10 changes: 10 additions & 0 deletions browser/components/nsBrowserGlue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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");
},

Expand Down
11 changes: 11 additions & 0 deletions intl/l10n/Localization.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions intl/l10n/jar.mn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
toolkit.jar:
content/global/l10n.js
51 changes: 51 additions & 0 deletions intl/l10n/l10n.js
Original file line number Diff line number Diff line change
@@ -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();
});
}
2 changes: 2 additions & 0 deletions intl/l10n/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell.ini']

MOCHITEST_CHROME_MANIFESTS += ['test/chrome.ini']

JAR_MANIFESTS += ['jar.mn']

FINAL_LIBRARY = 'xul'

0 comments on commit 7e897ed

Please sign in to comment.