Search code examples
c#asp.net.netglobalization

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.


Solution

  • 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.