Bug 1213450 - Complete the implementation of chrome.i18n - getAcceptLanguages, r=kmag

Implement browser.i18n.getAcceptLanguages() including tests
Rebase against fx-team, resolving some conflicts
Fix eslint errors/warnings

MozReview-Commit-ID: 52sZWsIHbl4

--HG--
extra : amend_source : 2339e42a83460682a8d6df206c10849b38a288eb
This commit is contained in:
bsilverberg 2016-02-25 07:29:09 -05:00
Родитель efcd855704
Коммит ad65868512
5 изменённых файлов: 102 добавлений и 1 удалений

Просмотреть файл

@ -120,6 +120,11 @@ var api = context => {
return context.extension.localizeMessage(messageName, substitutions);
},
getAcceptLanguages: function(callback) {
let result = context.extension.localeData.acceptLanguages;
return context.wrapPromise(Promise.resolve(result), callback);
},
getUILanguage: function() {
return context.extension.localeData.uiLocale;
},

Просмотреть файл

@ -20,6 +20,8 @@ XPCOMUtils.defineLazyModuleGetter(this, "LanguageDetector",
"resource:///modules/translation/LanguageDetector.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Locale",
"resource://gre/modules/Locale.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Preferences",
"resource://gre/modules/Preferences.jsm");
function filterStack(error) {
return String(error.stack).replace(/(^.*(Task\.jsm|Promise-backend\.js).*\n)+/gm, "<Promise Chain>\n");
@ -429,6 +431,16 @@ LocaleData.prototype = {
return result;
},
get acceptLanguages() {
let result = Preferences.get("intl.accept_languages", "", Ci.nsIPrefLocalizedString);
result = result.split(",");
result = result.map(lang => {
return lang.replace(/-/g, "_").trim();
});
return result;
},
get uiLocale() {
// Return the browser locale, but convert it to a Chrome-style
// locale code.

Просмотреть файл

@ -12,6 +12,11 @@ extensions.registerSchemaAPI("i18n", null, (extension, context) => {
return extension.localizeMessage(messageName, substitutions);
},
getAcceptLanguages: function() {
let result = extension.localeData.acceptLanguages;
return Promise.resolve(result);
},
getUILanguage: function() {
return extension.localeData.uiLocale;
},

Просмотреть файл

@ -30,7 +30,6 @@
"functions": [
{
"name": "getAcceptLanguages",
"unsupported": true,
"type": "function",
"description": "Gets the accept-languages of the browser. This is different from the locale used by the browser; to get the locale, use $(ref:i18n.getUILanguage).",
"async": "callback",

Просмотреть файл

@ -14,6 +14,7 @@
<script type="text/javascript">
"use strict";
SimpleTest.registerCleanupFunction(() => { SpecialPowers.clearUserPref("intl.accept_languages"); });
SimpleTest.registerCleanupFunction(() => { SpecialPowers.clearUserPref("general.useragent.locale"); });
add_task(function* test_i18n() {
@ -163,6 +164,85 @@ add_task(function* test_i18n() {
yield extension.unload();
});
add_task(function* test_get_accept_languages() {
function background() {
function checkResults(source, results, expected) {
browser.test.assertEq(
expected.length,
results.length,
`got expected number of languages in ${source}`);
results.forEach((lang, index) => {
browser.test.assertEq(
expected[index],
lang,
`got expected language in ${source}`);
});
}
let tabId;
browser.tabs.query({currentWindow: true, active: true}, tabs => {
tabId = tabs[0].id;
browser.test.sendMessage("ready");
});
browser.test.onMessage.addListener(([msg, expected]) => {
Promise.all([
new Promise(
resolve => browser.tabs.sendMessage(tabId, "get-results", resolve)),
browser.i18n.getAcceptLanguages(),
]).then(([contentResults, backgroundResults]) => {
checkResults("contentScript", contentResults, expected);
checkResults("background", backgroundResults, expected);
browser.test.sendMessage("done");
});
});
}
function content() {
browser.runtime.onMessage.addListener((msg, sender, respond) => {
browser.i18n.getAcceptLanguages(respond);
return true;
});
}
let extension = ExtensionTestUtils.loadExtension({
manifest: {
"content_scripts": [{
"matches": ["http://mochi.test/*/file_sample.html"],
"run_at": "document_start",
"js": ["content_script.js"],
}],
},
background: `(${background})()`,
files: {
"content_script.js": `(${content})()`,
},
});
let win = window.open("file_sample.html");
yield extension.startup();
yield extension.awaitMessage("ready");
let expectedLangs = ["en_US", "en"];
extension.sendMessage(["expect-results", expectedLangs]);
yield extension.awaitMessage("done");
expectedLangs = ["en_US", "en", "fr_CA", "fr"];
SpecialPowers.setCharPref("intl.accept_languages", expectedLangs.toString());
extension.sendMessage(["expect-results", expectedLangs]);
yield extension.awaitMessage("done");
SpecialPowers.clearUserPref("intl.accept_languages");
win.close();
yield extension.unload();
});
add_task(function* test_get_ui_language() {
function getResults() {
return {