Bug 1246748 - Complete the implementation of chrome.i18n.getUILanguage, r=kmag

Implement chrome.i18n.getUILanguage including tests

Add API to content scripts

MozReview-Commit-ID: IcDlLj8Et73
This commit is contained in:
bsilverberg 2016-02-18 07:50:17 -05:00
parent fb58d36b70
commit 18371f22fb
6 changed files with 97 additions and 5 deletions

View File

@ -1214,4 +1214,3 @@ Extension.prototype = extend(Object.create(ExtensionData.prototype), {
return this.localize(this.manifest.name);
},
});

View File

@ -118,6 +118,10 @@ var api = context => {
getMessage: function(messageName, substitutions) {
return context.extension.localizeMessage(messageName, substitutions);
},
getUILanguage: function() {
return context.extension.localeData.uiLocale;
},
},
};
};

View File

@ -333,9 +333,7 @@ LocaleData.prototype = {
// Check for certain pre-defined messages.
if (message == "@@ui_locale") {
// Return the browser locale, but convert it to a Chrome-style
// locale code.
return Locale.getLocale().replace(/-/g, "_");
return this.uiLocale;
} else if (message.startsWith("@@bidi_")) {
let registry = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(Ci.nsIXULChromeRegistry);
let rtl = registry.isLocaleRTL("global");
@ -426,6 +424,13 @@ LocaleData.prototype = {
this.messages.set(locale, result);
return result;
},
get uiLocale() {
// Return the browser locale, but convert it to a Chrome-style
// locale code.
return Locale.getLocale().replace(/-/g, "_");
},
};
// This is a generic class for managing event listeners. Example usage:

View File

@ -6,6 +6,10 @@ extensions.registerSchemaAPI("i18n", null, (extension, context) => {
getMessage: function(messageName, substitutions) {
return extension.localizeMessage(messageName, substitutions);
},
getUILanguage: function() {
return extension.localeData.uiLocale;
},
},
};
});

View File

@ -68,7 +68,6 @@
},
{
"name": "getUILanguage",
"unsupported": true,
"type": "function",
"description": "Gets the browser UI language of the browser. This is different from $(ref:i18n.getAcceptLanguages) which returns the preferred user languages.",
"parameters": [],

View File

@ -14,6 +14,8 @@
<script type="text/javascript">
"use strict";
SimpleTest.registerCleanupFunction(() => { SpecialPowers.clearUserPref("general.useragent.locale"); });
add_task(function* test_i18n() {
function runTests(assertEq) {
let _ = browser.i18n.getMessage.bind(browser.i18n);
@ -161,6 +163,85 @@ add_task(function* test_i18n() {
yield extension.unload();
});
add_task(function* test_get_ui_language() {
function getResults() {
return {
getUILanguage: browser.i18n.getUILanguage(),
getMessage: browser.i18n.getMessage("@@ui_locale"),
};
}
function background(getResults) {
function checkResults(source, results, expected) {
browser.test.assertEq(
expected,
results.getUILanguage,
`Got expected getUILanguage result in ${source}`
);
browser.test.assertEq(
expected,
results.getMessage,
`Got expected getMessage result in ${source}`
);
}
let tabId;
browser.test.onMessage.addListener(([msg, expected]) => {
browser.tabs.sendMessage(tabId, "get-results", result => {
checkResults("contentScript", result, expected);
checkResults("background", getResults(), expected);
browser.test.sendMessage("done");
});
});
browser.tabs.query({currentWindow: true, active: true}, tabs => {
tabId = tabs[0].id;
browser.test.sendMessage("ready");
});
}
function content(getResults) {
browser.runtime.onMessage.addListener((msg, sender, respond) => {
respond(getResults());
});
}
let extension = ExtensionTestUtils.loadExtension({
manifest: {
"content_scripts": [{
"matches": ["http://mochi.test/*/file_sample.html"],
"run_at": "document_start",
"js": ["content_script.js"],
}],
},
background: `(${background})(${getResults})`,
files: {
"content_script.js": `(${content})(${getResults})`,
},
});
let win = window.open("file_sample.html");
yield extension.startup();
yield extension.awaitMessage("ready");
extension.sendMessage(["expect-results", "en_US"]);
yield extension.awaitMessage("done");
SpecialPowers.setCharPref("general.useragent.locale", "he");
extension.sendMessage(["expect-results", "he"]);
yield extension.awaitMessage("done");
win.close();
yield extension.unload();
});
</script>
</body>