在运行时更改语言

发布于 2024-08-24 08:11:11 字数 59 浏览 6 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗫嚅 2024-08-31 08:11:12

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

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

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

编辑:如果表单已打开,您将必须处理并重新加载它。或者,您可以使用以下代码,这非常不舒服:

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

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.

冧九 2024-08-31 08:11:12
Thread.CurrentThread.CurrentCulture = new CultureInfo("hi-IN");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("hi-IN");

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文