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"
|
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"
|
2017-03-22 02:20:07 +03:00
|
|
|
#include "mozilla/intl/OSPreferences.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;
|
2017-03-22 02:20:07 +03:00
|
|
|
using mozilla::intl::OSPreferences;
|
2015-01-14 08:30:49 +03:00
|
|
|
|
2017-02-15 05:08:31 +03:00
|
|
|
static constexpr nsUConvProp kLangGroups[] = {
|
2015-01-14 08:30:49 +03:00
|
|
|
#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
|
|
|
|
2016-03-29 02:09:43 +03:00
|
|
|
nsCOMPtr<nsIAtom> lang = NS_Atomize(lowered);
|
2010-02-24 20:57:44 +03:00
|
|
|
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);
|
2016-03-29 02:09:43 +03:00
|
|
|
return NS_Atomize(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*
|
2017-03-13 06:39:22 +03:00
|
|
|
nsLanguageAtomService::GetLocaleLanguage()
|
2000-07-14 01:10:34 +04:00
|
|
|
{
|
2004-06-09 03:53:59 +04:00
|
|
|
do {
|
2010-02-24 20:57:44 +03:00
|
|
|
if (!mLocaleLanguage) {
|
2017-03-13 06:39:22 +03:00
|
|
|
nsAutoCString locale;
|
2017-03-22 02:20:07 +03:00
|
|
|
OSPreferences::GetInstance()->GetSystemLocale(locale);
|
2004-06-09 03:53:59 +04:00
|
|
|
|
2017-03-13 06:39:22 +03:00
|
|
|
ToLowerCase(locale); // use lowercase for all language atoms
|
|
|
|
mLocaleLanguage = NS_Atomize(locale);
|
2004-06-09 03:53:59 +04:00
|
|
|
}
|
|
|
|
} while (0);
|
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
|
|
|
}
|
|
|
|
|
2016-03-29 02:09:43 +03:00
|
|
|
nsCOMPtr<nsIAtom> langGroup = NS_Atomize(langGroupStr);
|
2010-02-24 20:57:44 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|