Change the language at runtime

How to change runtime language in .net? Maybe? (e.g. from English to Hindi or any other language). in a desktop application ...

+4
source share
2 answers

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 can use the Request.UserLanguages[0] as a language code to automatically select a language for users to select.

edit: If the form is already open, you will have to Dispose and reload it. Alternatively, you can use the following code, which is rather inconvenient:

 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 can do this in a loop.

edti2: An alternative approach is to restart the application automatically. Depending on the application, this may not be acceptable.

  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 you often see that the application tells the user to restart the application for the changed settings to take effect. this, combined with the second edit, is probably a way for many use cases.

+4
source
 Thread.CurrentThread.CurrentCulture = new CultureInfo("hi-IN"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("hi-IN"); 

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

+1
source

Source: https://habr.com/ru/post/1303786/


All Articles