views:

39

answers:

1

So i have:

App_GlobalResources/MyApp.resx

with a Name/Value pair of "Email" = "Email Address"

In my application, is there a way to override this value? At some point in the application, I want to change the value of the Name/Value pair.

"Email" = "Email Address" ... becomes... "Email" = "Contact Email Address"

And have that referenced the same way, Resources.MyApp.Email.

+2  A: 

Why not just make 2 entries in your resource file? One for "Contact Email Address" and one for "Email Address" and using your code decide which one to show under the circumstances.

So if you are using Email Address as default you would have:

<asp:Label ID="EmailAddressLabel" runat="server" meta:resourcekey="EmailAddressLabel"></asp:Label>

And then to override that you would put something like this in your code behind:

if (your logic here)
{
     Email.Text = (String)GetLocalResourceObject ("ContactEmailAddressLabel");
}

The above is for local resource files. For Global you could use:

Email.Text = (String)GetGlobalResourceObject("MyApp", "ContactEmailAddressLabel");

and that would read the global resource key for you.

Richard Reddy
Thanks Richard, this is definitely one approach, one I'll have to think through a bit. The first issue I see with this is the case where I have an Email Address label in 20 other files. I definitely would not want to put my logic in each code behind file. Ideally, I could set/override Resources.MyApp.Email once, when the application loads and use a single reference. Your idea is definitely nice and straight-forward, just need to find a way to better manage the logic. Thanks for sharing!
Kevin
You could put the logic within a class and just call that class from your code behind if it is used on loads of pages - but you would still need to add something like Email.Text = EmailWording.GetLabelWords(); to your codebehind pages. I feel your pain though, had to retro fit languages into a large site recently. Not fun! Good luck! :)
Richard Reddy
That's exactly what I was thinking last night. Something along the lines of, CustomResource.GetLabel("Email") and based on some system settings, return the proper string from the same global resource file.
Kevin