intl/locale/src/nsLanguageAtomService.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsLanguageAtomService.h"
michael@0 7 #include "nsICharsetConverterManager.h"
michael@0 8 #include "nsILocaleService.h"
michael@0 9 #include "nsUnicharUtils.h"
michael@0 10 #include "nsIAtom.h"
michael@0 11 #include "mozilla/Services.h"
michael@0 12 #include "nsServiceManagerUtils.h"
michael@0 13
michael@0 14 NS_IMPL_ISUPPORTS(nsLanguageAtomService, nsILanguageAtomService)
michael@0 15
michael@0 16 nsLanguageAtomService::nsLanguageAtomService()
michael@0 17 {
michael@0 18 }
michael@0 19
michael@0 20 nsresult
michael@0 21 nsLanguageAtomService::InitLangGroupTable()
michael@0 22 {
michael@0 23 if (mLangGroups)
michael@0 24 return NS_OK;
michael@0 25
michael@0 26 nsCOMPtr<nsIStringBundleService> bundleService =
michael@0 27 mozilla::services::GetStringBundleService();
michael@0 28 if (!bundleService)
michael@0 29 return NS_ERROR_FAILURE;
michael@0 30
michael@0 31 return bundleService->CreateBundle("resource://gre/res/langGroups.properties",
michael@0 32 getter_AddRefs(mLangGroups));
michael@0 33 }
michael@0 34
michael@0 35 nsIAtom*
michael@0 36 nsLanguageAtomService::LookupLanguage(const nsACString &aLanguage,
michael@0 37 nsresult *aError)
michael@0 38 {
michael@0 39 nsAutoCString lowered(aLanguage);
michael@0 40 ToLowerCase(lowered);
michael@0 41
michael@0 42 nsCOMPtr<nsIAtom> lang = do_GetAtom(lowered);
michael@0 43 return GetLanguageGroup(lang, aError);
michael@0 44 }
michael@0 45
michael@0 46 already_AddRefed<nsIAtom>
michael@0 47 nsLanguageAtomService::LookupCharSet(const char *aCharSet, nsresult *aError)
michael@0 48 {
michael@0 49 if (!mCharSets) {
michael@0 50 mCharSets = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID);
michael@0 51 if (!mCharSets) {
michael@0 52 if (aError)
michael@0 53 *aError = NS_ERROR_FAILURE;
michael@0 54
michael@0 55 return nullptr;
michael@0 56 }
michael@0 57 }
michael@0 58
michael@0 59 nsCOMPtr<nsIAtom> langGroup;
michael@0 60 mCharSets->GetCharsetLangGroup(aCharSet, getter_AddRefs(langGroup));
michael@0 61 if (!langGroup) {
michael@0 62 if (aError)
michael@0 63 *aError = NS_ERROR_FAILURE;
michael@0 64
michael@0 65 return nullptr;
michael@0 66 }
michael@0 67
michael@0 68 if (aError)
michael@0 69 *aError = NS_OK;
michael@0 70
michael@0 71 return langGroup.forget();
michael@0 72 }
michael@0 73
michael@0 74 nsIAtom*
michael@0 75 nsLanguageAtomService::GetLocaleLanguage(nsresult *aError)
michael@0 76 {
michael@0 77 nsresult res = NS_OK;
michael@0 78
michael@0 79 do {
michael@0 80 if (!mLocaleLanguage) {
michael@0 81 nsCOMPtr<nsILocaleService> localeService;
michael@0 82 localeService = do_GetService(NS_LOCALESERVICE_CONTRACTID);
michael@0 83 if (!localeService) {
michael@0 84 res = NS_ERROR_FAILURE;
michael@0 85 break;
michael@0 86 }
michael@0 87
michael@0 88 nsCOMPtr<nsILocale> locale;
michael@0 89 res = localeService->GetApplicationLocale(getter_AddRefs(locale));
michael@0 90 if (NS_FAILED(res))
michael@0 91 break;
michael@0 92
michael@0 93 nsAutoString loc;
michael@0 94 res = locale->GetCategory(NS_LITERAL_STRING(NSILOCALE_MESSAGE), loc);
michael@0 95 if (NS_FAILED(res))
michael@0 96 break;
michael@0 97
michael@0 98 ToLowerCase(loc); // use lowercase for all language atoms
michael@0 99 mLocaleLanguage = do_GetAtom(loc);
michael@0 100 }
michael@0 101 } while (0);
michael@0 102
michael@0 103 if (aError)
michael@0 104 *aError = res;
michael@0 105
michael@0 106 return mLocaleLanguage;
michael@0 107 }
michael@0 108
michael@0 109 nsIAtom*
michael@0 110 nsLanguageAtomService::GetLanguageGroup(nsIAtom *aLanguage,
michael@0 111 nsresult *aError)
michael@0 112 {
michael@0 113 nsIAtom *retVal;
michael@0 114 nsresult res = NS_OK;
michael@0 115
michael@0 116 retVal = mLangToGroup.GetWeak(aLanguage);
michael@0 117
michael@0 118 if (!retVal) {
michael@0 119 if (!mLangGroups) {
michael@0 120 if (NS_FAILED(InitLangGroupTable())) {
michael@0 121 if (aError) {
michael@0 122 *aError = NS_ERROR_FAILURE;
michael@0 123 }
michael@0 124 return nullptr;
michael@0 125 }
michael@0 126 }
michael@0 127
michael@0 128 nsString langStr;
michael@0 129 aLanguage->ToString(langStr);
michael@0 130
michael@0 131 nsXPIDLString langGroupStr;
michael@0 132 res = mLangGroups->GetStringFromName(langStr.get(),
michael@0 133 getter_Copies(langGroupStr));
michael@0 134 if (NS_FAILED(res)) {
michael@0 135 int32_t hyphen = langStr.FindChar('-');
michael@0 136 if (hyphen >= 0) {
michael@0 137 nsAutoString truncated(langStr);
michael@0 138 truncated.Truncate(hyphen);
michael@0 139 res = mLangGroups->GetStringFromName(truncated.get(),
michael@0 140 getter_Copies(langGroupStr));
michael@0 141 if (NS_FAILED(res)) {
michael@0 142 langGroupStr.AssignLiteral("x-unicode");
michael@0 143 }
michael@0 144 } else {
michael@0 145 langGroupStr.AssignLiteral("x-unicode");
michael@0 146 }
michael@0 147 }
michael@0 148
michael@0 149 nsCOMPtr<nsIAtom> langGroup = do_GetAtom(langGroupStr);
michael@0 150
michael@0 151 // The hashtable will keep an owning reference to the atom
michael@0 152 mLangToGroup.Put(aLanguage, langGroup);
michael@0 153 retVal = langGroup.get();
michael@0 154 }
michael@0 155
michael@0 156 if (aError) {
michael@0 157 *aError = res;
michael@0 158 }
michael@0 159
michael@0 160 return retVal;
michael@0 161 }

mercurial