Bumping manifests a=b2g-bump
[gecko.git] / intl / locale / nsLanguageAtomService.cpp
blob70dac5132cfb9d35b494892f5294e9a2fae5901a
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsLanguageAtomService.h"
7 #include "nsILocaleService.h"
8 #include "nsUnicharUtils.h"
9 #include "nsIAtom.h"
10 #include "mozilla/Services.h"
11 #include "nsServiceManagerUtils.h"
12 #include "mozilla/dom/EncodingUtils.h"
14 NS_IMPL_ISUPPORTS(nsLanguageAtomService, nsILanguageAtomService)
16 nsLanguageAtomService::nsLanguageAtomService()
20 nsresult
21 nsLanguageAtomService::InitLangGroupTable()
23 if (mLangGroups)
24 return NS_OK;
26 nsCOMPtr<nsIStringBundleService> bundleService =
27 mozilla::services::GetStringBundleService();
28 if (!bundleService)
29 return NS_ERROR_FAILURE;
31 return bundleService->CreateBundle("resource://gre/res/langGroups.properties",
32 getter_AddRefs(mLangGroups));
35 nsIAtom*
36 nsLanguageAtomService::LookupLanguage(const nsACString &aLanguage,
37 nsresult *aError)
39 nsAutoCString lowered(aLanguage);
40 ToLowerCase(lowered);
42 nsCOMPtr<nsIAtom> lang = do_GetAtom(lowered);
43 return GetLanguageGroup(lang, aError);
46 already_AddRefed<nsIAtom>
47 nsLanguageAtomService::LookupCharSet(const nsACString& aCharSet)
49 nsAutoCString group;
50 mozilla::dom::EncodingUtils::LangGroupForEncoding(aCharSet, group);
51 return do_GetAtom(group);
54 nsIAtom*
55 nsLanguageAtomService::GetLocaleLanguage(nsresult *aError)
57 nsresult res = NS_OK;
59 do {
60 if (!mLocaleLanguage) {
61 nsCOMPtr<nsILocaleService> localeService;
62 localeService = do_GetService(NS_LOCALESERVICE_CONTRACTID);
63 if (!localeService) {
64 res = NS_ERROR_FAILURE;
65 break;
68 nsCOMPtr<nsILocale> locale;
69 res = localeService->GetApplicationLocale(getter_AddRefs(locale));
70 if (NS_FAILED(res))
71 break;
73 nsAutoString loc;
74 res = locale->GetCategory(NS_LITERAL_STRING(NSILOCALE_MESSAGE), loc);
75 if (NS_FAILED(res))
76 break;
78 ToLowerCase(loc); // use lowercase for all language atoms
79 mLocaleLanguage = do_GetAtom(loc);
81 } while (0);
83 if (aError)
84 *aError = res;
86 return mLocaleLanguage;
89 nsIAtom*
90 nsLanguageAtomService::GetLanguageGroup(nsIAtom *aLanguage,
91 nsresult *aError)
93 nsIAtom *retVal;
94 nsresult res = NS_OK;
96 retVal = mLangToGroup.GetWeak(aLanguage);
98 if (!retVal) {
99 if (!mLangGroups) {
100 if (NS_FAILED(InitLangGroupTable())) {
101 if (aError) {
102 *aError = NS_ERROR_FAILURE;
104 return nullptr;
108 nsString langStr;
109 aLanguage->ToString(langStr);
111 nsXPIDLString langGroupStr;
112 res = mLangGroups->GetStringFromName(langStr.get(),
113 getter_Copies(langGroupStr));
114 if (NS_FAILED(res)) {
115 int32_t hyphen = langStr.FindChar('-');
116 if (hyphen >= 0) {
117 nsAutoString truncated(langStr);
118 truncated.Truncate(hyphen);
119 res = mLangGroups->GetStringFromName(truncated.get(),
120 getter_Copies(langGroupStr));
121 if (NS_FAILED(res)) {
122 langGroupStr.AssignLiteral("x-unicode");
124 } else {
125 langGroupStr.AssignLiteral("x-unicode");
129 nsCOMPtr<nsIAtom> langGroup = do_GetAtom(langGroupStr);
131 // The hashtable will keep an owning reference to the atom
132 mLangToGroup.Put(aLanguage, langGroup);
133 retVal = langGroup.get();
136 if (aError) {
137 *aError = res;
140 return retVal;