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/. */
5
6 #include "nsIStringBundle.h"
7 #include "nsString.h"
8 #include "nsParserMsgUtils.h"
9 #include "nsNetCID.h"
10 #include "mozilla/Components.h"
11
GetBundle(const char * aPropFileName,nsIStringBundle ** aBundle)12 static nsresult GetBundle(const char* aPropFileName,
13 nsIStringBundle** aBundle) {
14 NS_ENSURE_ARG_POINTER(aPropFileName);
15 NS_ENSURE_ARG_POINTER(aBundle);
16
17 // Create a bundle for the localization
18
19 nsCOMPtr<nsIStringBundleService> stringService =
20 mozilla::components::StringBundle::Service();
21 if (!stringService) return NS_ERROR_FAILURE;
22
23 return stringService->CreateBundle(aPropFileName, aBundle);
24 }
25
GetLocalizedStringByName(const char * aPropFileName,const char * aKey,nsString & oVal)26 nsresult nsParserMsgUtils::GetLocalizedStringByName(const char* aPropFileName,
27 const char* aKey,
28 nsString& oVal) {
29 oVal.Truncate();
30
31 NS_ENSURE_ARG_POINTER(aKey);
32
33 nsCOMPtr<nsIStringBundle> bundle;
34 nsresult rv = GetBundle(aPropFileName, getter_AddRefs(bundle));
35 if (NS_SUCCEEDED(rv) && bundle) {
36 nsAutoString valUni;
37 rv = bundle->GetStringFromName(aKey, valUni);
38 if (NS_SUCCEEDED(rv)) {
39 oVal.Assign(valUni);
40 }
41 }
42
43 return rv;
44 }
45
GetLocalizedStringByID(const char * aPropFileName,uint32_t aID,nsString & oVal)46 nsresult nsParserMsgUtils::GetLocalizedStringByID(const char* aPropFileName,
47 uint32_t aID,
48 nsString& oVal) {
49 oVal.Truncate();
50
51 nsCOMPtr<nsIStringBundle> bundle;
52 nsresult rv = GetBundle(aPropFileName, getter_AddRefs(bundle));
53 if (NS_SUCCEEDED(rv) && bundle) {
54 nsAutoString valUni;
55 rv = bundle->GetStringFromID(aID, valUni);
56 if (NS_SUCCEEDED(rv)) {
57 oVal.Assign(valUni);
58 }
59 }
60
61 return rv;
62 }
63