Last post Jul 07, 2007 08:33 PM by patrickdrd
Feb 10, 2007 08:18 PM|megetron|LINK
I am using a localize website, that knows which language to select according to the "Languages" properties of ms windows.
What I would like to do is to enable users switch languages without having to change the "Languages" default properties.
Let's say I will have a flags, and each time user will click on a flag, the language will be change.
I guess that there will be a need to save the user language in cookies or something.
Thanks for any advice.
Feb 11, 2007 12:44 AM|aweil|LINK
That's what I did on my site.
Add a LinkButton with the flag image, double click on it in design mode. Add the following code to the event handler that was added:
protected void UseEnglishAction_Click(object sender, ImageClickEventArgs e)
HttpContext context = HttpContext.Current;
context.Session["Language"] = new CultureInfo("en-GB");
// Re-process current page from the start
Also add the following method after that one:
protected override void InitializeCulture()
CultureInfo preferredCulture = HttpContext.Current.Session["Language"] as CultureInfo;
if (preferredCulture != null)
Thread.CurrentThread.CurrentUICulture = preferredCulture;
Thread.CurrentThread.CurrentCulture = preferredCulture;
That's it! It works like a charm on my site...
Hope this helps,
Feb 11, 2007 04:10 AM|megetron|LINK
Thanks, that looks like a good one and I will try it later on today.
In this code when a user clicked on a flag, the new language will set to site language only for this session. this behaviour will force user to click the button on each session. the question is how can I make user click only once, and for each login the
website will "remember" the user and will load the preffered language? I guess this will be done using cookies, or there is another commom action?
Feb 11, 2007 10:21 AM|aweil|LINK
Right, you need to use persistent cookies to remember the user's choice. Begin with this code sample, and then simply replace Session with Cookies: instead of Session["Language"]=... you will write Cookies.Add(new Cookie("Language", ..., true) (or something
like that, I don't have the documentation at hand).
Have fun translating!
Mar 02, 2007 07:02 PM|megetron|LINK
Long time but I am getting back to you. your solution works fine.
Why do you use a page localization? isnt it gonna be much easier to do a template localiztion?
Mar 02, 2007 07:30 PM|aweil|LINK
Well, I'd love to. The master page (I guess that's what you call template) is the most natural place to put the localization code.
However, a master page can't override the InitializeCulture method because its base class just doesn't have this method. So you have to localize each page, which can be done by inheritance.
It's a limitation of master pages, I guess they were coded at the same time as the InitializeCulture method was being introduced, and the coders didn't speak to each other...
Mar 03, 2007 11:03 AM|megetron|LINK
OK I see.
I used your solution, and all my pages are inherit from on base page whixh implements your InitializeCulture solution. means I writing this just once.
Mar 03, 2007 02:12 PM|aweil|LINK
Jul 07, 2007 08:33 PM|patrickdrd|LINK
yes, but what about localizing strings inside the masterpage?
how would I do that?
the masterpage is inheriting from just system.web.ui.masterpage or sth else?