michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIStringBundle.h" michael@0: #include "nsXPIDLString.h" michael@0: #include "nsParserMsgUtils.h" michael@0: #include "nsNetCID.h" michael@0: #include "mozilla/Services.h" michael@0: michael@0: static nsresult GetBundle(const char * aPropFileName, nsIStringBundle **aBundle) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aPropFileName); michael@0: NS_ENSURE_ARG_POINTER(aBundle); michael@0: michael@0: // Create a bundle for the localization michael@0: michael@0: nsCOMPtr stringService = michael@0: mozilla::services::GetStringBundleService(); michael@0: if (!stringService) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: return stringService->CreateBundle(aPropFileName, aBundle); michael@0: } michael@0: michael@0: nsresult michael@0: nsParserMsgUtils::GetLocalizedStringByName(const char * aPropFileName, const char* aKey, nsString& oVal) michael@0: { michael@0: oVal.Truncate(); michael@0: michael@0: NS_ENSURE_ARG_POINTER(aKey); michael@0: michael@0: nsCOMPtr bundle; michael@0: nsresult rv = GetBundle(aPropFileName,getter_AddRefs(bundle)); michael@0: if (NS_SUCCEEDED(rv) && bundle) { michael@0: nsXPIDLString valUni; michael@0: nsAutoString key; key.AssignWithConversion(aKey); michael@0: rv = bundle->GetStringFromName(key.get(), getter_Copies(valUni)); michael@0: if (NS_SUCCEEDED(rv) && valUni) { michael@0: oVal.Assign(valUni); michael@0: } michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: nsresult michael@0: nsParserMsgUtils::GetLocalizedStringByID(const char * aPropFileName, uint32_t aID, nsString& oVal) michael@0: { michael@0: oVal.Truncate(); michael@0: michael@0: nsCOMPtr bundle; michael@0: nsresult rv = GetBundle(aPropFileName,getter_AddRefs(bundle)); michael@0: if (NS_SUCCEEDED(rv) && bundle) { michael@0: nsXPIDLString valUni; michael@0: rv = bundle->GetStringFromID(aID, getter_Copies(valUni)); michael@0: if (NS_SUCCEEDED(rv) && valUni) { michael@0: oVal.Assign(valUni); michael@0: } michael@0: } michael@0: michael@0: return rv; michael@0: }