locked
Multi language application in C# RRS feed

  • Question

  • Dear people,

    I am trying to make a multi language application in C#. I am quite new to C# (I have a lot of programming experience in other languages and with other frameworks though) but picking it up quite well. What I basically have now is a Windows Forms project and have followed these steps:

    - I made a form and set the default captions for controls (like labels).
    - Then I set the Localizable property to True and the Language property to Dutch (Netherlands)
    - I set the captions in Dutch and after that reset the Language property to (Default)

    I'm sure this wil work great on computers with different languages, but I want to provide a way for the user to set the language themselves. I have found the folling statement, which I can add before Initializecomponent() in the form, which then switches the language:

    Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-NL"); 


    Well naturally I can set this to "nl-NL" for Dutch and "en-IE" for Ireland. However when I put this code into a button click handler, nothing changes. Do I really have to manually set all the properties of my form again.

    Two other things I noticed are the following. When I create a custom resource (for example 'Lang.resx') and another one for Dutch ('Lang.nl-NL.resx') it works when I do like 'label1.Text = Lang.label1Text' and displays the correct language. But here I also have to manually set it. Even worse, I can't bind this resource to the property (like I can with a Settings file/object).

    I hope someone can point me in the right direction. :-)
    Wednesday, September 10, 2008 10:36 AM

Answers

  • The change to locale will only affect the form when it is being created, so if you change it after the form has been created then it won't have any effect.

    Why not just use the current system regional settings at startup instead.

    Add this line right at the beginning of Main():

    System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;

    Then you'd change language by going to Control Panel -> Regional and Language Options, and select the appropriate language from "Current Format".
    Wednesday, September 10, 2008 11:20 AM
  • Yes, by default things will run in the default system language, NOT the current locale settings (which is why you need to set CurrentUICulture to CurrentCulture to make it use the current locale settings as set via regional settings).

    If you want to do it after some forms have been loaded by your application, you'll have to reload all the forms after making the change.

    Alternatively, you can ask the user to restart the application (I would imagine that people wouldn't be changing the display language that often, so that shouldn't be too much of a pain for them).

    Thursday, September 11, 2008 8:44 AM

All replies

  • The change to locale will only affect the form when it is being created, so if you change it after the form has been created then it won't have any effect.

    Why not just use the current system regional settings at startup instead.

    Add this line right at the beginning of Main():

    System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;

    Then you'd change language by going to Control Panel -> Regional and Language Options, and select the appropriate language from "Current Format".
    Wednesday, September 10, 2008 11:20 AM
  • Thank you for your suggestion. How I understand it, it already automatically takes the default system language if I do it via the Localizable and Language properties of the form. However, I explicitly want to offer the user a settings to change the language of the application, in the case they want to run the application in a different language than the system's language.

    If the setting of the locale/culture only has any effect when the form is being created, does that mean that my only options is to make my own methods to set the language on each component, after it changes?
    Wednesday, September 10, 2008 11:27 AM
  • Yes, by default things will run in the default system language, NOT the current locale settings (which is why you need to set CurrentUICulture to CurrentCulture to make it use the current locale settings as set via regional settings).

    If you want to do it after some forms have been loaded by your application, you'll have to reload all the forms after making the change.

    Alternatively, you can ask the user to restart the application (I would imagine that people wouldn't be changing the display language that often, so that shouldn't be too much of a pain for them).

    Thursday, September 11, 2008 8:44 AM