开发者

Can one local .resx string reference another local .resx string?

I am trying to determine if it is possible to add a concatenated string to one of my local .resx files. This example should clarify:

Let's say I have a simple ASP.NET webpage composed of (1) a label whose text is an important keyword (2) an input with required field validation and (3) a button that causes validation to occur:

(lblMyInput)
(txtMyInput)
(rfvMyInput)
(btnSubmit)

Now, inside the resource file for this page, we want to localize the strings for the page's controls. However, for our error message, we want to use the literal name of the input's label. This is were my question is.

PSEUDOCODE: myPage.resx

(1) lblMyInput.Text = "Name"
(2) rfvMyInput.ErrorMessage = "The " + lblMyInput.Text + " field may not be left blank."
(3) btnSubmit.Text = "Submit/Validate"

Is there any way to pull off this type of concatenation of one resource file's string into another string within the same file?

Th开发者_StackOverflow社区anks!


One way to do it would be to have your two resx strings and work thusly:

Resource1: "Hello, this is a {0}" Resource2: "Cookie"

And use the string parser to plop Resource2 into Resource1. A standard solution but not a very good one, as it requires the developer to know about the {0}. It also leads to localization issues if it ever goes off to translation.. not all languages have words in the same order as English.


You should use the GetGlobalResourceObject method inside the .aspx code or in the code behind, and save the string resource in the way

The {0} field may not be left blank.

For example in the .aspx code you could try with:

<asp:RequiredFieldValidator ruant="server" ID="rfvMyInput" ErrorMessage="<%= String.Format((string)GetGlobalResourceObject("GlobalResourceBaseName", "GlobalResourceKey"), lblMyInput.Text) %>" />

or in the code behind:

rfvMyInput.ErrorMessage=String.Format((string)GetGlobalResourceObject("GlobalResourceBaseName", "GlobalResourceKey"), lblMyInput.Text);

By changing the GlobalResourceBaseName and GlobalResourceKey string but the ones you use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜