.resx files and variables?

This is an open forum for any mojoPortal topics that don't fall into the other categories.

This thread is closed to new posts. You must sign in to post in the forums.
2/9/2012 2:39:17 PM
Gravatar
Total Posts 40

.resx files and variables?

I was reading about how the localization is done using .resx files and I'm wondering if it is possible to include a placeholder or variable in the actual value field of the .resx file that would then be replaced with that variables value when written to screen.

For example let's say the text in the value field is "About your tax return:". Now if I wanted to make it so that every year it just wrote the year in automagically, like: "About your [insert YEAR] Tax Return", where it replaces [insert YEAR] with the actual year, what's the syntax for that?

2/9/2012 3:25:53 PM
Gravatar
Total Posts 2210

Re: .resx files and variables?

Hi,

The answer lies in string.Format.

In resx file, use "About your {0} Tax Return" for the string value.

Then, in code, use string.format to place the year in the string:

lblAboutTaxReturn.text = String.Format(CultureInfo.InvariantCulture, Resources.myresources.AboutTaxReturnLabel, taxReturnYear);

lblAboutTaxReturn is the label that will be populated with the "About your ... Tax Return" text.

Resources.myresources.AboutTaxReturnLabel is a reference to the resx and the string you want to use.

taxReturnYear is the variable holding the year value for the tax return. 

You can read more about string formatting here.

HTH,
Joe D. 

You must sign in to post in the forums. This thread is closed to new posts.