Bug 1357902 - Use improved locale service APIs for localization. r=gandalf

MozReview-Commit-ID: 6Aj0SZkCJwg

--HG--
extra : source : a897a38aad5c4b2dbe7d880d13bf39d079b734a8
extra : amend_source : e929342f2dedf228565095e08cadbb68c1cfb94a
extra : intermediate-source : 9b44720d645bc7f4d9698ecdc0c7ffbf073b36e6
This commit is contained in:
Kris Maglione 2017-05-04 18:28:05 -07:00
Родитель 21f520f36d
Коммит d500662369
11 изменённых файлов: 129 добавлений и 31 удалений

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

@ -22,6 +22,7 @@ support-files =
file_title.html
file_inspectedwindow_reload_target.sjs
file_serviceWorker.html
locale/chrome.manifest
webNav_createdTarget.html
webNav_createdTargetSource.html
webNav_createdTargetSource_subframe.html

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

@ -6,6 +6,18 @@
// This file is imported into the same scope as head.js.
/* import-globals-from head.js */
{
const chromeRegistry = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(Ci.nsIChromeRegistry);
let localeDir = new URL("locale/", gTestPath).href;
let {file} = chromeRegistry.convertChromeURL(Services.io.newURI(localeDir)).QueryInterface(Ci.nsIFileURL);
Components.manager.addBootstrappedManifestLocation(file);
registerCleanupFunction(() => {
Components.manager.removeBootstrappedManifestLocation(file);
});
}
async function runTests(options) {
function background(getTests) {
let tabs;

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

@ -0,0 +1 @@
locale global es-ES resource://gre/chrome/en-US/locale/en-US/global/

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

@ -61,8 +61,6 @@ XPCOMUtils.defineLazyModuleGetter(this, "ExtensionStorage",
"resource://gre/modules/ExtensionStorage.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "ExtensionTestCommon",
"resource://testing-common/ExtensionTestCommon.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Locale",
"resource://gre/modules/Locale.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Log",
"resource://gre/modules/Log.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "MessageChannel",
@ -929,7 +927,7 @@ this.Extension = class extends ExtensionData {
}
parseManifest() {
return StartupCache.manifests.get([this.id, this.version, Locale.getLocale()],
return StartupCache.manifests.get([this.id, this.version, Services.locale.getAppLocaleAsLangTag()],
() => super.parseManifest());
}
@ -1050,12 +1048,12 @@ this.Extension = class extends ExtensionData {
if (locale === undefined) {
let locales = await this.promiseLocales();
let localeList = Array.from(locales.keys(), locale => {
return {name: locale, locales: [locale]};
});
let matches = Services.locale.negotiateLanguages(
Services.locale.getAppLocalesAsLangTags(),
Array.from(locales.keys()),
this.defaultLocale);
let match = Locale.findClosestLocale(localeList);
locale = match ? match.name : this.defaultLocale;
locale = matches[0];
}
return super.initLocale(locale);

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

@ -18,8 +18,6 @@ this.EXPORTED_SYMBOLS = ["ExtensionCommon"];
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Locale",
"resource://gre/modules/Locale.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "MessageChannel",
"resource://gre/modules/MessageChannel.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Preferences",
@ -1242,8 +1240,7 @@ LocaleData.prototype = {
if (message == "@@ui_locale") {
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");
let rtl = Services.locale.isAppLocaleRTL;
if (message == "@@bidi_dir") {
return rtl ? "rtl" : "ltr";
@ -1348,7 +1345,7 @@ LocaleData.prototype = {
get uiLocale() {
// Return the browser locale, but convert it to a Chrome-style
// locale code.
return Locale.getLocale().replace(/-/g, "_");
return Services.locale.getAppLocaleAsBCP47().replace(/-/g, "_");
},
};

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

@ -0,0 +1,2 @@
locale global fr resource://gre/chrome/en-US/locale/en-US/global/
locale global jp resource://gre/chrome/en-US/locale/en-US/global/

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

@ -13,6 +13,7 @@ const BASE_URL = `http://localhost:${server.identity.primaryPort}/data`;
do_register_cleanup(() => {
Preferences.reset("intl.accept_languages");
Preferences.reset("intl.locale.matchOS");
Preferences.reset("general.useragent.locale");
});
@ -161,6 +162,82 @@ add_task(async function test_i18n() {
await extension.unload();
});
add_task(async function test_i18n_negotiation() {
function runTests(expected) {
let _ = browser.i18n.getMessage.bind(browser.i18n);
browser.test.assertEq(expected, _("foo"), "Got expected message");
}
let extensionData = {
manifest: {
"default_locale": "en_US",
content_scripts: [
{"matches": ["http://*/*/file_sample.html"],
"js": ["content.js"]},
],
},
files: {
"_locales/en_US/messages.json": {
"foo": {
"message": "English.",
"description": "foo",
},
},
"_locales/jp/messages.json": {
"foo": {
"message": "\u65e5\u672c\u8a9e",
"description": "foo",
},
},
"content.js": "new " + function(runTestsFn) {
browser.test.onMessage.addListener(expected => {
runTestsFn(expected);
browser.test.sendMessage("content-script-finished");
});
browser.test.sendMessage("content-ready");
} + `(${runTests})`,
},
background: "new " + function(runTestsFn) {
browser.test.onMessage.addListener(expected => {
runTestsFn(expected);
browser.test.sendMessage("background-script-finished");
});
} + `(${runTests})`,
};
Components.manager.addBootstrappedManifestLocation(do_get_file("data/locales/"));
let contentPage = await ExtensionTestUtils.loadContentPage(`${BASE_URL}/file_sample.html`);
Preferences.set("intl.locale.matchOS", false);
for (let [lang, msg] of [["en-US", "English."], ["jp", "\u65e5\u672c\u8a9e"]]) {
Preferences.set("general.useragent.locale", lang);
let extension = ExtensionTestUtils.loadExtension(extensionData);
await extension.startup();
await extension.awaitMessage("content-ready");
extension.sendMessage(msg);
await extension.awaitMessage("background-script-finished");
await extension.awaitMessage("content-script-finished");
await extension.unload();
}
Preferences.reset("general.useragent.locale");
await contentPage.close();
});
add_task(async function test_get_accept_languages() {
function checkResults(source, results, expected) {
browser.test.assertEq(
@ -298,12 +375,15 @@ add_task(async function test_get_ui_language() {
await extension.awaitMessage("background-done");
await extension.awaitMessage("content-done");
Preferences.set("general.useragent.locale", "he");
// We don't currently have a good way to mock this.
if (false) {
Preferences.set("general.useragent.locale", "he");
extension.sendMessage(["expect-results", "he"]);
extension.sendMessage(["expect-results", "he"]);
await extension.awaitMessage("background-done");
await extension.awaitMessage("content-done");
await extension.awaitMessage("background-done");
await extension.awaitMessage("content-done");
}
await contentPage.close();

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

@ -98,23 +98,26 @@ add_task(async function test_i18n_css() {
css = await fetch(cssURL);
equal(css, '* { content: "en_US ltr rtl left right" }', "CSS file localized in mochitest scope");
const LOCALE = "general.useragent.locale";
const DIR = "intl.uidirection";
const DIR_LEGACY = "intl.uidirection.en"; // Needed for Android until bug 1215247 is resolved
// We don't currently have a good way to mock this.
if (false) {
const LOCALE = "general.useragent.locale";
const DIR = "intl.uidirection";
const DIR_LEGACY = "intl.uidirection.en"; // Needed for Android until bug 1215247 is resolved
// We don't wind up actually switching the chrome registry locale, since we
// don't have a chrome package for Hebrew. So just override it, and force
// RTL directionality.
Preferences.set(LOCALE, "he");
Preferences.set(DIR, 1);
Preferences.set(DIR_LEGACY, "rtl");
// We don't wind up actually switching the chrome registry locale, since we
// don't have a chrome package for Hebrew. So just override it, and force
// RTL directionality.
Preferences.set(LOCALE, "he");
Preferences.set(DIR, 1);
Preferences.set(DIR_LEGACY, "rtl");
css = await fetch(cssURL);
equal(css, '* { content: "he rtl ltr right left" }', "CSS file localized in mochitest scope");
css = await fetch(cssURL);
equal(css, '* { content: "he rtl ltr right left" }', "CSS file localized in mochitest scope");
Preferences.reset(LOCALE);
Preferences.reset(DIR);
Preferences.reset(DIR_LEGACY);
Preferences.reset(LOCALE);
Preferences.reset(DIR);
Preferences.reset(DIR_LEGACY);
}
await extension.awaitFinish("i18n-css");
await extension.unload();

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

@ -61,6 +61,7 @@ add_task(async function() {
return extension.awaitMessage("manifest");
}
Components.manager.addBootstrappedManifestLocation(do_get_file("data/locales/"));
await extension.startup();
@ -79,6 +80,7 @@ add_task(async function() {
do_print("Change locale to 'fr' and restart");
Preferences.set("intl.locale.matchOS", false);
Preferences.set("general.useragent.locale", "fr");
await AddonTestUtils.promiseRestartManager();
await extension.awaitStartup();

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

@ -54,6 +54,7 @@ skip-if = true # This test no longer tests what it is meant to test.
[test_ext_shutdown_cleanup.js]
[test_ext_simple.js]
[test_ext_startup_cache.js]
skip-if = os == "android"
[test_ext_storage.js]
[test_ext_storage_sync.js]
head = head.js head_sync.js

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

@ -1,4 +1,5 @@
[test_ext_i18n.js]
skip-if = os == "android"
[test_ext_i18n_css.js]
[test_ext_contentscript.js]
[test_ext_contentscript_xrays.js]