зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1301655 - pt 0 - Add an optional parameter to nsIChromeRegistry.getSelectedLocale to allow callers to request the locale be returned as a valid BCP47 lang tag. r=gandalf
This commit is contained in:
Родитель
190e9ca088
Коммит
e007b6ec83
|
@ -32,6 +32,10 @@
|
||||||
#include "mozilla/StyleSheet.h"
|
#include "mozilla/StyleSheet.h"
|
||||||
#include "mozilla/StyleSheetInlines.h"
|
#include "mozilla/StyleSheetInlines.h"
|
||||||
|
|
||||||
|
#ifdef ENABLE_INTL_API
|
||||||
|
#include "unicode/uloc.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
nsChromeRegistry* nsChromeRegistry::gChromeRegistry;
|
nsChromeRegistry* nsChromeRegistry::gChromeRegistry;
|
||||||
|
|
||||||
// DO NOT use namespace mozilla; it'll break due to a naming conflict between
|
// DO NOT use namespace mozilla; it'll break due to a naming conflict between
|
||||||
|
@ -709,3 +713,32 @@ nsChromeRegistry::GetSingleton()
|
||||||
|
|
||||||
return cr.forget();
|
return cr.forget();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
nsChromeRegistry::SanitizeForBCP47(nsACString& aLocale)
|
||||||
|
{
|
||||||
|
#ifdef ENABLE_INTL_API
|
||||||
|
// Currently, the only locale code we use that's not BCP47-conformant is
|
||||||
|
// "ja-JP-mac" on OS X, but let's try to be more general than just
|
||||||
|
// hard-coding that here.
|
||||||
|
const int32_t LANG_TAG_CAPACITY = 128;
|
||||||
|
char langTag[LANG_TAG_CAPACITY];
|
||||||
|
nsAutoCString locale(aLocale);
|
||||||
|
UErrorCode err = U_ZERO_ERROR;
|
||||||
|
// This is a fail-safe method that will set langTag to "und" if it cannot
|
||||||
|
// match any part of the input locale code.
|
||||||
|
int32_t len = uloc_toLanguageTag(locale.get(), langTag, LANG_TAG_CAPACITY,
|
||||||
|
false, &err);
|
||||||
|
if (U_SUCCESS(err) && len > 0) {
|
||||||
|
aLocale.Assign(langTag, len);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
// This is only really needed for Intl API purposes, AFAIK,
|
||||||
|
// so probably won't be used in a non-ENABLE_INTL_API build.
|
||||||
|
// But let's fix up the single anomalous code we actually ship,
|
||||||
|
// just in case:
|
||||||
|
if (aLocale.EqualsLiteral("ja-JP-mac")) {
|
||||||
|
aLocale.AssignLiteral("ja-JP");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
|
@ -101,6 +101,8 @@ protected:
|
||||||
|
|
||||||
bool GetDirectionForLocale(const nsACString& aLocale);
|
bool GetDirectionForLocale(const nsACString& aLocale);
|
||||||
|
|
||||||
|
void SanitizeForBCP47(nsACString& aLocale);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static already_AddRefed<nsChromeRegistry> GetSingleton();
|
static already_AddRefed<nsChromeRegistry> GetSingleton();
|
||||||
|
|
||||||
|
|
|
@ -224,7 +224,7 @@ nsChromeRegistryChrome::IsLocaleRTL(const nsACString& package, bool *aResult)
|
||||||
*aResult = false;
|
*aResult = false;
|
||||||
|
|
||||||
nsAutoCString locale;
|
nsAutoCString locale;
|
||||||
GetSelectedLocale(package, locale);
|
GetSelectedLocale(package, false, locale);
|
||||||
if (locale.Length() < 2)
|
if (locale.Length() < 2)
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
|
|
||||||
|
@ -234,6 +234,7 @@ nsChromeRegistryChrome::IsLocaleRTL(const nsACString& package, bool *aResult)
|
||||||
|
|
||||||
nsresult
|
nsresult
|
||||||
nsChromeRegistryChrome::GetSelectedLocale(const nsACString& aPackage,
|
nsChromeRegistryChrome::GetSelectedLocale(const nsACString& aPackage,
|
||||||
|
bool aAsBCP47,
|
||||||
nsACString& aLocale)
|
nsACString& aLocale)
|
||||||
{
|
{
|
||||||
nsCString realpackage;
|
nsCString realpackage;
|
||||||
|
@ -248,6 +249,10 @@ nsChromeRegistryChrome::GetSelectedLocale(const nsACString& aPackage,
|
||||||
if (aLocale.IsEmpty())
|
if (aLocale.IsEmpty())
|
||||||
return NS_ERROR_FAILURE;
|
return NS_ERROR_FAILURE;
|
||||||
|
|
||||||
|
if (aAsBCP47) {
|
||||||
|
SanitizeForBCP47(aLocale);
|
||||||
|
}
|
||||||
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,7 @@ class nsChromeRegistryChrome : public nsChromeRegistry
|
||||||
NS_IMETHOD IsLocaleRTL(const nsACString& package,
|
NS_IMETHOD IsLocaleRTL(const nsACString& package,
|
||||||
bool *aResult) override;
|
bool *aResult) override;
|
||||||
NS_IMETHOD GetSelectedLocale(const nsACString& aPackage,
|
NS_IMETHOD GetSelectedLocale(const nsACString& aPackage,
|
||||||
|
bool aAsBCP47,
|
||||||
nsACString& aLocale) override;
|
nsACString& aLocale) override;
|
||||||
NS_IMETHOD Observe(nsISupports *aSubject, const char *aTopic,
|
NS_IMETHOD Observe(nsISupports *aSubject, const char *aTopic,
|
||||||
const char16_t *someData) override;
|
const char16_t *someData) override;
|
||||||
|
|
|
@ -222,6 +222,7 @@ nsChromeRegistryContent::IsLocaleRTL(const nsACString& aPackage,
|
||||||
|
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsChromeRegistryContent::GetSelectedLocale(const nsACString& aPackage,
|
nsChromeRegistryContent::GetSelectedLocale(const nsACString& aPackage,
|
||||||
|
bool aAsBCP47,
|
||||||
nsACString& aLocale)
|
nsACString& aLocale)
|
||||||
{
|
{
|
||||||
if (aPackage != nsDependentCString("global")) {
|
if (aPackage != nsDependentCString("global")) {
|
||||||
|
@ -229,6 +230,9 @@ nsChromeRegistryContent::GetSelectedLocale(const nsACString& aPackage,
|
||||||
return NS_ERROR_NOT_AVAILABLE;
|
return NS_ERROR_NOT_AVAILABLE;
|
||||||
}
|
}
|
||||||
aLocale = mLocale;
|
aLocale = mLocale;
|
||||||
|
if (aAsBCP47) {
|
||||||
|
SanitizeForBCP47(aLocale);
|
||||||
|
}
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,7 @@ class nsChromeRegistryContent : public nsChromeRegistry
|
||||||
NS_IMETHOD IsLocaleRTL(const nsACString& package,
|
NS_IMETHOD IsLocaleRTL(const nsACString& package,
|
||||||
bool *aResult) override;
|
bool *aResult) override;
|
||||||
NS_IMETHOD GetSelectedLocale(const nsACString& aPackage,
|
NS_IMETHOD GetSelectedLocale(const nsACString& aPackage,
|
||||||
|
bool aAsBCP47,
|
||||||
nsACString& aLocale) override;
|
nsACString& aLocale) override;
|
||||||
NS_IMETHOD GetStyleOverlays(nsIURI *aChromeURL,
|
NS_IMETHOD GetStyleOverlays(nsIURI *aChromeURL,
|
||||||
nsISimpleEnumerator **aResult) override;
|
nsISimpleEnumerator **aResult) override;
|
||||||
|
|
|
@ -50,8 +50,13 @@ interface nsIXULChromeRegistry : nsIChromeRegistry
|
||||||
{
|
{
|
||||||
/* Should be called when locales change to reload all chrome (including XUL). */
|
/* Should be called when locales change to reload all chrome (including XUL). */
|
||||||
void reloadChrome();
|
void reloadChrome();
|
||||||
|
|
||||||
ACString getSelectedLocale(in ACString packageName);
|
// If the optional asBCP47 parameter is true, the locale code will be
|
||||||
|
// converted to a BCP47 language tag; in particular, this means that
|
||||||
|
// "ja-JP-mac" will be returned as "ja-JP-x-lvariant-mac", which can be
|
||||||
|
// passed to ECMA402 Intl API methods without throwing a RangeError.
|
||||||
|
ACString getSelectedLocale(in ACString packageName,
|
||||||
|
[optional] in boolean asBCP47);
|
||||||
|
|
||||||
// Get the direction of the locale via the intl.uidirection.<locale> pref
|
// Get the direction of the locale via the intl.uidirection.<locale> pref
|
||||||
boolean isLocaleRTL(in ACString package);
|
boolean isLocaleRTL(in ACString package);
|
||||||
|
|
|
@ -69,7 +69,7 @@ FallbackEncoding::Get(nsACString& aFallback)
|
||||||
nsCOMPtr<nsIXULChromeRegistry> registry =
|
nsCOMPtr<nsIXULChromeRegistry> registry =
|
||||||
mozilla::services::GetXULChromeRegistryService();
|
mozilla::services::GetXULChromeRegistryService();
|
||||||
if (registry) {
|
if (registry) {
|
||||||
registry->GetSelectedLocale(NS_LITERAL_CSTRING("global"), locale);
|
registry->GetSelectedLocale(NS_LITERAL_CSTRING("global"), false, locale);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Let's lower case the string just in case unofficial language packs
|
// Let's lower case the string just in case unofficial language packs
|
||||||
|
|
|
@ -487,7 +487,7 @@ nsXULPrototypeCache::BeginCaching(nsIURI* aURI)
|
||||||
nsCOMPtr<nsIXULChromeRegistry> chromeReg
|
nsCOMPtr<nsIXULChromeRegistry> chromeReg
|
||||||
= do_GetService(NS_CHROMEREGISTRY_CONTRACTID, &rv);
|
= do_GetService(NS_CHROMEREGISTRY_CONTRACTID, &rv);
|
||||||
nsAutoCString locale;
|
nsAutoCString locale;
|
||||||
rv = chromeReg->GetSelectedLocale(package, locale);
|
rv = chromeReg->GetSelectedLocale(package, false, locale);
|
||||||
if (NS_FAILED(rv))
|
if (NS_FAILED(rv))
|
||||||
return rv;
|
return rv;
|
||||||
|
|
||||||
|
|
|
@ -922,7 +922,7 @@ nsEditorSpellCheck::DictionaryFetched(DictionaryFetcher* aFetcher)
|
||||||
if (packageRegistry) {
|
if (packageRegistry) {
|
||||||
nsAutoCString utf8DictName;
|
nsAutoCString utf8DictName;
|
||||||
rv2 = packageRegistry->GetSelectedLocale(NS_LITERAL_CSTRING("global"),
|
rv2 = packageRegistry->GetSelectedLocale(NS_LITERAL_CSTRING("global"),
|
||||||
utf8DictName);
|
false, utf8DictName);
|
||||||
dictName.Assign(EmptyString());
|
dictName.Assign(EmptyString());
|
||||||
AppendUTF8toUTF16(utf8DictName, dictName);
|
AppendUTF8toUTF16(utf8DictName, dictName);
|
||||||
#ifdef DEBUG_DICT
|
#ifdef DEBUG_DICT
|
||||||
|
|
|
@ -75,7 +75,7 @@ ICUUtils::LanguageTagIterForContent::GetNext(nsACString& aBCP47LangTag)
|
||||||
mozilla::services::GetToolkitChromeRegistryService();
|
mozilla::services::GetToolkitChromeRegistryService();
|
||||||
nsAutoCString uaLangTag;
|
nsAutoCString uaLangTag;
|
||||||
if (cr) {
|
if (cr) {
|
||||||
cr->GetSelectedLocale(NS_LITERAL_CSTRING("global"), uaLangTag);
|
cr->GetSelectedLocale(NS_LITERAL_CSTRING("global"), true, uaLangTag);
|
||||||
}
|
}
|
||||||
if (!uaLangTag.IsEmpty()) {
|
if (!uaLangTag.IsEmpty()) {
|
||||||
aBCP47LangTag = uaLangTag;
|
aBCP47LangTag = uaLangTag;
|
||||||
|
|
Загрузка…
Ссылка в новой задаче