Page view counter

Using string resources from .resx - with parameters?

Last post 10-08-2008 3:27 PM by Talespinner. 3 replies.

Sort Posts:

  • Using string resources from .resx - with parameters?

    01-16-2007, 8:51 AM
    • Loading...
    • mortdk
    • Joined on 11-24-2006, 12:42 PM
    • Posts 12

    Hi all,

    Is it possible to send parameters along with the call to a resource string?

    example: I have a string I wish to display in the UI:  "You have xx new messages".
    In my .resx file, I have 2 key/values, "You have" and "new messages". The code sticks them together with the xx parameter to get the result I need.
    It seems a little tedious, and quickly becomes complicated if I have to code
    resource.string01 + getNumberOfMessages() + resource.string02

     
    Is there a way to send a parameter to the resource provider, so that I only have to maintain one key/value for the above entry? I was figuring my key/value could look something like "You have {0} messages", and the call to the resource would replace the {0} with whatever parameter I provide. Possible out of the box, or do I have to implement my own resource provider to get this?

    /Mort

     

  • Re: Using string resources from .resx - with parameters?

    01-16-2007, 9:40 AM
    Answer
    • Loading...
    • mortdk
    • Joined on 11-24-2006, 12:42 PM
    • Posts 12

    I have worked out one way of doing this:

    My resource key/value is made to look like this: "You have {0} number of messages"
    The call to the resource then replaces  the {0} with whatever I want, ie:

    string text = Resources.UIRes.MyString.replace("{0}", getNumberOfNewMessages());

    It works, but doesn't seem like the best way to do it.

    Any suggestions? 

  • Re: Using string resources from .resx - with parameters?

    01-18-2007, 12:29 AM
    • Loading...
    • GeorgeZ
    • Joined on 11-16-2006, 6:09 AM
    • UpC
    • Posts 552
    mortdk:

    My resource key/value is made to look like this: "You have {0} number of messages"
    The call to the resource then replaces  the {0} with whatever I want, ie:

    string text = Resources.UIRes.MyString.replace("{0}", getNumberOfNewMessages());

     

    This way looks great. Yesl

  • Re: Using string resources from .resx - with parameters?

    10-08-2008, 3:27 PM
    • Loading...
    • Talespinner
    • Joined on 10-20-2005, 12:55 PM
    • Posts 2

    You can also use StringBuilder.Replace, or String.Format.

     string text = Resources.UIRes.MyString.Format("{0}", getNumberOfNewMessages());

    Whatever you can do, or dream you can, begin it. Boldness has genius, power, and magic in it. -- Goethe
Page 1 of 1 (4 items)
Microsoft Communities