Question

I'd like an option, to change the language in C#, Visual Studio. I have the Form1.resx which holds the Default language items. I also have Form1.en.resx, and Form1.en-US.resx, which holds the english translations. (Of course I only need one of them, but while testing I created both.)

When I run the application, the Default language captions appear. But the following code should overwrite this:

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

When I check the CurrentCulture or CurrentUICulture values, they are indeed changed to "en-US", after this code, but the same resx file is used, so the captions won't change. So for excample label1.Text stays the same though it has a different value in Form1.en-US.resx.

How should I solve this problem?

Additional information: I use .NET Framework 4.5, and Visual Studio 2012. The project files (really simple project) are avaible here if you need them: https://dl.dropboxusercontent.com/u/36411041/Multi.zip

Était-ce utile?

La solution 2

Sinatr was right about changing the language after the form is showed: it can't be done. But if you know the wanted language before that, the following code should do it.

    public Form1()
    {
        //Set the wanted language here
        CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");

        CultureInfo.DefaultThreadCurrentCulture = culture;
        CultureInfo.DefaultThreadCurrentUICulture = culture;
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;


        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        //...
    }

Thank you for your help while figuring this out.

Autres conseils

Try to recreate forms when you change language. One possibility:

    /// <summary>
    /// Program entry point
    /// </summary>
    [STAThread]
    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        // ...

        // close the form after you change the language:
        //     DialogResult = DialogResult.Cancel;
        // exit with any other value

        DialogResult result;
        do
        {
             MainForm form = new MainForm();
             result = form.ShowDialog()
        } while(result == DialogResult.Cancel);
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top