4

如何在 .net 运行时更改语言?有可能的?(即英语到印地语或任何其他语言。)在桌面应用程序中......

4

2 回答 2

4

在之前使用以下代码InitializeComponent()

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

在 ASP 环境中,您可以使用Request.UserLanguages[0]字符串作为语言代码来自动将语言设置为用户选择。

编辑:如果表单已经打开,您将不得不 Dispose 并重新加载它。或者,您可以使用以下代码,这很不舒服:

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

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

根据您组织资源文件的方式,您可以循环执行此操作。

edti2:另一种方法是自动重新启动应用程序。根据应用程序,这可能不合适。

 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:如果经常看到应用程序告诉用户重新启动应用程序以使更改的设置生效。这与第二次编辑相结合可能是许多用例的方式。

于 2010-03-11T12:33:18.463 回答
1
Thread.CurrentThread.CurrentCulture = new CultureInfo("hi-IN");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("hi-IN");

当然,这仅在该语言中有卫星程序集时才会更改该语言。

于 2010-03-11T12:34:21.103 回答