0

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"));
        }
    }
  • Need much more code. How are you swapping between languages? Is this a form or a web app? Please provide a minimal but verifiable example - we should be able to copy the code and run it to see the issue you are having. – nhouser9 Apr 24 '16 at 01:18
  • Thanks, but you ignored the rest of my post... Please see this for how to ask questions: http://stackoverflow.com/help/mcve – nhouser9 Apr 24 '16 at 01:20
  • I didn't ignore it. Here is entire code –  Apr 24 '16 at 01:24
  • Sorry if I was unclear - the example you provide needs to be something we can run. That is the code for one button. It is also important (i assume) for us to see how you are applying the language to the form itself. If we can't run it and reproduce the issue it's hard to help. We can't run the code for one button. – nhouser9 Apr 24 '16 at 01:27
  • Check this out it might help you achieve your goal. http://codereview.stackexchange.com/questions/721/automatic-translation-of-forms – jegtugado Apr 24 '16 at 01:29
  • Thanks. But nhourser9 I still don't get what you want me to do? I provided you ALL my code which relates to me attempting to change the language. I do have other code but for other stuff not anything language based. –  Apr 24 '16 at 01:36

1 Answers1

1

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);
}
Community
  • 1
  • 1
Gabor
  • 3,021
  • 1
  • 11
  • 20
  • Thanks I will try this soon. –  Apr 24 '16 at 03:29
  • I get error on apply button cannot find cboCultureInfo and ResourceLoader. Also how do I get this to work on the item selected in the combobox? Also what are the parameters to call the method? –  Apr 24 '16 at 08:33
  • @DialUp In my example `cboCultureInfo` is the name of the combobox. I use its `SelectedItem` property. So replace `cboCultureInfo` name with the name you specified for the combobox. The `ResourceLoader` class I assume is in a namespace you specified. Make sure you access that namespace from the event handler method. Extend the method call with the namespace if needed, e.g. `YourNamespace.ResourceLoader.ChangeLanguage(this, cultureInfo);` or put `using YourNamespace;` to the using statements where your event handler is defined. – Gabor Apr 24 '16 at 08:41
  • @DialUp Additionaly the combobox should contain language codes e.g. `en-US`, `fr-FR`, etc. to make this example work. If you would like to list friendly names to the user then you may create a `Dictionary` type to easily get the appropriate `CultureInfo` based on the user selection. E.g. `Dictionary languages = new Dictionary() { { "English", new CultureInfo("en-US") }, { "French", new CultureInfo("fr-FR") } };` then in the event handler you can use this: `var cultureInfo = languages[cboCultureInfo.SelectedItem.ToString()];` – Gabor Apr 24 '16 at 08:48
  • Ah ok bit confusing but I'll change the combobox part. Oh yes I missed the resource class some how lol –  Apr 24 '16 at 11:02
  • Didn't work but its all good man I appreciate all the effort :) –  Apr 24 '16 at 11:08
  • I can share my working example solution with you if you would like to get it or we can have a screen sharing session (e.g. with TeamViewer) where I can help you further. (I can share my screen if it is more suitable for you.) Anyway, if you consider my answer to be a solution, please mark it as accepted. Thanks. :) – Gabor Apr 24 '16 at 15:34
  • If you want wanna add me on Steam? –  Apr 25 '16 at 01:06