Skip to content

Instantly share code, notes, and snippets.

@kmaglione
Created April 20, 2018 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmaglione/cf2446c418b0326d7cd4c8867043083f to your computer and use it in GitHub Desktop.
Save kmaglione/cf2446c418b0326d7cd4c8867043083f to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="localized.js"></script>
</head>
<body>
</body>
</html>
dump(`Sup.\n`);
dump(` . ${browser}\n`);
dump(` . ${browser.fluent}\n`);
dump(` . ${Object.keys(browser.fluent)}\n`);
dump(` . ${browser.fluent.generateMessages}\n`);
(async () => {
for await (let ctxt of browser.fluent.generateMessages(["/test.ftl"])) {
dump(`CTXT ${ctxt}\n`);
dump(` has ${ctxt.hasMessage("key")}\n`);
let msg = ctxt.getMessage("key");
dump(` msg ${msg}\n`);
dump(` fmt ${ctxt.format(msg, [], [])}\n`);
}
window.postMessage("done", "*");
})();
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
ChromeUtils.import("resource://gre/modules/ContentLocalization.jsm");
ChromeUtils.import("resource://gre/modules/Localization.jsm");
ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.import("resource://gre/modules/ExtensionUtils.jsm");
function mockSomeThings() {
const { L10nRegistry, FileSource } =
ChromeUtils.import("resource://gre/modules/L10nRegistry.jsm", {});
const known_platforms = {
"linux": "linux",
"win": "windows",
"macosx": "macos",
"android": "android",
};
const fs = {
"/localization/en-US/test.ftl": `
key = { PLATFORM() ->
${ Object.values(known_platforms).map(
name => ` [${ name }] ${ name.toUpperCase() } Value\n`).join("") }
*[other] OTHER Value
}`,
};
L10nRegistry.load = async function(url) {
return fs[url];
};
const source = new FileSource("test", ["en-US"], "/localization/{locale}");
L10nRegistry.registerSource(source);
}
add_task(async function() {
let resProto = Services.io.getProtocolHandler("resource")
.QueryInterface(Ci.nsISubstitutingProtocolHandler);
let rootURI = Services.io.newFileURI(do_get_file("data"));
resProto.setSubstitution("foo", rootURI);
mockSomeThings();
ContentLocalization.registerContentPages({
matches: ["resource://foo/*"],
resourceIds: ["test.ftl"],
});
let browser = Services.appShell.createWindowlessBrowser(false);
let webNav = browser.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDocShell)
.QueryInterface(Ci.nsIWebNavigation);
webNav.loadURI("resource://foo/localized.html", 0, null, null, null);
await new Promise(executeSoon);
await ExtensionUtils.promiseEvent(webNav.document.ownerGlobal, "message");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment