Title: Proper syntax for asp.net embedded Resource tags
Tags: asp.net;resources;localization
Question: 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 %&gt;
<asp:Literal ID="litLabel" runat="server" Text="<%$ Resources:ReportTitles, EndOfDay %&gt;"/&gt;
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 %&gt;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 %&gt;"/&gt;
gets the value out of the default US English ReportTitles.resx file.
What's wrong with the 2nd line?  Thanks
Comment: It is set in the Page_Load of the master page, and it happens more or less like this...
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("DE-DE");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("DE-DE");

Comment: Could you provide us with an example how (and _when_) you set CurrentCulture/CurrentUICulture?

Here is the accepted answer: 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. ;)
Comment for this answer: 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. :)


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2010:00:08/26/10', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<asp.net><resources><localization>', 'Title': 'Proper syntax for asp.net embedded Resource tags', 'ViewCount': 1048, 'id': 3576864, 'user_AboutMe': None, 'user_AccountId': 191458.0, 'user_DisplayName': 'Booberry', 'user_Reputation': 1726.0, 'user_Views': 43.0, 'user_id': 432044.0}}