user5417424
user5417424

Reputation:

C# How to select language?

Hey guys I been working on a code in my combobox I got some items in it 3 languages english, french, and german I'm hoping when I press the apply button on my program all of the text changes in the form, though can't get it to work:

private void ApplyButtonOptions_Click(object sender, EventArgs e)
    {
        Properties.Settings.Default.Save();

        if (comboBox1.SelectedItem.ToString() == "English")
        {
            comboBox1.SelectedItem = englishLanguage;
        }

        if (comboBox1.SelectedItem.ToString() == "German")
        {
            comboBox1.SelectedItem = GermanLanguage;
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new System.Globalization.CultureInfo("de"));
        }
    }

Upvotes: 0

Views: 755

Answers (1)

Gabor
Gabor

Reputation: 3246

First set the UI controls' text (and size if needed) for all the languages you want to support.

Here is how: https://msdn.microsoft.com/en-us/library/y99d1cd3%28v=vs.71%29.aspx

Then you have to create a method that will update all the UI controls on the current Form. You may create this method in a separate static helper class like this:

public static class ResourceLoader
{
    public static void ChangeLanguage(System.Windows.Forms.Form form, System.Globalization.CultureInfo language)
    {
        var resources = new System.ComponentModel.ComponentResourceManager(form.GetType());

        foreach (Control control in form.Controls)
        {
            resources.ApplyResources(control, control.Name, language);
        }

        // These may not be needed, check if you need them.
        Thread.CurrentThread.CurrentUICulture = language;
        Thread.CurrentThread.CurrentCulture = language;
    }
}

This code is based on Suprotim Agarwal's article.

Read about the differences between CurrentCulture and CurrentUICulture here: What is the difference between CurrentCulture and CurrentUICulture properties of CultureInfo in .NET?

In the button click event handler you only have to call this method:

private void ApplyButton_Click(object sender, EventArgs e)
{
    var cultureInfo = new System.Globalization.CultureInfo(cboCultureInfo.SelectedItem.ToString());

    ResourceLoader.ChangeLanguage(this, cultureInfo);
}

Upvotes: 1

Related Questions