Search code examples
c#winformsculture.net-4.6.2

How set a culture for entire winform application


I want to set a culture for entire winform application.
How can i do that?
I changed my Program.cs file like this :

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Divar
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            var culture = new CultureInfo("en-US");
            CultureInfo.DefaultThreadCurrentCulture = culture;
            CultureInfo.DefaultThreadCurrentUICulture = culture;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new RadForm1());
        }
    }
}

Did i do it right?


Also there is another link about that :(Please take a look)
https://www.c-sharpcorner.com/forums/set-cultureinfo-for-winform-application

This has limited success, so at the top of "Form1" before the InitializeComponent() I placed:

    System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("en-GB");
    Application.CurrentCulture = cultureInfo;
    System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-GB");

Is it necessary to add those three lines before the InitializeComponent() in every form?


Solution

  • Set these two in Main to the desired culture: CultureInfo.DefaultThreadCurrentCulture CultureInfo.DefaultThreadCurrentUICulture

    Additionally, you can change Application.CurrentCulture whenever you want to change the culture on the current thread of the application.

    [STAThread]
    static void Main()
    {
        var culture = CultureInfo.GetCultureInfo("en-US");
    
        // this may fail sometimes: (see Drachenkatze's comment below)
        // var culture = new CultureInfo("en-US");
    
        //Culture for any thread
        CultureInfo.DefaultThreadCurrentCulture = culture;
    
        //Culture for UI in any thread
        CultureInfo.DefaultThreadCurrentUICulture = culture;
    
        //Culture for current thread (STA)
        //no need for: Application.CurrentCulture = culture;
    
        //Thread.CurrentThread.CurrentCulture == Application.CurrentCulture
        //no need for: Thread.CurrentThread.CurrentCulture = culture;
    
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new RadForm1());
    }