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:
Jonathan Kew 2016-10-28 12:04:06 +01:00
Родитель 190e9ca088
Коммит e007b6ec83
11 изменённых файлов: 58 добавлений и 7 удалений

Просмотреть файл

@ -32,6 +32,10 @@
#include "mozilla/StyleSheet.h"
#include "mozilla/StyleSheetInlines.h"
#ifdef ENABLE_INTL_API
#include "unicode/uloc.h"
#endif
nsChromeRegistry* nsChromeRegistry::gChromeRegistry;
// DO NOT use namespace mozilla; it'll break due to a naming conflict between
@ -709,3 +713,32 @@ nsChromeRegistry::GetSingleton()
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);
void SanitizeForBCP47(nsACString& aLocale);
public:
static already_AddRefed<nsChromeRegistry> GetSingleton();

Просмотреть файл

@ -224,7 +224,7 @@ nsChromeRegistryChrome::IsLocaleRTL(const nsACString& package, bool *aResult)
*aResult = false;
nsAutoCString locale;
GetSelectedLocale(package, locale);
GetSelectedLocale(package, false, locale);
if (locale.Length() < 2)
return NS_OK;
@ -234,6 +234,7 @@ nsChromeRegistryChrome::IsLocaleRTL(const nsACString& package, bool *aResult)
nsresult
nsChromeRegistryChrome::GetSelectedLocale(const nsACString& aPackage,
bool aAsBCP47,
nsACString& aLocale)
{
nsCString realpackage;
@ -248,6 +249,10 @@ nsChromeRegistryChrome::GetSelectedLocale(const nsACString& aPackage,
if (aLocale.IsEmpty())
return NS_ERROR_FAILURE;
if (aAsBCP47) {
SanitizeForBCP47(aLocale);
}
return NS_OK;
}

Просмотреть файл

@ -36,6 +36,7 @@ class nsChromeRegistryChrome : public nsChromeRegistry
NS_IMETHOD IsLocaleRTL(const nsACString& package,
bool *aResult) override;
NS_IMETHOD GetSelectedLocale(const nsACString& aPackage,
bool aAsBCP47,
nsACString& aLocale) override;
NS_IMETHOD Observe(nsISupports *aSubject, const char *aTopic,
const char16_t *someData) override;

Просмотреть файл

@ -222,6 +222,7 @@ nsChromeRegistryContent::IsLocaleRTL(const nsACString& aPackage,
NS_IMETHODIMP
nsChromeRegistryContent::GetSelectedLocale(const nsACString& aPackage,
bool aAsBCP47,
nsACString& aLocale)
{
if (aPackage != nsDependentCString("global")) {
@ -229,6 +230,9 @@ nsChromeRegistryContent::GetSelectedLocale(const nsACString& aPackage,
return NS_ERROR_NOT_AVAILABLE;
}
aLocale = mLocale;
if (aAsBCP47) {
SanitizeForBCP47(aLocale);
}
return NS_OK;
}

Просмотреть файл

@ -33,6 +33,7 @@ class nsChromeRegistryContent : public nsChromeRegistry
NS_IMETHOD IsLocaleRTL(const nsACString& package,
bool *aResult) override;
NS_IMETHOD GetSelectedLocale(const nsACString& aPackage,
bool aAsBCP47,
nsACString& aLocale) override;
NS_IMETHOD GetStyleOverlays(nsIURI *aChromeURL,
nsISimpleEnumerator **aResult) override;

Просмотреть файл

@ -50,8 +50,13 @@ interface nsIXULChromeRegistry : nsIChromeRegistry
{
/* Should be called when locales change to reload all chrome (including XUL). */
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
boolean isLocaleRTL(in ACString package);

Просмотреть файл

@ -69,7 +69,7 @@ FallbackEncoding::Get(nsACString& aFallback)
nsCOMPtr<nsIXULChromeRegistry> registry =
mozilla::services::GetXULChromeRegistryService();
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

Просмотреть файл

@ -487,7 +487,7 @@ nsXULPrototypeCache::BeginCaching(nsIURI* aURI)
nsCOMPtr<nsIXULChromeRegistry> chromeReg
= do_GetService(NS_CHROMEREGISTRY_CONTRACTID, &rv);
nsAutoCString locale;
rv = chromeReg->GetSelectedLocale(package, locale);
rv = chromeReg->GetSelectedLocale(package, false, locale);
if (NS_FAILED(rv))
return rv;

Просмотреть файл

@ -922,7 +922,7 @@ nsEditorSpellCheck::DictionaryFetched(DictionaryFetcher* aFetcher)
if (packageRegistry) {
nsAutoCString utf8DictName;
rv2 = packageRegistry->GetSelectedLocale(NS_LITERAL_CSTRING("global"),
utf8DictName);
false, utf8DictName);
dictName.Assign(EmptyString());
AppendUTF8toUTF16(utf8DictName, dictName);
#ifdef DEBUG_DICT

Просмотреть файл

@ -75,7 +75,7 @@ ICUUtils::LanguageTagIterForContent::GetNext(nsACString& aBCP47LangTag)
mozilla::services::GetToolkitChromeRegistryService();
nsAutoCString uaLangTag;
if (cr) {
cr->GetSelectedLocale(NS_LITERAL_CSTRING("global"), uaLangTag);
cr->GetSelectedLocale(NS_LITERAL_CSTRING("global"), true, uaLangTag);
}
if (!uaLangTag.IsEmpty()) {
aBCP47LangTag = uaLangTag;