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