2

I'm localizing an ASP.NET web site. Usually to localize text in an .aspx page I just use

<%= Resources.ResourceFile.ResourceName %>

For asp.net controls, this won't work. I have to use the syntax

<%$ Resources:ResourceFile, ResourceName %>

However, if I have a button and localize the Text property that way, but add any additional characters after it, the localization breaks and it shows as plaintext.

So Text="<%$ Resources:ResourceFile, ResourceName %> &raquo;" displays as
<%$ Resources:ResourceFile, ResourceName %> »

I'm sure there is a valid reason for this, I just can't find the explanation on MSDN on how the Text property evaluates this. I'm not even 100% sure on what the <%$ actually does.

Rob
  • 45,296
  • 24
  • 122
  • 150
Brandon
  • 68,708
  • 30
  • 194
  • 223

1 Answers1

1

What's happening is that ASP.net is invoking an Expression Builder. What effectively happens here is that rather than the ASP.net compiler translating your:

<asp:AControlWithATextProperty runat="server" Text="Some Text">

to:

AControlWithATextProperty ctl1 = new AControlWithATextProperty();
ctl1.Text = "Some Text";

When it transforms the markup in the .aspx file into a .cs file combined with the code-behind, it actually does something similar to this:

<asp:AControlWithATextProperty runat="server" Text="<%$ Resources:ResourceFile, ResourceName %>">

Becomes:

AControlWithATextProperty ctl1 = new AControlWithATextProperty();
ctl1.Text = ResourceExpressionBuilder.EvaluateExpression("ResourceFile, Resourcename");

It would seem that the asp.net compiler can't handle concatenating the content of the <%$ %> tags with any further text in the property from markup. Either a bug, or by design. i.e. You don't end up with ctl1.Text = ResourceExpressionBuilder.EvaluateExpression("ResourceFile, Resourcename") + "&raquo;".

You can read more about the ResourceExpressionBuilder on msdn, ExpressionBuilder in general, or if you really want to; an implementation of one for localisation (database backed, hence the fact that I didn't use the ResourceExpressionBuilder) on my blog (3 parts).

Rob
  • 45,296
  • 24
  • 122
  • 150
  • Thanks for the thorough explanation. Out of curiosity, do you think I should file a possible bug report regarding the concatenation? – Brandon Feb 16 '11 at 21:32
  • @Brandon, I'm honestly not sure, it can't hurt to raise a connect issue against this (if there's not one already) and see what the response from Microsoft is =) – Rob Feb 17 '11 at 08:48