Search code examples
c#cultureuiculture

Set a culture on c# backend page


I'm trying to set the culture and culture ui in the Page_Load of a .aspx.cs page. I have tried

Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-CA");

and

CultureInfo ci = new CultureInfo("fr-CA");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;

and

Culture = "fr-CA";
UICulture = "fr-CA";

but none of them work. Does anyone know how to set the culture and uiculture?

Thank you!


Solution

  • I found a solution: override the InitializeCulture. This is what it looks like when I incorporated cookies and use the current cookie to set the language of the page.

    protected override void InitializeCulture()
        {
    
            HttpCookie cookie = Request.Cookies["CurrentLanguage"];
            if (!IsPostBack && cookie != null && cookie.Value != null)
            {
                if (cookie.Value.ToString() == "en-CA")
                {
                   // currently english
                    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-CA");
                    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-CA");
                    base.InitializeCulture();
                }
                else
                {
                   //currently french
                    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-CA");
                    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-CA");
                    base.InitializeCulture();
                }
            }
        }