13 June 2007

ASP.NET Localization Catch-22

Posted by Barnabas under: How To; Programming .

nihongoAgain, sorry for the tech-heavy post, but I like to keep things that I learn here as sort of a permanent notebook. I’ve found many answers to my problems thanks to people who’ve written down their solutions, so it’s payback time for me.

If you’re using ASP.NET localization to allow your site visitors to set their UI language, then there’s an interesting catch-22. When a visitor changes their language settings, they would expect the confirmation message to be in the language they picked. For example, if they’re changing their settings from the default English to Spanish, you would expect the next page they see to say “Gracias”, not “Thank you”. If you do what I did, you might set Thread.CurrentThread.CurrentUICulture to a new CultureInfo object in the event handler of a Save button, then scratch your head and wonder why the language didn’t change.

It turns out that the CurrentUICulture did change, but it was too late: the correct page event to set the new culture is Pre-Init, way before the control event handlers fire. Unfortunately, at Pre-Init, the controls haven’t been loaded either, unlike Init or Load. In order to determine what the new language is supposed to be, you’ll have to extract that value from the request form classic ASP style, which I’ll leave as an exercise to the reader.

Leave a Reply

Categories

Archives

Links

Meta