1

I have successfully implemented a multi-language website in which user can change language by clicking one of the imagebuttons. I used a Hiddenfield to store code of language that was selected by the user (on Imagebutton click event). My InitializeCulture method looks like this:

     protected override void InitializeCulture()
    {
    string culture = "Auto";
    string selectedValue = Request.Form["ctl00$HiddenFieldLang"];
    switch (selectedValue)
    {
        case "1": culture = "Auto";
            break;
        case "2": culture = "zh-HK";
            break;
        default: break;
    }

    if (culture != "Auto")
    {
        System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(culture);
        //ci = System.Globalization.CultureInfo.CreateSpecificCulture(culture);
        System.Threading.Thread.CurrentThread.CurrentCulture = ci;
        System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
    }
    base.InitializeCulture();
    }

This works fine. But it takes 2 postbacks to change the language of the page. I'm guessing, in one postback (which changes nothing), the hiddenfield's value is set. In second postback, actual translation takes place.

How do I implement it so that the page is translated in just one click of imagebutton?

Thanks!!

KhD
  • 453
  • 3
  • 11
  • 29
  • Change your ImageButton to a regular button to see if it still takes 2 clicks. I seem to remember there being some issues with ImageButtons and postbacks in certain scenarios. If a regular button clears up your issue, look into using a regular button and setting the background of it to your image. – TheGeekYouNeed Aug 01 '11 at 07:11
  • As I understand it, Khushboo's using the Form collection so it shouldn't really matter what control is used as the control's aren't initialized when InitCulture is called? – Oskar Duveborn Aug 01 '11 at 08:12

3 Answers3

0

You may add client-side handler for imagebutton's click event and set value on the HiddenFieldLang on it. Or you can get an control that caused a postback in InitializeCulture method and if it one of buttons used for switching a language then apply appropriate culture.

The first option in my opinion is preferred as you don't need check which control cause postback each time.

Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68
0

I have implemented the Multi-Language site in ASP.NET and had some issue once... you can find the required code in the Question with an problem's solution regarding Hyperlink in the answer by me itself. And here I think you have to find the Image control and not the HiddenField control

Community
  • 1
  • 1
Harsh Baid
  • 7,199
  • 5
  • 48
  • 92
0

Thank you all for the replies. I managed to solve this problem with the help of this link.

But I have another issue now. I have a masterpage and I put the Hiddenfield and the Javascript function to set the value of hiddenfield in my masterpage. In the URL of all my pages, I have a querystring with the language id. So, whenever the language changes, I want to change the querystring as well.

Is there any way I can fetch the URL of current content page from the masterpage and do a Response.Redirect or Server.Transfer from SetHiddenValue(val) function itself and ensure that the text is translated?

KhD
  • 453
  • 3
  • 11
  • 29
  • You may change window.location just in javascript with a new language parameter and handle this parameter in the InitializeCulture method. This way you don't need an additional hidden field for selected language. – Yuriy Rozhovetskiy Aug 02 '11 at 08:00
  • By language parameter, do you mean using the querystring value in InitializeCulture? I have already tried that, but apparently InitializeCulture() method does not recognize querystring, perhaps because it runs very early in the lifecycle of a webpage. Please correct me if I'm wrong. – KhD Aug 02 '11 at 08:05
  • Try once again. The Request.QueryString must be accessible at any period of page's life cycle. – Yuriy Rozhovetskiy Aug 02 '11 at 08:38
  • wow. Thanks a lot. It is working now. I had tried accessing Querystring value in InitializeCulture earlier and it did not work. I tried again and it works. Thanks a lot for prompting me to try it again. – KhD Aug 02 '11 at 09:33