0

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

bluish
  • 26,356
  • 27
  • 122
  • 180
weiszam
  • 187
  • 4
  • 14
  • Correct me if I am wrong, but localization in crapwinforms occurs only when you create an instance of the form. If form is already shown - it's fail. You have to recreate forms after changing language or use custom made localization manager. I found the best to use reflection to translate everything on the fly. – Sinatr Jul 18 '13 at 12:05
  • Alright, but how can I solve that, changing the language, and then restarting the application. How would that work? – weiszam Jul 18 '13 at 12:08

2 Answers2

0

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);
    }
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • And if I want to restart the software, with a different language? There's some error at the SetCompatibleTextRenderingDefault: SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application. – weiszam Jul 18 '13 at 12:20
0

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.

weiszam
  • 187
  • 4
  • 14