summaryrefslogtreecommitdiffstats
path: root/intl/locale/nsLanguageAtomService.cpp
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /intl/locale/nsLanguageAtomService.cpp
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'intl/locale/nsLanguageAtomService.cpp')
-rw-r--r--intl/locale/nsLanguageAtomService.cpp123
1 files changed, 123 insertions, 0 deletions
diff --git a/intl/locale/nsLanguageAtomService.cpp b/intl/locale/nsLanguageAtomService.cpp
new file mode 100644
index 000000000..9002a03f7
--- /dev/null
+++ b/intl/locale/nsLanguageAtomService.cpp
@@ -0,0 +1,123 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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/. */
+
+#include "nsLanguageAtomService.h"
+#include "nsILocaleService.h"
+#include "nsUConvPropertySearch.h"
+#include "nsUnicharUtils.h"
+#include "nsIAtom.h"
+#include "mozilla/ArrayUtils.h"
+#include "mozilla/Services.h"
+#include "nsServiceManagerUtils.h"
+#include "mozilla/dom/EncodingUtils.h"
+
+using namespace mozilla;
+
+static const nsUConvProp kLangGroups[] = {
+#include "langGroups.properties.h"
+};
+
+NS_IMPL_ISUPPORTS(nsLanguageAtomService, nsILanguageAtomService)
+
+nsLanguageAtomService::nsLanguageAtomService()
+{
+}
+
+nsIAtom*
+nsLanguageAtomService::LookupLanguage(const nsACString &aLanguage,
+ nsresult *aError)
+{
+ nsAutoCString lowered(aLanguage);
+ ToLowerCase(lowered);
+
+ nsCOMPtr<nsIAtom> lang = NS_Atomize(lowered);
+ return GetLanguageGroup(lang, aError);
+}
+
+already_AddRefed<nsIAtom>
+nsLanguageAtomService::LookupCharSet(const nsACString& aCharSet)
+{
+ nsAutoCString group;
+ mozilla::dom::EncodingUtils::LangGroupForEncoding(aCharSet, group);
+ return NS_Atomize(group);
+}
+
+nsIAtom*
+nsLanguageAtomService::GetLocaleLanguage(nsresult *aError)
+{
+ nsresult res = NS_OK;
+
+ do {
+ if (!mLocaleLanguage) {
+ 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;
+ res = locale->GetCategory(NS_LITERAL_STRING(NSILOCALE_MESSAGE), loc);
+ if (NS_FAILED(res))
+ break;
+
+ ToLowerCase(loc); // use lowercase for all language atoms
+ mLocaleLanguage = NS_Atomize(loc);
+ }
+ } while (0);
+
+ if (aError)
+ *aError = res;
+
+ return mLocaleLanguage;
+}
+
+nsIAtom*
+nsLanguageAtomService::GetLanguageGroup(nsIAtom *aLanguage,
+ nsresult *aError)
+{
+ nsIAtom *retVal;
+ nsresult res = NS_OK;
+
+ retVal = mLangToGroup.GetWeak(aLanguage);
+
+ if (!retVal) {
+ nsAutoCString langStr;
+ aLanguage->ToUTF8String(langStr);
+
+ nsAutoCString langGroupStr;
+ res = nsUConvPropertySearch::SearchPropertyValue(kLangGroups,
+ ArrayLength(kLangGroups),
+ langStr, langGroupStr);
+ while (NS_FAILED(res)) {
+ int32_t hyphen = langStr.RFindChar('-');
+ if (hyphen <= 0) {
+ langGroupStr.AssignLiteral("x-unicode");
+ break;
+ }
+ langStr.Truncate(hyphen);
+ res = nsUConvPropertySearch::SearchPropertyValue(kLangGroups,
+ ArrayLength(kLangGroups),
+ langStr, langGroupStr);
+ }
+
+ nsCOMPtr<nsIAtom> langGroup = NS_Atomize(langGroupStr);
+
+ // The hashtable will keep an owning reference to the atom
+ mLangToGroup.Put(aLanguage, langGroup);
+ retVal = langGroup.get();
+ }
+
+ if (aError) {
+ *aError = res;
+ }
+
+ return retVal;
+}