Last post Jul 10, 2007 08:23 AM by Biser
Jul 04, 2007 07:42 PM|akaKlen|LINK
I'm trying to use caching for localized wesite. I have created MyOwnPage (derived from Page) class and overriden InitializeCulture() where I set CurrentThread.CurrentUICulture.
To make Cache useful for localization I set VaryByCustom="uiCulture" for every page in the site. And I overriden GetVaryByCustomString for VaryByCustom to work. Here's the code:
But Thread.CurrentThread.CurrentUICulture here reset back to original value!!! Why? I don't have any other code that sets CurrentUICulture, I double checked. And these two methods got invoked in this order within a request:
Because of this caching does not work properly for selected language!
Jul 06, 2007 06:05 AM|Biser|LINK
Why don't use Session to store it.
Here is my code if you decide to use Session:
Add to MyOwnPage:
protected override void InitializeCulture()
CultureInfo preferredCulture = HttpContext.Current.Session["Language"] as CultureInfo;
if (preferredCulture != null)
Thread.CurrentThread.CurrentUICulture = preferredCulture;
Thread.CurrentThread.CurrentCulture = preferredCulture;
I don't know what kind of controls you use to select Language (ImageButton, Button , DropDown) add to there Event Handlers(for all language that your web site support just change CultureInfo("en-US") for each language ):
HttpContext context = HttpContext.Current;
context.Session["Language"] = new CultureInfo("en-US");
// Refresh current page to display selected language
I hope that i help you.
Jul 07, 2007 08:43 PM|akaKlen|LINK
Biser, you can use Session to store it but inside GetVaryByString method Session does not exist yet. That's why I use CurrentUICulture.
Anyway, I'm talking about completely different thing here - I initialize CurrentUICulture in Page.InitializeCulture() method to 'ru-RU' and then assume I can read CurrentUICulture inside the Application.GetVaryByCustomString(..). But inside the latter the
CurrentUICulture value is 'en-US'! So, the returned value is wrong and wrong cached content is returned (content for 'en-US' culture).
Jul 10, 2007 08:23 AM|Biser|LINK
I look around your problem and found that is some kind
a bug in the Auto-Culture switching in ASP.NET’s page mechanism.
For more information take a look here: http://west-wind.com/WebLog/posts/8485.aspx
Take a look also this thread: http://forums.asp.net/p/889466/941270.aspx
I hope this will solve your problem.