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

陌路散爱 提交于 2019-12-14 01:34:54

问题


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?

Thanks!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3072706/can-one-local-resx-string-reference-another-local-resx-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!