2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsLanguageAtomService.h"
|
|
|
|
#include "nsILocaleService.h"
|
2015-01-13 21:30:49 -08:00
|
|
|
#include "nsUConvPropertySearch.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsUnicharUtils.h"
|
2010-02-24 09:57:44 -08:00
|
|
|
#include "nsIAtom.h"
|
2015-01-13 21:30:49 -08:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2010-05-14 02:24:41 -07:00
|
|
|
#include "mozilla/Services.h"
|
2013-09-27 09:45:04 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2014-05-08 02:32:00 -07:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2015-01-13 21:30:49 -08:00
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
static const char* kLangGroups[][3] = {
|
|
|
|
#include "langGroups.properties.h"
|
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsLanguageAtomService, nsILanguageAtomService)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsLanguageAtomService::nsLanguageAtomService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom*
|
2011-05-30 22:09:05 -07:00
|
|
|
nsLanguageAtomService::LookupLanguage(const nsACString &aLanguage,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult *aError)
|
|
|
|
{
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString lowered(aLanguage);
|
2007-03-22 10:30:00 -07:00
|
|
|
ToLowerCase(lowered);
|
|
|
|
|
2010-02-24 09:57:44 -08:00
|
|
|
nsCOMPtr<nsIAtom> lang = do_GetAtom(lowered);
|
|
|
|
return GetLanguageGroup(lang, aError);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIAtom>
|
2014-05-08 02:32:00 -07:00
|
|
|
nsLanguageAtomService::LookupCharSet(const nsACString& aCharSet)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-08 02:32:00 -07:00
|
|
|
nsAutoCString group;
|
|
|
|
mozilla::dom::EncodingUtils::LangGroupForEncoding(aCharSet, group);
|
|
|
|
return do_GetAtom(group);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom*
|
2010-02-24 09:57:44 -08:00
|
|
|
nsLanguageAtomService::GetLocaleLanguage(nsresult *aError)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
do {
|
2010-02-24 09:57:44 -08:00
|
|
|
if (!mLocaleLanguage) {
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
2011-11-01 00:16:48 -07:00
|
|
|
res = locale->GetCategory(NS_LITERAL_STRING(NSILOCALE_MESSAGE), loc);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(res))
|
|
|
|
break;
|
|
|
|
|
2010-02-24 09:57:44 -08:00
|
|
|
ToLowerCase(loc); // use lowercase for all language atoms
|
|
|
|
mLocaleLanguage = do_GetAtom(loc);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (aError)
|
|
|
|
*aError = res;
|
|
|
|
|
2010-02-24 09:57:44 -08:00
|
|
|
return mLocaleLanguage;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom*
|
|
|
|
nsLanguageAtomService::GetLanguageGroup(nsIAtom *aLanguage,
|
|
|
|
nsresult *aError)
|
|
|
|
{
|
|
|
|
nsIAtom *retVal;
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
retVal = mLangToGroup.GetWeak(aLanguage);
|
|
|
|
|
|
|
|
if (!retVal) {
|
2015-01-13 21:30:49 -08:00
|
|
|
nsAutoCString langStr;
|
|
|
|
aLanguage->ToUTF8String(langStr);
|
2010-02-24 09:57:44 -08:00
|
|
|
|
2015-01-13 21:30:49 -08:00
|
|
|
nsAutoCString langGroupStr;
|
|
|
|
res = nsUConvPropertySearch::SearchPropertyValue(kLangGroups,
|
|
|
|
ArrayLength(kLangGroups),
|
|
|
|
langStr, langGroupStr);
|
2014-11-26 02:24:16 -08:00
|
|
|
while (NS_FAILED(res)) {
|
|
|
|
int32_t hyphen = langStr.RFindChar('-');
|
|
|
|
if (hyphen <= 0) {
|
2010-02-24 09:57:44 -08:00
|
|
|
langGroupStr.AssignLiteral("x-unicode");
|
2014-11-26 02:24:16 -08:00
|
|
|
break;
|
2010-02-24 09:57:44 -08:00
|
|
|
}
|
2014-11-26 02:24:16 -08:00
|
|
|
langStr.Truncate(hyphen);
|
2015-01-13 21:30:49 -08:00
|
|
|
res = nsUConvPropertySearch::SearchPropertyValue(kLangGroups,
|
|
|
|
ArrayLength(kLangGroups),
|
|
|
|
langStr, langGroupStr);
|
2010-02-24 09:57:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|