views:

162

answers:

2

How to change language at run time in .net ? It is possible? (i.e. English to Hindi or any other language.) in desktop application...

+3  A: 

Use the following code before InitializeComponent():

Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");

In an ASP-Environment, you may use the Request.UserLanguages[0] string as language code to automatically set the language to the users choice.

edit: If the form is already opened, you will have to Dispose and reload it. Alternatively, you can use the following code, which is pretty uncomfortable:

System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(MyFormClas));

this.myButton.Text = resources.GetString("myButton.Text");
this.myButton2.Text = resources.GetString("myButton2.Text");
...

Depending on how you organized your ressource files, you may do this in a loop.

edti2: an alternative approach is to restart the application automatically. depending on the application, this might not be suitable.

 if (MessageBox.Show(Resources.QWantToRestart, Resources.Warning,
      MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
 {
     System.Diagnostics.Process.Start(Application.ExecutablePath);
     System.Windows.Forms.Application.Exit();
 }    

edti3: what if often see is that an application tells the user to restart the application in order for the changed settings to take effect. this combined with the second edit is probably the way to go for many use cases.

henchman
yes, but unfortunately it won't update the user interface to reflect the change...
Thomas Levesque
+1  A: 
Thread.CurrentThread.CurrentCulture = new CultureInfo("hi-IN");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("hi-IN");

Of course this only changes the language if there are satelite assemblies in that language.

Lars Truijens