Bug 1190323: Part 1 - [webext] Fix @@extension_id locale string substitutions. r=billm

This commit is contained in:
Kris Maglione 2016-01-08 16:26:22 -08:00
parent 8e8e10a1d7
commit 779504a2f6
2 changed files with 23 additions and 14 deletions

View File

@ -426,6 +426,8 @@ this.ExtensionData = function(rootURI) {
};
ExtensionData.prototype = {
builtinMessages: null,
get logger() {
let id = this.id || "<unknown>";
return Log.repository.getLogger(LOGGER_ID_BASE + id);
@ -609,6 +611,12 @@ ExtensionData.prototype = {
}
}
this.localeData = new LocaleData({
defaultLocale: this.defaultLocale,
locales,
builtinMessages: this.builtinMessages,
});
return locales;
}.bind(this));
}
@ -622,7 +630,6 @@ ExtensionData.prototype = {
// as returned by |readLocaleFile|.
initAllLocales: Task.async(function* () {
let locales = yield this.promiseLocales();
this.localeData = new LocaleData({ defaultLocale: this.defaultLocale, locales });
yield Promise.all(Array.from(locales.keys(),
locale => this.readLocaleFile(locale)));
@ -651,8 +658,6 @@ ExtensionData.prototype = {
//
// If no locales are unavailable, resolves to |null|.
initLocale: Task.async(function* (locale = this.defaultLocale) {
this.localeData = new LocaleData({ defaultLocale: this.defaultLocale });
if (locale == null) {
return null;
}
@ -970,6 +975,12 @@ Extension.prototype = extend(Object.create(ExtensionData.prototype), {
this.onShutdown.delete(obj);
},
get builtinMessages() {
return new Map([
["@@extension_id", this.uuid],
]);
},
// Reads the locale file for the given Gecko-compatible locale code, or if
// no locale is given, the available locale closest to the UI locale.
// Sets the currently selected locale on success.

View File

@ -113,11 +113,15 @@ function LocaleData(data) {
this.selectedLocale = data.selectedLocale;
this.locales = data.locales || new Map();
// Map(locale-name -> Map(message-key -> localized-strings))
// Map(locale-name -> Map(message-key -> localized-string))
//
// Contains a key for each loaded locale, each of which is a
// Map of message keys to their localized strings.
this.messages = data.messages || new Map();
if (data.builtinMessages) {
this.messages.set(this.BUILTIN, data.builtinMessages);
}
}
LocaleData.prototype = {
@ -132,13 +136,15 @@ LocaleData.prototype = {
};
},
BUILTIN: "@@BUILTIN_MESSAGES",
has(locale) {
return this.messages.has(locale);
},
// https://developer.chrome.com/extensions/i18n
localizeMessage(message, substitutions = [], locale = this.selectedLocale, defaultValue = "??") {
let locales = new Set([locale, this.defaultLocale]
let locales = new Set([this.BUILTIN, locale, this.defaultLocale]
.filter(locale => this.messages.has(locale)));
// Message names are case-insensitive, so normalize them to lower-case.
@ -170,15 +176,7 @@ LocaleData.prototype = {
}
// Check for certain pre-defined messages.
if (message == "@@extension_id") {
if ("uuid" in this) {
// Per Chrome, this isn't available before an ID is guaranteed
// to have been assigned, namely, in manifest files.
// This should only be present in instances of the |Extension|
// subclass.
return this.uuid;
}
} else if (message == "@@ui_locale") {
if (message == "@@ui_locale") {
// Return the browser locale, but convert it to a Chrome-style
// locale code.
return Locale.getLocale().replace(/-/g, "_");