Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a multilingual site in asp.net

I am creating a website that will be multilingual in nature. I am providing a functionality whereby the user can select desired language by selecting it from the drop down.

Now my problem starts here (when a language is selected from the drop down). I am currently implementing 2 languages English and Arabic.

Problem is when the user select Arabic from the drop down on the login page the page is refreshed and the browser loads all the content in Arabic.

But...

When i select English back again, the page refreshes but the language content does not change !!

i have check the code and the values (culture name value) are being applied correctly!!

Any clues as to what is wrong and where...

Here is my code...

protected override void InitializeCulture()
{
    String selectedLanguage = string.Empty;

    if (Request.Form["ddlLanguage"] != null)
    {
        selectedLanguage = Request.Form["ddlLanguage"];

        CultureInfo ci = new CultureInfo(selectedLanguage);

        Thread.CurrentThread.CurrentCulture = ci;
        Thread.CurrentThread.CurrentUICulture = ci;
    }

    base.InitializeCulture();
}

Any help will be great !!

Thanks

like image 700
Shrewdroid Avatar asked Dec 17 '25 03:12

Shrewdroid


1 Answers

The Culture settings must set on each request. It's not enough to set the Thread cultures once when the selection changes.

In order to set the culture according to the user selection on each request, there are several possible ways.

  1. Set the culture in (for instance) the Page_Init event of the master page.
  2. Create custom base page (maybe MyBasePage) for the content pages and override the InitializeCulture method of that class. Then make all the content pages derive from the class instead of directly from Page.
  3. Add code for setting the culture in a suitable event in Global.asax.
  4. Probably there are several other possible ways to handle this as well.

Regardless of which method you will use, the culture that the user has chosen must be available to the code that is going to set the culture on the thread. So when the user changes his/her selection, you must save that selection in a place where you can access it in the coming requests. This can also be solved in several possible ways. Here are some options:

  1. If you are using a ProfileProvider, you could save the selection to the user's profile.
  2. Alternatively you can store it in a cookie.
  3. You could possibly store it in the Session although that would mean that the user must re-select it whenever the Session has been reset.
  4. You could use some other custom way to store the selection (database, some file on disk etc).

For a more detailed exmple of how this could be done using Global.asax and a cookie, have a look over here.

like image 60
user1429080 Avatar answered Dec 19 '25 20:12

user1429080



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!