parser/htmlparser/src/nsParserMsgUtils.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/parser/htmlparser/src/nsParserMsgUtils.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,65 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsIServiceManager.h"
    1.10 +#include "nsIStringBundle.h"
    1.11 +#include "nsXPIDLString.h"
    1.12 +#include "nsParserMsgUtils.h"
    1.13 +#include "nsNetCID.h"
    1.14 +#include "mozilla/Services.h"
    1.15 +
    1.16 +static nsresult GetBundle(const char * aPropFileName, nsIStringBundle **aBundle)
    1.17 +{
    1.18 +  NS_ENSURE_ARG_POINTER(aPropFileName);
    1.19 +  NS_ENSURE_ARG_POINTER(aBundle);
    1.20 +
    1.21 +  // Create a bundle for the localization
    1.22 +
    1.23 +  nsCOMPtr<nsIStringBundleService> stringService =
    1.24 +    mozilla::services::GetStringBundleService();
    1.25 +  if (!stringService)
    1.26 +    return NS_ERROR_FAILURE;
    1.27 +
    1.28 +  return stringService->CreateBundle(aPropFileName, aBundle);
    1.29 +}
    1.30 +
    1.31 +nsresult
    1.32 +nsParserMsgUtils::GetLocalizedStringByName(const char * aPropFileName, const char* aKey, nsString& oVal)
    1.33 +{
    1.34 +  oVal.Truncate();
    1.35 +
    1.36 +  NS_ENSURE_ARG_POINTER(aKey);
    1.37 +
    1.38 +  nsCOMPtr<nsIStringBundle> bundle;
    1.39 +  nsresult rv = GetBundle(aPropFileName,getter_AddRefs(bundle));
    1.40 +  if (NS_SUCCEEDED(rv) && bundle) {
    1.41 +    nsXPIDLString valUni;
    1.42 +    nsAutoString key; key.AssignWithConversion(aKey);
    1.43 +    rv = bundle->GetStringFromName(key.get(), getter_Copies(valUni));
    1.44 +    if (NS_SUCCEEDED(rv) && valUni) {
    1.45 +      oVal.Assign(valUni);
    1.46 +    }  
    1.47 +  }
    1.48 +
    1.49 +  return rv;
    1.50 +}
    1.51 +
    1.52 +nsresult
    1.53 +nsParserMsgUtils::GetLocalizedStringByID(const char * aPropFileName, uint32_t aID, nsString& oVal)
    1.54 +{
    1.55 +  oVal.Truncate();
    1.56 +
    1.57 +  nsCOMPtr<nsIStringBundle> bundle;
    1.58 +  nsresult rv = GetBundle(aPropFileName,getter_AddRefs(bundle));
    1.59 +  if (NS_SUCCEEDED(rv) && bundle) {
    1.60 +    nsXPIDLString valUni;
    1.61 +    rv = bundle->GetStringFromID(aID, getter_Copies(valUni));
    1.62 +    if (NS_SUCCEEDED(rv) && valUni) {
    1.63 +      oVal.Assign(valUni);
    1.64 +    }  
    1.65 +  }
    1.66 +
    1.67 +  return rv;
    1.68 +}

mercurial