mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-12-02 01:48:05 +00:00
Bug 1385416 - Remove Locale.jsm. r=kmag
MozReview-Commit-ID: IeSJKWuIQ2c --HG-- extra : rebase_source : cfd14d21487070a5fa6583796e3f97f62d836f33
This commit is contained in:
parent
55ea239b11
commit
929712ae1b
@ -1,81 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
this.EXPORTED_SYMBOLS = ["Locale"];
|
||||
|
||||
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
||||
|
||||
Cu.import("resource://gre/modules/Services.jsm");
|
||||
|
||||
this.Locale = {
|
||||
/**
|
||||
* Gets the currently selected locale for display.
|
||||
* @return the selected locale or "en-US" if none is selected
|
||||
*/
|
||||
getLocale() {
|
||||
return Services.locale.getRequestedLocale() || "en-US";
|
||||
},
|
||||
|
||||
/**
|
||||
* Selects the closest matching locale from a list of locales.
|
||||
*
|
||||
* @param aLocales
|
||||
* An array of locales
|
||||
* @return the best match for the currently selected locale
|
||||
*/
|
||||
findClosestLocale(aLocales) {
|
||||
let appLocale = this.getLocale();
|
||||
|
||||
// Holds the best matching localized resource
|
||||
var bestmatch = null;
|
||||
// The number of locale parts it matched with
|
||||
var bestmatchcount = 0;
|
||||
// The number of locale parts in the match
|
||||
var bestpartcount = 0;
|
||||
|
||||
var matchLocales = [appLocale.toLowerCase()];
|
||||
/* If the current locale is English then it will find a match if there is
|
||||
a valid match for en-US so no point searching that locale too. */
|
||||
if (matchLocales[0].substring(0, 3) != "en-")
|
||||
matchLocales.push("en-us");
|
||||
|
||||
for (let locale of matchLocales) {
|
||||
var lparts = locale.split("-");
|
||||
for (let localized of aLocales) {
|
||||
for (let found of localized.locales) {
|
||||
found = found.toLowerCase();
|
||||
// Exact match is returned immediately
|
||||
if (locale == found)
|
||||
return localized;
|
||||
|
||||
var fparts = found.split("-");
|
||||
/* If we have found a possible match and this one isn't any longer
|
||||
then we dont need to check further. */
|
||||
if (bestmatch && fparts.length < bestmatchcount)
|
||||
continue;
|
||||
|
||||
// Count the number of parts that match
|
||||
var maxmatchcount = Math.min(fparts.length, lparts.length);
|
||||
var matchcount = 0;
|
||||
while (matchcount < maxmatchcount &&
|
||||
fparts[matchcount] == lparts[matchcount])
|
||||
matchcount++;
|
||||
|
||||
/* If we matched more than the last best match or matched the same and
|
||||
this locale is less specific than the last best match. */
|
||||
if (matchcount > bestmatchcount ||
|
||||
(matchcount == bestmatchcount && fparts.length < bestpartcount)) {
|
||||
bestmatch = localized;
|
||||
bestmatchcount = matchcount;
|
||||
bestpartcount = fparts.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
// If we found a valid match for this locale return it
|
||||
if (bestmatch)
|
||||
return bestmatch;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
};
|
@ -94,9 +94,6 @@ with Files('LightweightThemeConsumer.jsm'):
|
||||
with Files('LoadContextInfo.jsm'):
|
||||
BUG_COMPONENT = ('Core', 'Networking: Cache')
|
||||
|
||||
with Files('Locale.jsm'):
|
||||
BUG_COMPONENT = ('Core', 'Internationalization')
|
||||
|
||||
with Files('Memory.jsm'):
|
||||
BUG_COMPONENT = ('Core', 'DOM: Content Processes')
|
||||
|
||||
@ -207,7 +204,6 @@ EXTRA_JS_MODULES += [
|
||||
'Integration.jsm',
|
||||
'JSONFile.jsm',
|
||||
'LoadContextInfo.jsm',
|
||||
'Locale.jsm',
|
||||
'Log.jsm',
|
||||
'Memory.jsm',
|
||||
'NewTabUtils.jsm',
|
||||
|
@ -36,8 +36,6 @@ XPCOMUtils.defineLazyModuleGetter(this, "ChromeManifestParser",
|
||||
"resource://gre/modules/ChromeManifestParser.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "ExtensionData",
|
||||
"resource://gre/modules/Extension.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "Locale",
|
||||
"resource://gre/modules/Locale.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
|
||||
"resource://gre/modules/FileUtils.jsm");
|
||||
XPCOMUtils.defineLazyGetter(this, "IconDetails", () => {
|
||||
|
@ -27,8 +27,6 @@ XPCOMUtils.defineLazyModuleGetter(this, "ChromeManifestParser",
|
||||
"resource://gre/modules/ChromeManifestParser.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "LightweightThemeManager",
|
||||
"resource://gre/modules/LightweightThemeManager.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "Locale",
|
||||
"resource://gre/modules/Locale.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
|
||||
"resource://gre/modules/FileUtils.jsm");
|
||||
XPCOMUtils.defineLazyModuleGetter(this, "ZipUtils",
|
||||
@ -4811,8 +4809,49 @@ AddonInternal.prototype = {
|
||||
get selectedLocale() {
|
||||
if (this._selectedLocale)
|
||||
return this._selectedLocale;
|
||||
let locale = Locale.findClosestLocale(this.locales);
|
||||
this._selectedLocale = locale ? locale : this.defaultLocale;
|
||||
|
||||
/**
|
||||
* this.locales is a list of objects that have property `locales`.
|
||||
* It's value is an array of locale codes.
|
||||
*
|
||||
* First, we reduce this nested structure to a flat list of locale codes.
|
||||
*/
|
||||
const locales = [].concat(...this.locales.map(loc => loc.locales));
|
||||
|
||||
let requestedLocales = Services.locale.getRequestedLocales();
|
||||
|
||||
/**
|
||||
* If en is not the top locale, add "en-US" to the list.
|
||||
*/
|
||||
if (!requestedLocales[0].startsWith("en")) {
|
||||
requestedLocales.push("en-US");
|
||||
}
|
||||
|
||||
/**
|
||||
* Then we negotiate best locale code matching the app locales.
|
||||
*/
|
||||
let bestLocale = Services.locale.negotiateLanguages(
|
||||
requestedLocales,
|
||||
locales,
|
||||
"und",
|
||||
Services.locale.langNegStrategyLookup
|
||||
)[0];
|
||||
|
||||
/**
|
||||
* If no match has been found, we'll assign the default locale as
|
||||
* the selected one.
|
||||
*/
|
||||
if (bestLocale === "und") {
|
||||
this._selectedLocale = this.defaultLocale;
|
||||
} else {
|
||||
/**
|
||||
* Otherwise, we'll go through all locale entries looking for the one
|
||||
* that has the best match in it's locales list.
|
||||
*/
|
||||
this._selectedLocale = this.locales.find(loc =>
|
||||
loc.locales.includes(bestLocale));
|
||||
}
|
||||
|
||||
return this._selectedLocale;
|
||||
},
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user