开发者

Proper syntax for asp.net embedded Resource tags

I'm trying to localize an asp.net web application. Consider the following asp.net code. I'm running with CurrentCulture and CurrentUICulture set to German ("DE-DE").

<%= ReportTitles.EndOfDay %>
<asp:Literal ID="litLabel" runat="server" Text="<%$ Resources:ReportTitles, EndOfDay %>"/>

I expect these two lines to yield the same result, but instead I get this:

Auswertungen für den Tagesabschluss

End of Day

In other words, the first syntax <%= ReportTitles.EndOfDay开发者_JAVA百科 %>successfully retrieves the value from the ReportTitles.de.resx file we're using, but the second syntax <asp:Literal ID="litLabel" runat="server" Text="<%$ Resources:ReportTitles, EndOfDay %>"/> gets the value out of the default US English ReportTitles.resx file.

What's wrong with the 2nd line? Thanks


Setting culture during Load is somewhere in the middle of the life cycle. This is after the literal control has been created.

Basically, the control tree is created when Page.ProcessRequest calls FrameworkInitialize, which calls __BuildControlTree, a method autogenerated from your code file. It will instantiate a new Literal control, set all properties, and add it to the control tree. It has basically already read from the active ResourceManager. This is before Load, it's even before PreInit. This means that you havn't changed culture yet.

<%= ... %> will be parsed into a call to HtmlTextWriter.Write during render. This is at the end of the life cycle, and it will use the new culture.

The usual place to implement this is either using a HttpModule/HttpHandler or overriding Page.InitializeCulture.

Check out http://ghferr.free.fr/wiki/Articles/images/aspnet_page-control-life-cycle.jpg, a overview worth printing and putting on a wall in your vicinity. ;)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜