Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force a culture in asp.net?

I want to be able to set the culture during runtime. For example:

protected void Page_Load(object sender, EventArgs e)
{
    Page.Culture = "fr-FR";
    Page.UICulture = "fr";
}

But that's not having any effect. I'm using resource files for translation. If I change the language of my browser it works fine, but I want the user to also be able to choose the language as well. So in this case the user wants French as the language.

Any ideas? I'm lost.

like image 274
Smeegs Avatar asked Nov 30 '22 12:11

Smeegs


2 Answers

You can try with this:

 Thread.CurrentThread.CurrentCulture = 
            CultureInfo.CreateSpecificCulture("en-US");
 Thread.CurrentThread.CurrentUICulture = new 
            CultureInfo("en-US");

Refer this article from MSDN for further details.

like image 29
Santosh Panda Avatar answered Dec 06 '22 05:12

Santosh Panda


If you're creating a site where you are allowing the user to change language for example, then you need to perform this in the Global.asax file in the Application_BeginRequest method.

Each request will then have the culture set.

You simply set the following 2 lines:

    Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR");
    Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-FR");

The first line will set the number/date/etc formatting.

The second line specifies which resource localization to load - which will contain your translated content.

like image 149
Darren Wainwright Avatar answered Dec 06 '22 04:12

Darren Wainwright