gecko/intl/locale/nsLanguageAtomService.cpp
Birunthan Mohanathas e418b25428 Bug 1038537 - Part 3: Flatten intl/locale/src/ directory. r=smontagu
--HG--
rename : intl/locale/src/Makefile.in => intl/locale/Makefile.in
rename : intl/locale/src/PluralForm.jsm => intl/locale/PluralForm.jsm
rename : intl/locale/src/langGroups.properties => intl/locale/langGroups.properties
rename : intl/locale/src/language.properties => intl/locale/language.properties
rename : intl/locale/src/mac/moz.build => intl/locale/mac/moz.build
rename : intl/locale/src/mac/nsCollationMacUC.cpp => intl/locale/mac/nsCollationMacUC.cpp
rename : intl/locale/src/mac/nsCollationMacUC.h => intl/locale/mac/nsCollationMacUC.h
rename : intl/locale/src/mac/nsDateTimeFormatMac.cpp => intl/locale/mac/nsDateTimeFormatMac.cpp
rename : intl/locale/src/mac/nsDateTimeFormatMac.h => intl/locale/mac/nsDateTimeFormatMac.h
rename : intl/locale/src/mac/nsMacCharset.cpp => intl/locale/mac/nsMacCharset.cpp
rename : intl/locale/src/nsCollation.cpp => intl/locale/nsCollation.cpp
rename : intl/locale/src/nsCollation.h => intl/locale/nsCollation.h
rename : intl/locale/src/nsLanguageAtomService.cpp => intl/locale/nsLanguageAtomService.cpp
rename : intl/locale/src/nsLanguageAtomService.h => intl/locale/nsLanguageAtomService.h
rename : intl/locale/src/nsLocale.cpp => intl/locale/nsLocale.cpp
rename : intl/locale/src/nsLocale.h => intl/locale/nsLocale.h
rename : intl/locale/src/nsLocaleConstructors.h => intl/locale/nsLocaleConstructors.h
rename : intl/locale/src/nsLocaleService.cpp => intl/locale/nsLocaleService.cpp
rename : intl/locale/src/nsPlatformCharset.h => intl/locale/nsPlatformCharset.h
rename : intl/locale/src/nsScriptableDateFormat.cpp => intl/locale/nsScriptableDateFormat.cpp
rename : intl/locale/src/nsUConvPropertySearch.cpp => intl/locale/nsUConvPropertySearch.cpp
rename : intl/locale/src/nsUConvPropertySearch.h => intl/locale/nsUConvPropertySearch.h
rename : intl/locale/src/props2arrays.py => intl/locale/props2arrays.py
rename : intl/locale/src/unix/Makefile.in => intl/locale/unix/Makefile.in
rename : intl/locale/src/unix/moz.build => intl/locale/unix/moz.build
rename : intl/locale/src/unix/nsAndroidCharset.cpp => intl/locale/unix/nsAndroidCharset.cpp
rename : intl/locale/src/unix/nsCollationUnix.cpp => intl/locale/unix/nsCollationUnix.cpp
rename : intl/locale/src/unix/nsCollationUnix.h => intl/locale/unix/nsCollationUnix.h
rename : intl/locale/src/unix/nsDateTimeFormatUnix.cpp => intl/locale/unix/nsDateTimeFormatUnix.cpp
rename : intl/locale/src/unix/nsDateTimeFormatUnix.h => intl/locale/unix/nsDateTimeFormatUnix.h
rename : intl/locale/src/unix/nsPosixLocale.cpp => intl/locale/unix/nsPosixLocale.cpp
rename : intl/locale/src/unix/nsUNIXCharset.cpp => intl/locale/unix/nsUNIXCharset.cpp
rename : intl/locale/src/unix/unixcharset.properties => intl/locale/unix/unixcharset.properties
rename : intl/locale/src/windows/Makefile.in => intl/locale/windows/Makefile.in
rename : intl/locale/src/windows/moz.build => intl/locale/windows/moz.build
rename : intl/locale/src/windows/nsCollationWin.cpp => intl/locale/windows/nsCollationWin.cpp
rename : intl/locale/src/windows/nsCollationWin.h => intl/locale/windows/nsCollationWin.h
rename : intl/locale/src/windows/nsDateTimeFormatWin.cpp => intl/locale/windows/nsDateTimeFormatWin.cpp
rename : intl/locale/src/windows/nsDateTimeFormatWin.h => intl/locale/windows/nsDateTimeFormatWin.h
rename : intl/locale/src/windows/nsWin32Locale.cpp => intl/locale/windows/nsWin32Locale.cpp
rename : intl/locale/src/windows/nsWinCharset.cpp => intl/locale/windows/nsWinCharset.cpp
rename : intl/locale/src/windows/wincharset.properties => intl/locale/windows/wincharset.properties
2014-07-24 10:56:38 -07:00

142 lines
3.6 KiB
C++

/* -*- 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 "nsLanguageAtomService.h"
#include "nsILocaleService.h"
#include "nsUnicharUtils.h"
#include "nsIAtom.h"
#include "mozilla/Services.h"
#include "nsServiceManagerUtils.h"
#include "mozilla/dom/EncodingUtils.h"
NS_IMPL_ISUPPORTS(nsLanguageAtomService, nsILanguageAtomService)
nsLanguageAtomService::nsLanguageAtomService()
{
}
nsresult
nsLanguageAtomService::InitLangGroupTable()
{
if (mLangGroups)
return NS_OK;
nsCOMPtr<nsIStringBundleService> bundleService =
mozilla::services::GetStringBundleService();
if (!bundleService)
return NS_ERROR_FAILURE;
return bundleService->CreateBundle("resource://gre/res/langGroups.properties",
getter_AddRefs(mLangGroups));
}
nsIAtom*
nsLanguageAtomService::LookupLanguage(const nsACString &aLanguage,
nsresult *aError)
{
nsAutoCString lowered(aLanguage);
ToLowerCase(lowered);
nsCOMPtr<nsIAtom> lang = do_GetAtom(lowered);
return GetLanguageGroup(lang, aError);
}
already_AddRefed<nsIAtom>
nsLanguageAtomService::LookupCharSet(const nsACString& aCharSet)
{
nsAutoCString group;
mozilla::dom::EncodingUtils::LangGroupForEncoding(aCharSet, group);
return do_GetAtom(group);
}
nsIAtom*
nsLanguageAtomService::GetLocaleLanguage(nsresult *aError)
{
nsresult res = NS_OK;
do {
if (!mLocaleLanguage) {
nsCOMPtr<nsILocaleService> localeService;
localeService = do_GetService(NS_LOCALESERVICE_CONTRACTID);
if (!localeService) {
res = NS_ERROR_FAILURE;
break;
}
nsCOMPtr<nsILocale> locale;
res = localeService->GetApplicationLocale(getter_AddRefs(locale));
if (NS_FAILED(res))
break;
nsAutoString loc;
res = locale->GetCategory(NS_LITERAL_STRING(NSILOCALE_MESSAGE), loc);
if (NS_FAILED(res))
break;
ToLowerCase(loc); // use lowercase for all language atoms
mLocaleLanguage = do_GetAtom(loc);
}
} while (0);
if (aError)
*aError = res;
return mLocaleLanguage;
}
nsIAtom*
nsLanguageAtomService::GetLanguageGroup(nsIAtom *aLanguage,
nsresult *aError)
{
nsIAtom *retVal;
nsresult res = NS_OK;
retVal = mLangToGroup.GetWeak(aLanguage);
if (!retVal) {
if (!mLangGroups) {
if (NS_FAILED(InitLangGroupTable())) {
if (aError) {
*aError = NS_ERROR_FAILURE;
}
return nullptr;
}
}
nsString langStr;
aLanguage->ToString(langStr);
nsXPIDLString langGroupStr;
res = mLangGroups->GetStringFromName(langStr.get(),
getter_Copies(langGroupStr));
if (NS_FAILED(res)) {
int32_t hyphen = langStr.FindChar('-');
if (hyphen >= 0) {
nsAutoString truncated(langStr);
truncated.Truncate(hyphen);
res = mLangGroups->GetStringFromName(truncated.get(),
getter_Copies(langGroupStr));
if (NS_FAILED(res)) {
langGroupStr.AssignLiteral("x-unicode");
}
} else {
langGroupStr.AssignLiteral("x-unicode");
}
}
nsCOMPtr<nsIAtom> langGroup = do_GetAtom(langGroupStr);
// The hashtable will keep an owning reference to the atom
mLangToGroup.Put(aLanguage, langGroup);
retVal = langGroup.get();
}
if (aError) {
*aError = res;
}
return retVal;
}