Change language at runtime
How to change language at run time in .net ?开发者_运维问答 It is possible? (i.e. English to Hindi or any other language.) in desktop application...
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.
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.
精彩评论