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"
|
2017-03-22 02:20:07 +03:00
|
|
|
#include "mozilla/intl/OSPreferences.h"
|
2014-05-08 13:32:00 +04:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2015-04-03 22:39:23 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2017-05-26 01:04:13 +03:00
|
|
|
#include "mozilla/ServoBindings.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"
|
|
|
|
};
|
|
|
|
|
2015-04-03 22:39:23 +03:00
|
|
|
// static
|
|
|
|
nsLanguageAtomService*
|
|
|
|
nsLanguageAtomService::GetService()
|
2000-05-03 03:04:35 +04:00
|
|
|
{
|
2015-04-03 22:39:23 +03:00
|
|
|
static UniquePtr<nsLanguageAtomService> gLangAtomService;
|
|
|
|
if (!gLangAtomService) {
|
|
|
|
gLangAtomService = MakeUnique<nsLanguageAtomService>();
|
|
|
|
ClearOnShutdown(&gLangAtomService);
|
|
|
|
}
|
|
|
|
return gLangAtomService.get();
|
2000-05-03 03:04:35 +04:00
|
|
|
}
|
|
|
|
|
2004-06-09 03:53:59 +04:00
|
|
|
nsIAtom*
|
2015-04-03 22:39:23 +03:00
|
|
|
nsLanguageAtomService::LookupLanguage(const nsACString &aLanguage)
|
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);
|
2015-04-03 22:39:23 +03:00
|
|
|
return GetLanguageGroup(lang);
|
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;
|
2015-04-03 22:39:23 +03:00
|
|
|
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*
|
2017-05-26 01:04:13 +03:00
|
|
|
nsLanguageAtomService::GetLanguageGroup(nsIAtom *aLanguage, bool* aNeedsToCache)
|
2010-02-24 20:57:44 +03:00
|
|
|
{
|
2015-04-03 22:39:23 +03:00
|
|
|
nsIAtom *retVal = mLangToGroup.GetWeak(aLanguage);
|
2010-02-24 20:57:44 +03:00
|
|
|
|
|
|
|
if (!retVal) {
|
2017-05-26 01:04:13 +03:00
|
|
|
if (aNeedsToCache) {
|
|
|
|
*aNeedsToCache = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-04-03 22:39:23 +03:00
|
|
|
nsCOMPtr<nsIAtom> uncached = GetUncachedLanguageGroup(aLanguage);
|
2017-04-04 21:11:27 +03:00
|
|
|
retVal = uncached.get();
|
|
|
|
|
2017-05-26 01:04:13 +03:00
|
|
|
AssertIsMainThreadOrServoLangFontPrefsCacheLocked();
|
2017-04-04 21:11:27 +03:00
|
|
|
// The hashtable will keep an owning reference to the atom
|
|
|
|
mLangToGroup.Put(aLanguage, uncached);
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIAtom>
|
2015-04-03 22:39:23 +03:00
|
|
|
nsLanguageAtomService::GetUncachedLanguageGroup(nsIAtom* aLanguage) const
|
2017-04-04 21:11:27 +03:00
|
|
|
{
|
|
|
|
nsAutoCString langStr;
|
|
|
|
aLanguage->ToUTF8String(langStr);
|
|
|
|
|
|
|
|
nsAutoCString langGroupStr;
|
2015-04-03 22:39:23 +03:00
|
|
|
nsresult res =
|
|
|
|
nsUConvPropertySearch::SearchPropertyValue(kLangGroups,
|
|
|
|
ArrayLength(kLangGroups),
|
|
|
|
langStr, langGroupStr);
|
2017-04-04 21:11:27 +03:00
|
|
|
while (NS_FAILED(res)) {
|
|
|
|
int32_t hyphen = langStr.RFindChar('-');
|
|
|
|
if (hyphen <= 0) {
|
|
|
|
langGroupStr.AssignLiteral("x-unicode");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
langStr.Truncate(hyphen);
|
2015-01-14 08:30:49 +03:00
|
|
|
res = nsUConvPropertySearch::SearchPropertyValue(kLangGroups,
|
|
|
|
ArrayLength(kLangGroups),
|
|
|
|
langStr, langGroupStr);
|
2017-04-09 18:24:54 +03:00
|
|
|
}
|
2017-04-04 21:11:27 +03:00
|
|
|
|
2017-04-04 21:11:27 +03:00
|
|
|
nsCOMPtr<nsIAtom> langGroup = NS_Atomize(langGroupStr);
|
|
|
|
|
|
|
|
return langGroup.forget();
|
2000-07-14 01:10:34 +04:00
|
|
|
}
|