在运行时更改语言
Posted
技术标签:
【中文标题】在运行时更改语言【英文标题】:Change language at runtime 【发布时间】:2010-03-11 12:30:16 【问题描述】:如何在 .net 运行时更改语言?有可能的? (即英语到印地语或任何其他语言。) 在桌面应用程序中...
【问题讨论】:
网络应用还是桌面应用?请提供更多信息 【参考方案1】:在InitializeComponent()
之前使用以下代码:
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");
在 ASP 环境中,您可以使用 Request.UserLanguages[0]
字符串作为语言代码来自动将语言设置为用户选择。
edit:如果表单已经打开,您将不得不 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: 如果经常看到应用程序告诉用户重新启动应用程序以使更改的设置生效。这与第二次编辑相结合可能是许多用例的方式。
【讨论】:
是的,但遗憾的是它不会更新用户界面以反映更改...【参考方案2】:Thread.CurrentThread.CurrentCulture = new CultureInfo("hi-IN");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("hi-IN");
当然,这只会在该语言中存在卫星程序集时更改该语言。
【讨论】:
以上是关于在运行时更改语言的主要内容,如果未能解决你的问题,请参考以下文章