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