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"
|
|
|
|
#include "nsUnicharUtils.h"
|
2010-02-24 09:57:44 -08:00
|
|
|
#include "nsIAtom.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
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsLanguageAtomService, nsILanguageAtomService)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsLanguageAtomService::nsLanguageAtomService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsLanguageAtomService::InitLangGroupTable()
|
|
|
|
{
|
2010-05-14 02:24:41 -07:00
|
|
|
if (mLangGroups)
|
|
|
|
return NS_OK;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIStringBundleService> bundleService =
|
2010-05-14 02:24:41 -07:00
|
|
|
mozilla::services::GetStringBundleService();
|
|
|
|
if (!bundleService)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-14 02:24:41 -07:00
|
|
|
return bundleService->CreateBundle("resource://gre/res/langGroups.properties",
|
|
|
|
getter_AddRefs(mLangGroups));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (!mLangGroups) {
|
|
|
|
if (NS_FAILED(InitLangGroupTable())) {
|
|
|
|
if (aError) {
|
|
|
|
*aError = NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-02-24 09:57:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString langStr;
|
|
|
|
aLanguage->ToString(langStr);
|
|
|
|
|
|
|
|
nsXPIDLString langGroupStr;
|
|
|
|
res = mLangGroups->GetStringFromName(langStr.get(),
|
|
|
|
getter_Copies(langGroupStr));
|
|
|
|
if (NS_FAILED(res)) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t hyphen = langStr.FindChar('-');
|
2010-02-24 09:57:44 -08:00
|
|
|
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;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|