views:

28

answers:

1

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 %>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

+1  A: 

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://codebetter.com/blogs//images/codebetter_com/raymond.lewallen/89/o_aspNet_Page_LifeCycle.jpg, a overview worth printing and putting on a wall in your vicinity. ;)

Simon Svensson
Thanks for the info. I overrode the InitializeCulture in our BasePage class and that did the trick. As soon as I have the rep I'll vote you up. :)
Booberry