зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1347306 - Hand over language negotiation from ChromeRegistry to LocaleService. r=jfkthame
MozReview-Commit-ID: RIPZUHN4LW --HG-- extra : rebase_source : 3011d3c9f1887c9943d6636ea959bb643644bd3e
This commit is contained in:
Родитель
6109edd9bd
Коммит
ca25902077
|
@ -487,7 +487,6 @@ nsChromeRegistry::FlushAllCaches()
|
|||
NS_IMETHODIMP
|
||||
nsChromeRegistry::ReloadChrome()
|
||||
{
|
||||
UpdateSelectedLocale();
|
||||
FlushAllCaches();
|
||||
// Do a reload of all top level windows.
|
||||
nsresult rv = NS_OK;
|
||||
|
|
|
@ -79,10 +79,6 @@ protected:
|
|||
void FlushSkinCaches();
|
||||
void FlushAllCaches();
|
||||
|
||||
// Update the selected locale used by the chrome registry, and fire a
|
||||
// notification about this change
|
||||
virtual nsresult UpdateSelectedLocale() = 0;
|
||||
|
||||
static void LogMessage(const char* aMsg, ...)
|
||||
MOZ_FORMAT_PRINTF(1, 2);
|
||||
static void LogMessageWithContext(nsIURI* aURL, uint32_t aLineNumber, uint32_t flags,
|
||||
|
@ -95,8 +91,6 @@ protected:
|
|||
virtual nsresult GetFlagsFromPackage(const nsCString& aPackage,
|
||||
uint32_t* aFlags) = 0;
|
||||
|
||||
nsresult SelectLocaleFromPref(nsIPrefBranch* prefs);
|
||||
|
||||
static nsresult RefreshWindow(nsPIDOMWindowOuter* aWindow);
|
||||
static nsresult GetProviderAndPath(nsIURL* aChromeURL,
|
||||
nsACString& aProvider, nsACString& aPath);
|
||||
|
|
|
@ -28,8 +28,6 @@
|
|||
#include "mozilla/Unused.h"
|
||||
#include "mozilla/intl/LocaleService.h"
|
||||
|
||||
#include "nsICommandLine.h"
|
||||
#include "nsILocaleService.h"
|
||||
#include "nsIObserverService.h"
|
||||
#include "nsIPrefBranch.h"
|
||||
#include "nsIPrefService.h"
|
||||
|
@ -39,16 +37,13 @@
|
|||
#include "nsIXPConnect.h"
|
||||
#include "nsIXULRuntime.h"
|
||||
|
||||
#define UILOCALE_CMD_LINE_ARG "UILocale"
|
||||
|
||||
#define MATCH_OS_LOCALE_PREF "intl.locale.matchOS"
|
||||
#define SELECTED_LOCALE_PREF "general.useragent.locale"
|
||||
#define SELECTED_SKIN_PREF "general.skins.selectedSkin"
|
||||
#define PACKAGE_OVERRIDE_BRANCH "chrome.override_package."
|
||||
|
||||
using namespace mozilla;
|
||||
using mozilla::dom::ContentParent;
|
||||
using mozilla::dom::PContentParent;
|
||||
using mozilla::intl::LocaleService;
|
||||
|
||||
// We use a "best-fit" algorithm for matching locales and themes.
|
||||
// 1) the exact selected locale/theme
|
||||
|
@ -113,7 +108,6 @@ nsChromeRegistryChrome::Init()
|
|||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
|
||||
mSelectedLocale = NS_LITERAL_CSTRING("en-US");
|
||||
mSelectedSkin = NS_LITERAL_CSTRING("classic/1.0");
|
||||
|
||||
bool safeMode = false;
|
||||
|
@ -140,17 +134,13 @@ nsChromeRegistryChrome::Init()
|
|||
if (NS_SUCCEEDED(rv))
|
||||
mSelectedSkin = provider;
|
||||
|
||||
SelectLocaleFromPref(prefs);
|
||||
|
||||
rv = prefs->AddObserver(MATCH_OS_LOCALE_PREF, this, true);
|
||||
rv = prefs->AddObserver(SELECTED_LOCALE_PREF, this, true);
|
||||
rv = prefs->AddObserver(SELECTED_SKIN_PREF, this, true);
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIObserverService> obsService = mozilla::services::GetObserverService();
|
||||
if (obsService) {
|
||||
obsService->AddObserver(this, "command-line-startup", true);
|
||||
obsService->AddObserver(this, "profile-initial-state", true);
|
||||
obsService->AddObserver(this, "intl:app-locales-changed", true);
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
|
@ -203,22 +193,6 @@ nsChromeRegistryChrome::GetLocalesForPackage(const nsACString& aPackage,
|
|||
return rv;
|
||||
}
|
||||
|
||||
static nsresult
|
||||
getUILangCountry(nsACString& aUILang)
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
nsCOMPtr<nsILocaleService> localeService = do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
nsAutoString uiLang;
|
||||
rv = localeService->GetLocaleComponentForUserAgent(uiLang);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
CopyUTF16toUTF8(uiLang, aUILang);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsChromeRegistryChrome::IsLocaleRTL(const nsACString& package, bool *aResult)
|
||||
{
|
||||
|
@ -233,11 +207,27 @@ nsChromeRegistryChrome::IsLocaleRTL(const nsACString& package, bool *aResult)
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method negotiates only between the app locale and the available
|
||||
* chrome packages.
|
||||
*
|
||||
* If you want to get the current application's UI locale, please use
|
||||
* LocaleService::GetAppLocaleAsLangTag.
|
||||
*/
|
||||
nsresult
|
||||
nsChromeRegistryChrome::GetSelectedLocale(const nsACString& aPackage,
|
||||
bool aAsBCP47,
|
||||
nsACString& aLocale)
|
||||
{
|
||||
nsAutoCString reqLocale;
|
||||
if (aPackage.Equals("global")) {
|
||||
LocaleService::GetInstance()->GetAppLocaleAsLangTag(reqLocale);
|
||||
} else {
|
||||
AutoTArray<nsCString, 10> requestedLocales;
|
||||
LocaleService::GetInstance()->GetRequestedLocales(requestedLocales);
|
||||
reqLocale.Assign(requestedLocales[0]);
|
||||
}
|
||||
|
||||
nsCString realpackage;
|
||||
nsresult rv = OverrideLocalePackage(aPackage, realpackage);
|
||||
if (NS_FAILED(rv))
|
||||
|
@ -246,7 +236,7 @@ nsChromeRegistryChrome::GetSelectedLocale(const nsACString& aPackage,
|
|||
if (!mPackagesHash.Get(realpackage, &entry))
|
||||
return NS_ERROR_FILE_NOT_FOUND;
|
||||
|
||||
aLocale = entry->locales.GetSelected(mSelectedLocale, nsProviderArray::LOCALE);
|
||||
aLocale = entry->locales.GetSelected(reqLocale, nsProviderArray::LOCALE);
|
||||
if (aLocale.IsEmpty())
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
|
@ -272,34 +262,6 @@ nsChromeRegistryChrome::OverrideLocalePackage(const nsACString& aPackage,
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsChromeRegistryChrome::SelectLocaleFromPref(nsIPrefBranch* prefs)
|
||||
{
|
||||
nsresult rv;
|
||||
bool matchOSLocale = false;
|
||||
rv = prefs->GetBoolPref(MATCH_OS_LOCALE_PREF, &matchOSLocale);
|
||||
|
||||
if (NS_SUCCEEDED(rv) && matchOSLocale) {
|
||||
// compute lang and region code only when needed!
|
||||
nsAutoCString uiLocale;
|
||||
rv = getUILangCountry(uiLocale);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
mSelectedLocale = uiLocale;
|
||||
}
|
||||
else {
|
||||
nsXPIDLCString provider;
|
||||
rv = prefs->GetCharPref(SELECTED_LOCALE_PREF, getter_Copies(provider));
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
mSelectedLocale = provider;
|
||||
}
|
||||
}
|
||||
|
||||
if (NS_FAILED(rv))
|
||||
NS_ERROR("Couldn't select locale from pref!");
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsChromeRegistryChrome::Observe(nsISupports *aSubject, const char *aTopic,
|
||||
const char16_t *someData)
|
||||
|
@ -312,13 +274,7 @@ nsChromeRegistryChrome::Observe(nsISupports *aSubject, const char *aTopic,
|
|||
|
||||
NS_ConvertUTF16toUTF8 pref(someData);
|
||||
|
||||
if (pref.EqualsLiteral(MATCH_OS_LOCALE_PREF) ||
|
||||
pref.EqualsLiteral(SELECTED_LOCALE_PREF)) {
|
||||
rv = UpdateSelectedLocale();
|
||||
if (NS_SUCCEEDED(rv) && mProfileLoaded)
|
||||
FlushAllCaches();
|
||||
}
|
||||
else if (pref.EqualsLiteral(SELECTED_SKIN_PREF)) {
|
||||
if (pref.EqualsLiteral(SELECTED_SKIN_PREF)) {
|
||||
nsXPIDLCString provider;
|
||||
rv = prefs->GetCharPref(pref.get(), getter_Copies(provider));
|
||||
if (NS_FAILED(rv)) {
|
||||
|
@ -332,24 +288,14 @@ nsChromeRegistryChrome::Observe(nsISupports *aSubject, const char *aTopic,
|
|||
NS_ERROR("Unexpected pref!");
|
||||
}
|
||||
}
|
||||
else if (!strcmp("command-line-startup", aTopic)) {
|
||||
nsCOMPtr<nsICommandLine> cmdLine (do_QueryInterface(aSubject));
|
||||
if (cmdLine) {
|
||||
nsAutoString uiLocale;
|
||||
rv = cmdLine->HandleFlagWithParam(NS_LITERAL_STRING(UILOCALE_CMD_LINE_ARG),
|
||||
false, uiLocale);
|
||||
if (NS_SUCCEEDED(rv) && !uiLocale.IsEmpty()) {
|
||||
CopyUTF16toUTF8(uiLocale, mSelectedLocale);
|
||||
nsCOMPtr<nsIPrefBranch> prefs (do_GetService(NS_PREFSERVICE_CONTRACTID));
|
||||
if (prefs) {
|
||||
prefs->RemoveObserver(SELECTED_LOCALE_PREF, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (!strcmp("profile-initial-state", aTopic)) {
|
||||
mProfileLoaded = true;
|
||||
}
|
||||
else if (!strcmp("intl:app-locales-changed", aTopic)) {
|
||||
if (mProfileLoaded) {
|
||||
FlushAllCaches();
|
||||
}
|
||||
}
|
||||
else {
|
||||
NS_ERROR("Unexpected observer topic!");
|
||||
}
|
||||
|
@ -375,25 +321,6 @@ nsChromeRegistryChrome::CheckForNewChrome()
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult nsChromeRegistryChrome::UpdateSelectedLocale()
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
|
||||
if (prefs) {
|
||||
rv = SelectLocaleFromPref(prefs);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
nsCOMPtr<nsIObserverService> obsSvc =
|
||||
mozilla::services::GetObserverService();
|
||||
NS_ASSERTION(obsSvc, "Couldn't get observer service.");
|
||||
obsSvc->NotifyObservers((nsIChromeRegistry*) this,
|
||||
"selected-locale-has-changed", nullptr);
|
||||
mozilla::intl::LocaleService::GetInstance()->Refresh();
|
||||
}
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
static void
|
||||
SerializeURI(nsIURI* aURI,
|
||||
SerializedURI& aSerializedURI)
|
||||
|
@ -416,7 +343,7 @@ nsChromeRegistryChrome::SendRegisteredChrome(
|
|||
for (auto iter = mPackagesHash.Iter(); !iter.Done(); iter.Next()) {
|
||||
ChromePackage chromePackage;
|
||||
ChromePackageFromPackageEntry(iter.Key(), iter.UserData(), &chromePackage,
|
||||
mSelectedLocale, mSelectedSkin);
|
||||
mSelectedSkin);
|
||||
packages.AppendElement(chromePackage);
|
||||
}
|
||||
|
||||
|
@ -447,9 +374,12 @@ nsChromeRegistryChrome::SendRegisteredChrome(
|
|||
overrides.AppendElement(override);
|
||||
}
|
||||
|
||||
nsAutoCString appLocale;
|
||||
LocaleService::GetInstance()->GetAppLocaleAsLangTag(appLocale);
|
||||
|
||||
if (aParent) {
|
||||
bool success = aParent->SendRegisterChrome(packages, resources, overrides,
|
||||
mSelectedLocale, false);
|
||||
appLocale, false);
|
||||
NS_ENSURE_TRUE_VOID(success);
|
||||
} else {
|
||||
nsTArray<ContentParent*> parents;
|
||||
|
@ -460,7 +390,7 @@ nsChromeRegistryChrome::SendRegisteredChrome(
|
|||
for (uint32_t i = 0; i < parents.Length(); i++) {
|
||||
DebugOnly<bool> success =
|
||||
parents[i]->SendRegisterChrome(packages, resources, overrides,
|
||||
mSelectedLocale, true);
|
||||
appLocale, true);
|
||||
NS_WARNING_ASSERTION(success,
|
||||
"couldn't reset a child's registered chrome");
|
||||
}
|
||||
|
@ -471,12 +401,13 @@ nsChromeRegistryChrome::SendRegisteredChrome(
|
|||
nsChromeRegistryChrome::ChromePackageFromPackageEntry(const nsACString& aPackageName,
|
||||
PackageEntry* aPackage,
|
||||
ChromePackage* aChromePackage,
|
||||
const nsCString& aSelectedLocale,
|
||||
const nsCString& aSelectedSkin)
|
||||
{
|
||||
nsAutoCString appLocale;
|
||||
LocaleService::GetInstance()->GetAppLocaleAsLangTag(appLocale);
|
||||
|
||||
SerializeURI(aPackage->baseURI, aChromePackage->contentBaseURI);
|
||||
SerializeURI(aPackage->locales.GetBase(aSelectedLocale,
|
||||
nsProviderArray::LOCALE),
|
||||
SerializeURI(aPackage->locales.GetBase(appLocale, nsProviderArray::LOCALE),
|
||||
aChromePackage->localeBaseURI);
|
||||
SerializeURI(aPackage->skins.GetBase(aSelectedSkin, nsProviderArray::ANY),
|
||||
aChromePackage->skinBaseURI);
|
||||
|
@ -511,7 +442,9 @@ nsChromeRegistryChrome::GetBaseURIFromPackage(const nsCString& aPackage,
|
|||
}
|
||||
|
||||
if (aProvider.EqualsLiteral("locale")) {
|
||||
return entry->locales.GetBase(mSelectedLocale, nsProviderArray::LOCALE);
|
||||
nsAutoCString appLocale;
|
||||
LocaleService::GetInstance()->GetAppLocaleAsLangTag(appLocale);
|
||||
return entry->locales.GetBase(appLocale, nsProviderArray::LOCALE);
|
||||
}
|
||||
else if (aProvider.EqualsLiteral("skin")) {
|
||||
return entry->skins.GetBase(mSelectedSkin, nsProviderArray::ANY);
|
||||
|
@ -764,7 +697,7 @@ nsChromeRegistryChrome::ManifestContent(ManifestProcessingContext& cx, int linen
|
|||
if (mDynamicRegistration) {
|
||||
ChromePackage chromePackage;
|
||||
ChromePackageFromPackageEntry(packageName, entry, &chromePackage,
|
||||
mSelectedLocale, mSelectedSkin);
|
||||
mSelectedSkin);
|
||||
SendManifestEntry(chromePackage);
|
||||
}
|
||||
}
|
||||
|
@ -800,9 +733,15 @@ nsChromeRegistryChrome::ManifestLocale(ManifestProcessingContext& cx, int lineno
|
|||
if (mDynamicRegistration) {
|
||||
ChromePackage chromePackage;
|
||||
ChromePackageFromPackageEntry(packageName, entry, &chromePackage,
|
||||
mSelectedLocale, mSelectedSkin);
|
||||
mSelectedSkin);
|
||||
SendManifestEntry(chromePackage);
|
||||
}
|
||||
|
||||
if (strcmp(package, "global") == 0) {
|
||||
// We should refresh the LocaleService, since the available
|
||||
// locales changed.
|
||||
LocaleService::GetInstance()->Refresh();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -836,7 +775,7 @@ nsChromeRegistryChrome::ManifestSkin(ManifestProcessingContext& cx, int lineno,
|
|||
if (mDynamicRegistration) {
|
||||
ChromePackage chromePackage;
|
||||
ChromePackageFromPackageEntry(packageName, entry, &chromePackage,
|
||||
mSelectedLocale, mSelectedSkin);
|
||||
mSelectedSkin);
|
||||
SendManifestEntry(chromePackage);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,13 +58,10 @@ class nsChromeRegistryChrome : public nsChromeRegistry
|
|||
static void ChromePackageFromPackageEntry(const nsACString& aPackageName,
|
||||
PackageEntry* aPackage,
|
||||
ChromePackage* aChromePackage,
|
||||
const nsCString& aSelectedLocale,
|
||||
const nsCString& aSelectedSkin);
|
||||
|
||||
nsresult OverrideLocalePackage(const nsACString& aPackage,
|
||||
nsACString& aOverride);
|
||||
nsresult SelectLocaleFromPref(nsIPrefBranch* prefs);
|
||||
nsresult UpdateSelectedLocale() override;
|
||||
nsIURI* GetBaseURIFromPackage(const nsCString& aPackage,
|
||||
const nsCString& aProvider,
|
||||
const nsCString& aPath) override;
|
||||
|
@ -157,7 +154,6 @@ class nsChromeRegistryChrome : public nsChromeRegistry
|
|||
bool mProfileLoaded;
|
||||
bool mDynamicRegistration;
|
||||
|
||||
nsCString mSelectedLocale;
|
||||
nsCString mSelectedSkin;
|
||||
|
||||
// Hash of package names ("global") to PackageEntry objects
|
||||
|
|
|
@ -257,11 +257,6 @@ nsChromeRegistryContent::GetXULOverlays(nsIURI *aChromeURL,
|
|||
CONTENT_NOT_IMPLEMENTED();
|
||||
}
|
||||
|
||||
nsresult nsChromeRegistryContent::UpdateSelectedLocale()
|
||||
{
|
||||
CONTENT_NOT_IMPLEMENTED();
|
||||
}
|
||||
|
||||
void
|
||||
nsChromeRegistryContent::ManifestContent(ManifestProcessingContext& cx,
|
||||
int lineno, char *const * argv,
|
||||
|
|
|
@ -56,7 +56,6 @@ class nsChromeRegistryContent : public nsChromeRegistry
|
|||
uint32_t flags;
|
||||
};
|
||||
|
||||
nsresult UpdateSelectedLocale() override;
|
||||
nsIURI* GetBaseURIFromPackage(const nsCString& aPackage,
|
||||
const nsCString& aProvider,
|
||||
const nsCString& aPath) override;
|
||||
|
|
|
@ -1,94 +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/.
|
||||
*/
|
||||
|
||||
var MANIFESTS = [
|
||||
do_get_file("data/test_bug519468.manifest")
|
||||
];
|
||||
|
||||
Components.utils.import("resource://testing-common/MockRegistrar.jsm");
|
||||
// Stub in the locale service so we can control what gets returned as the OS locale setting
|
||||
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
|
||||
var stubOSLocale = null;
|
||||
|
||||
StubLocaleService = {
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsILocaleService, Ci.nsISupports]),
|
||||
|
||||
getLocaleComponentForUserAgent: function SLS_getLocaleComponentForUserAgent()
|
||||
{
|
||||
return stubOSLocale;
|
||||
}
|
||||
}
|
||||
|
||||
MockRegistrar.register("@mozilla.org/intl/nslocaleservice;1", StubLocaleService);
|
||||
|
||||
// Now fire up the test
|
||||
do_test_pending()
|
||||
registerManifests(MANIFESTS);
|
||||
|
||||
var chromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"]
|
||||
.getService(Ci.nsIXULChromeRegistry)
|
||||
.QueryInterface(Ci.nsIToolkitChromeRegistry);
|
||||
chromeReg.checkForNewChrome();
|
||||
|
||||
var prefService = Cc["@mozilla.org/preferences-service;1"]
|
||||
.getService(Ci.nsIPrefService)
|
||||
.QueryInterface(Ci.nsIPrefBranch);
|
||||
var os = Cc["@mozilla.org/observer-service;1"]
|
||||
.getService(Ci.nsIObserverService);
|
||||
|
||||
var testsLocale = [
|
||||
// These tests cover when the OS local is included in our manifest
|
||||
{matchOS: false, selected: "en-US", osLocale: "xx-AA", locale: "en-US"},
|
||||
{matchOS: true, selected: "en-US", osLocale: "xx-AA", locale: "xx-AA"},
|
||||
{matchOS: false, selected: "fr-FR", osLocale: "xx-AA", locale: "fr-FR"},
|
||||
{matchOS: true, selected: "fr-FR", osLocale: "xx-AA", locale: "xx-AA"},
|
||||
{matchOS: false, selected: "de-DE", osLocale: "xx-AA", locale: "de-DE"},
|
||||
{matchOS: true, selected: "de-DE", osLocale: "xx-AA", locale: "xx-AA"},
|
||||
// these tests cover the case where the OS locale is not available in our manifest, but the
|
||||
// base language is (ie, substitute xx-AA which we have for xx-BB which we don't)
|
||||
{matchOS: false, selected: "en-US", osLocale: "xx-BB", locale: "en-US"},
|
||||
{matchOS: true, selected: "en-US", osLocale: "xx-BB", locale: "xx-AA"},
|
||||
{matchOS: false, selected: "fr-FR", osLocale: "xx-BB", locale: "fr-FR"},
|
||||
{matchOS: true, selected: "fr-FR", osLocale: "xx-BB", locale: "xx-AA"},
|
||||
// These tests cover where the language is not available
|
||||
{matchOS: false, selected: "en-US", osLocale: "xy-BB", locale: "en-US"},
|
||||
{matchOS: true, selected: "en-US", osLocale: "xy-BB", locale: "en-US"},
|
||||
{matchOS: false, selected: "fr-FR", osLocale: "xy-BB", locale: "fr-FR"},
|
||||
{matchOS: true, selected: "fr-FR", osLocale: "xy-BB", locale: "en-US"},
|
||||
];
|
||||
|
||||
var observedLocale = null;
|
||||
|
||||
function test_locale(aTest) {
|
||||
observedLocale = null;
|
||||
|
||||
stubOSLocale = aTest.osLocale;
|
||||
prefService.setBoolPref("intl.locale.matchOS", aTest.matchOS);
|
||||
prefService.setCharPref("general.useragent.locale", aTest.selected);
|
||||
|
||||
chromeReg.reloadChrome();
|
||||
|
||||
do_check_eq(observedLocale, aTest.locale);
|
||||
}
|
||||
|
||||
// Callback function for observing locale change. May be called more than once
|
||||
// per test iteration.
|
||||
function checkValidity() {
|
||||
observedLocale = chromeReg.getSelectedLocale("testmatchos");
|
||||
dump("checkValidity called back with locale = " + observedLocale + "\n");
|
||||
}
|
||||
|
||||
function run_test() {
|
||||
os.addObserver(checkValidity, "selected-locale-has-changed", false);
|
||||
|
||||
for (let count = 0 ; count < testsLocale.length ; count++) {
|
||||
dump("count = " + count + " " + testsLocale[count].toSource() + "\n");
|
||||
test_locale(testsLocale[count]);
|
||||
}
|
||||
|
||||
os.removeObserver(checkValidity, "selected-locale-has-changed");
|
||||
do_test_finished();
|
||||
}
|
|
@ -9,7 +9,6 @@ support-files = data/**
|
|||
[test_bug399707.js]
|
||||
[test_bug401153.js]
|
||||
[test_bug415367.js]
|
||||
[test_bug519468.js]
|
||||
[test_bug564667.js]
|
||||
tags = addons
|
||||
[test_bug848297.js]
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include "nsIObserverService.h"
|
||||
#include "nsStringEnumerator.h"
|
||||
#include "nsIToolkitChromeRegistry.h"
|
||||
#include "nsXULAppAPI.h"
|
||||
|
||||
#ifdef ENABLE_INTL_API
|
||||
#include "unicode/uloc.h"
|
||||
|
@ -76,24 +77,39 @@ static void SanitizeForBCP47(nsACString& aLocale)
|
|||
* Currently it collects the locale ID used by nsChromeRegistry and
|
||||
* adds hardcoded "en-US" locale as a fallback.
|
||||
*/
|
||||
static void
|
||||
ReadAppLocales(nsTArray<nsCString>& aRetVal)
|
||||
void
|
||||
LocaleService::NegotiateAppLocales(nsTArray<nsCString>& aRetVal)
|
||||
{
|
||||
nsAutoCString defaultLocale;
|
||||
GetDefaultLocale(defaultLocale);
|
||||
|
||||
if (XRE_IsParentProcess()) {
|
||||
AutoTArray<nsCString, 100> availableLocales;
|
||||
AutoTArray<nsCString, 10> requestedLocales;
|
||||
GetAvailableLocales(availableLocales);
|
||||
GetRequestedLocales(requestedLocales);
|
||||
|
||||
NegotiateLanguages(requestedLocales, availableLocales, defaultLocale,
|
||||
LangNegStrategy::Filtering, aRetVal);
|
||||
} else {
|
||||
//XXX: In bug 1348042 we're working on getting the content process
|
||||
// to follow the parent process negotiated locales.
|
||||
// Until we have it, we're going to match the behavior of following
|
||||
// the ChromeRegistry locale in the content process.
|
||||
|
||||
nsAutoCString uaLangTag;
|
||||
nsCOMPtr<nsIToolkitChromeRegistry> cr =
|
||||
mozilla::services::GetToolkitChromeRegistryService();
|
||||
if (cr) {
|
||||
// We don't want to canonicalize the locale from ChromeRegistry into
|
||||
// it's BCP47 form because we will use it for direct language
|
||||
// negotiation and BCP47 changes `ja-JP-mac` into `ja-JP-x-variant-mac`.
|
||||
cr->GetSelectedLocale(NS_LITERAL_CSTRING("global"), false, uaLangTag);
|
||||
}
|
||||
if (!uaLangTag.IsEmpty()) {
|
||||
aRetVal.AppendElement(uaLangTag);
|
||||
}
|
||||
|
||||
if (!uaLangTag.EqualsLiteral("en-US")) {
|
||||
aRetVal.AppendElement(NS_LITERAL_CSTRING("en-US"));
|
||||
if (!uaLangTag.Equals(defaultLocale)) {
|
||||
aRetVal.AppendElement(defaultLocale);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -121,7 +137,7 @@ void
|
|||
LocaleService::GetAppLocalesAsLangTags(nsTArray<nsCString>& aRetVal)
|
||||
{
|
||||
if (mAppLocales.IsEmpty()) {
|
||||
ReadAppLocales(mAppLocales);
|
||||
NegotiateAppLocales(mAppLocales);
|
||||
}
|
||||
aRetVal = mAppLocales;
|
||||
}
|
||||
|
@ -130,7 +146,7 @@ void
|
|||
LocaleService::GetAppLocalesAsBCP47(nsTArray<nsCString>& aRetVal)
|
||||
{
|
||||
if (mAppLocales.IsEmpty()) {
|
||||
ReadAppLocales(mAppLocales);
|
||||
NegotiateAppLocales(mAppLocales);
|
||||
}
|
||||
for (uint32_t i = 0; i < mAppLocales.Length(); i++) {
|
||||
nsAutoCString locale(mAppLocales[i]);
|
||||
|
@ -156,16 +172,9 @@ LocaleService::GetRequestedLocales(nsTArray<nsCString>& aRetVal)
|
|||
}
|
||||
|
||||
// Otherwise, we'll try to get the requested locale from the prefs.
|
||||
|
||||
// In some cases, mainly on Fennec and on Linux version,
|
||||
// `general.useragent.locale` is a special 'localized' value, like:
|
||||
// "chrome://global/locale/intl.properties"
|
||||
if (!NS_SUCCEEDED(Preferences::GetLocalizedCString(SELECTED_LOCALE_PREF, &locale))) {
|
||||
// If not, we can attempt to retrieve it as a simple string value.
|
||||
if (!NS_SUCCEEDED(Preferences::GetCString(SELECTED_LOCALE_PREF, &locale))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// At the moment we just take a single locale, but in the future
|
||||
// we'll want to allow user to specify a list of requested locales.
|
||||
|
@ -203,14 +212,23 @@ LocaleService::GetAvailableLocales(nsTArray<nsCString>& aRetVal)
|
|||
void
|
||||
LocaleService::Refresh()
|
||||
{
|
||||
// if mAppLocales has not been initialized yet, just return
|
||||
if (mAppLocales.IsEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsTArray<nsCString> newLocales;
|
||||
ReadAppLocales(newLocales);
|
||||
NegotiateAppLocales(newLocales);
|
||||
|
||||
if (mAppLocales != newLocales) {
|
||||
mAppLocales = Move(newLocales);
|
||||
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
||||
if (obs) {
|
||||
obs->NotifyObservers(nullptr, "intl:app-locales-changed", nullptr);
|
||||
|
||||
// Deprecated, please use `intl:app-locales-changed`.
|
||||
// Kept for now for compatibility reasons
|
||||
obs->NotifyObservers(nullptr, "selected-locale-has-changed", nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -386,6 +404,7 @@ LocaleService::Observe(nsISupports *aSubject, const char *aTopic,
|
|||
// user requested locales.
|
||||
NS_ConvertUTF16toUTF8 pref(aData);
|
||||
if (pref.EqualsLiteral(MATCH_OS_LOCALE_PREF) || pref.EqualsLiteral(SELECTED_LOCALE_PREF)) {
|
||||
Refresh();
|
||||
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
||||
if (obs) {
|
||||
obs->NotifyObservers(nullptr, "intl:requested-locales-changed", nullptr);
|
||||
|
@ -420,7 +439,7 @@ NS_IMETHODIMP
|
|||
LocaleService::GetAppLocalesAsLangTags(uint32_t* aCount, char*** aOutArray)
|
||||
{
|
||||
if (mAppLocales.IsEmpty()) {
|
||||
ReadAppLocales(mAppLocales);
|
||||
NegotiateAppLocales(mAppLocales);
|
||||
}
|
||||
|
||||
*aCount = mAppLocales.Length();
|
||||
|
@ -445,7 +464,7 @@ NS_IMETHODIMP
|
|||
LocaleService::GetAppLocaleAsLangTag(nsACString& aRetVal)
|
||||
{
|
||||
if (mAppLocales.IsEmpty()) {
|
||||
ReadAppLocales(mAppLocales);
|
||||
NegotiateAppLocales(mAppLocales);
|
||||
}
|
||||
aRetVal = mAppLocales[0];
|
||||
return NS_OK;
|
||||
|
@ -455,7 +474,7 @@ NS_IMETHODIMP
|
|||
LocaleService::GetAppLocaleAsBCP47(nsACString& aRetVal)
|
||||
{
|
||||
if (mAppLocales.IsEmpty()) {
|
||||
ReadAppLocales(mAppLocales);
|
||||
NegotiateAppLocales(mAppLocales);
|
||||
}
|
||||
aRetVal = mAppLocales[0];
|
||||
SanitizeForBCP47(aRetVal);
|
||||
|
|
|
@ -212,6 +212,8 @@ private:
|
|||
LangNegStrategy aStrategy,
|
||||
nsTArray<nsCString>& aRetVal);
|
||||
|
||||
void NegotiateAppLocales(nsTArray<nsCString>& aRetVal);
|
||||
|
||||
virtual ~LocaleService();
|
||||
|
||||
nsTArray<nsCString> mAppLocales;
|
||||
|
|
Загрузка…
Ссылка в новой задаче