Why does this line of localization behave this way?
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 %> »"
displays as
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 <%$
ac开发者_如何学JAVAtually does.
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") + "»"
.
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).
精彩评论