mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-25 05:41:12 +00:00
Bug 1357902 - Use improved locale service APIs for localization. r=gandalf
MozReview-Commit-ID: 6Aj0SZkCJwg --HG-- extra : rebase_source : 3d38444d85dea47c70c0c4199f5d0d1877eea081
This commit is contained in:
parent
d6df91fa28
commit
4772055bc5
@ -21,6 +21,7 @@ support-files =
|
||||
file_dummy.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",
|
||||
@ -907,7 +905,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());
|
||||
}
|
||||
|
||||
@ -1028,12 +1026,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/
|
@ -161,6 +161,81 @@ 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`);
|
||||
|
||||
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(
|
||||
@ -294,12 +369,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();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user