Bug 1332207 - Introduce mozilla::intl::LocaleService. r=jfkthame

MozReview-Commit-ID: AV1bvCt6tmP

--HG--
extra : rebase_source : 1951fbecf24d991d204c0bf00de3336878886df1
This commit is contained in:
Zibi Braniecki 2017-01-25 15:58:14 -08:00
parent 95b4a438ea
commit da241c5563
6 changed files with 238 additions and 0 deletions

View File

@ -26,6 +26,7 @@
#include "mozilla/LookAndFeel.h"
#include "mozilla/Unused.h"
#include "mozilla/intl/LocaleService.h"
#include "nsICommandLine.h"
#include "nsILocaleService.h"
@ -386,6 +387,7 @@ nsresult nsChromeRegistryChrome::UpdateSelectedLocale()
NS_ASSERTION(obsSvc, "Couldn't get observer service.");
obsSvc->NotifyObservers((nsIChromeRegistry*) this,
"selected-locale-has-changed", nullptr);
mozilla::intl::LocaleService::GetInstance()->Refresh();
}
}

View File

@ -0,0 +1,80 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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/. */
#include "LocaleService.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/Services.h"
#include "nsIObserverService.h"
#include "nsIToolkitChromeRegistry.h"
using namespace mozilla::intl;
/**
* This function performs the actual language negotiation for the API.
*
* Currently it collects the locale ID used by nsChromeRegistry and
* adds hardcoded "en-US" locale as a fallback.
*/
static void
ReadAppLocales(nsTArray<nsCString>& aRetVal)
{
nsAutoCString uaLangTag;
nsCOMPtr<nsIToolkitChromeRegistry> cr =
mozilla::services::GetToolkitChromeRegistryService();
if (cr) {
cr->GetSelectedLocale(NS_LITERAL_CSTRING("global"), true, uaLangTag);
}
if (!uaLangTag.IsEmpty()) {
aRetVal.AppendElement(uaLangTag);
}
if (!uaLangTag.EqualsLiteral("en-US")) {
aRetVal.AppendElement(NS_LITERAL_CSTRING("en-US"));
}
}
mozilla::StaticAutoPtr<LocaleService> LocaleService::sInstance;
LocaleService* LocaleService::GetInstance()
{
if (!sInstance) {
sInstance = new LocaleService();
ClearOnShutdown(&sInstance);
}
return sInstance;
}
void
LocaleService::GetAppLocales(nsTArray<nsCString>& aRetVal)
{
if (mAppLocales.IsEmpty()) {
ReadAppLocales(mAppLocales);
}
aRetVal = mAppLocales;
}
void
LocaleService::GetAppLocale(nsACString& aRetVal)
{
if (mAppLocales.IsEmpty()) {
ReadAppLocales(mAppLocales);
}
aRetVal = mAppLocales[0];
}
void
LocaleService::Refresh()
{
nsTArray<nsCString> newLocales;
ReadAppLocales(newLocales);
if (mAppLocales != newLocales) {
mAppLocales = Move(newLocales);
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->NotifyObservers(nullptr, "intl:app-locales-changed", nullptr);
}
}
}

View File

@ -0,0 +1,82 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode:nil; c-basic-offset: 2 -*- */
/* 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/. */
#ifndef mozilla_intl_LocaleService_h__
#define mozilla_intl_LocaleService_h__
#include "mozilla/StaticPtr.h"
#include "nsString.h"
#include "nsTArray.h"
namespace mozilla {
namespace intl {
/**
* LocaleService is a manager of language negotiation in Gecko.
*
* It's intended to be the core place for collecting available and
* requested languages and negotiating them to produce a fallback
* chain of locales for the application.
*/
class LocaleService
{
public:
static LocaleService* GetInstance();
/**
* Returns a list of locales that the application should be localized to.
*
* The result is a sorted list of valid locale IDs and it should be
* used for all APIs that accept list of locales, like ECMA402 and L10n APIs.
*
* This API always returns at least one locale.
*
* Example: ["en-US", "de", "pl", "sr-Cyrl", "zh-Hans-HK"]
*
* Usage:
* nsTArray<nsCString> appLocales;
* LocaleService::GetInstance()->GetAppLocales(appLocales);
*/
void GetAppLocales(nsTArray<nsCString>& aRetVal);
/**
* Returns the best locale that the application should be localized to.
*
* The result is a valid locale IDs and it should be
* used for all APIs that do not handle language negotiation.
*
* Where possible, GetAppLocales should be preferred over this API and
* all callsites should handle some form of "best effort" language
* negotiation to respect user preferences in case the use case does
* not have data for the first locale in the list.
*
* Example: "zh-Hans-HK"
*
* Usage:
* nsAutoCString appLocale;
* LocaleService::GetInstance()->GetAppLocale(appLocale);
*/
void GetAppLocale(nsACString& aRetVal);
/**
* Triggers a refresh of the language negotiation process.
*
* If the result differs from the previous list, it will additionally
* trigger a global event "intl:app-locales-changed".
*/
void Refresh();
protected:
nsTArray<nsCString> mAppLocales;
private:
static StaticAutoPtr<LocaleService> sInstance;
};
} // intl
} // namespace mozilla
#endif /* mozilla_intl_LocaleService_h__ */

View File

@ -35,7 +35,12 @@ EXPORTS += [
'nsWin32Locale.h',
]
EXPORTS.mozilla.intl += [
'LocaleService.h'
]
UNIFIED_SOURCES += [
'LocaleService.cpp',
'nsCollation.cpp',
'nsLanguageAtomService.cpp',
'nsLocale.cpp',
@ -74,3 +79,6 @@ GENERATED_FILES += [
langgroups = GENERATED_FILES['langGroups.properties.h']
langgroups.script = 'props2arrays.py'
langgroups.inputs = ['langGroups.properties']
if CONFIG['ENABLE_TESTS']:
DIRS += ['tests/gtest']

View File

@ -0,0 +1,51 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#include "gtest/gtest.h"
#include "LocaleService.h"
#include "mozilla/Services.h"
#include "nsIToolkitChromeRegistry.h"
using namespace mozilla::intl;
TEST(Intl_Locale_LocaleService, GetAppLocales) {
nsTArray<nsCString> appLocales;
LocaleService::GetInstance()->GetAppLocales(appLocales);
ASSERT_FALSE(appLocales.IsEmpty());
}
TEST(Intl_Locale_LocaleService, GetAppLocales_firstMatchesChromeReg) {
nsTArray<nsCString> appLocales;
LocaleService::GetInstance()->GetAppLocales(appLocales);
nsAutoCString uaLangTag;
nsCOMPtr<nsIToolkitChromeRegistry> cr =
mozilla::services::GetToolkitChromeRegistryService();
if (cr) {
cr->GetSelectedLocale(NS_LITERAL_CSTRING("global"), true, uaLangTag);
}
ASSERT_TRUE(appLocales[0].Equals(uaLangTag));
}
TEST(Intl_Locale_LocaleService, GetAppLocales_lastIsEnUS) {
nsTArray<nsCString> appLocales;
LocaleService::GetInstance()->GetAppLocales(appLocales);
int32_t len = appLocales.Length();
ASSERT_TRUE(appLocales[len - 1].EqualsLiteral("en-US"));
}
TEST(Intl_Locale_LocaleService, GetAppLocale) {
nsTArray<nsCString> appLocales;
LocaleService::GetInstance()->GetAppLocales(appLocales);
nsAutoCString locale;
LocaleService::GetInstance()->GetAppLocale(locale);
ASSERT_TRUE(appLocales[0] == locale);
}

View File

@ -0,0 +1,15 @@
# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# 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/.
UNIFIED_SOURCES += [
'TestLocaleService.cpp',
]
LOCAL_INCLUDES += [
'/intl/locale',
]
FINAL_LIBRARY = 'xul-gtest'