简体   繁体   English

Visual Studio 2010 - 更改语言属性

[英]Visual Studio 2010 - changing Language property

Is it possible to change the Language property of the Main Form via code?是否可以通过代码更改主窗体的语言属性? If so, how?如果是这样,如何?

Details: In the Config file I have set a lang="EN".详细信息:在配置文件中,我设置了 lang="EN"。 What I want is to use the Localization setting to change the Main Form dependent on this variable.我想要的是使用本地化设置来更改依赖于此变量的主窗体。 I have set the Localizable property to True .我已将Localizable属性设置为True

For example:例如:

if (Config.lang == "FR")
{
    //change **Language** property to "French"
}
else
{
    //remain (Default)
}

The issue is that I don't see the Language property anywhere in the coding window no matter where I look so I'm wondering if it's even possible to do this.问题是无论我在哪里看,我都没有在编码窗口中的任何地方看到Language属性,所以我想知道是否可以这样做。

Because of the way resources are managed in .Net UI programs, the easiest way to do this is to set the thread locale appropriately right at the start of the program before you create any forms.由于在 .Net UI 程序中管理资源的方式,最简单的方法是在创建任何表单之前在程序开始时适当地设置线程区域设置。

Firstly determine the culture you want from your config file:首先从配置文件中确定您想要的文化:

CultureInfo culture = ... whatever

Then set the main thread's CurrentUICulture and CurrentCulture to that locale:然后将主线程的CurrentUICultureCurrentCulture设置为该语言环境:

System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
System.Threading.Thread.CurrentThread.CurrentCulture   = culture;

This code would go into program.cs at the start of Main() .此代码将在Main()开始时进入program.cs

Note that if you start any other threads that display any UI (or output any localised data), you will also need to do the same thing at the start of those threads.请注意,如果您启动任何其他显示任何 UI(或输出任何本地化数据)的线程,您还需要在这些线程开始时执行相同的操作。

Also note that a major limitation of this approach is that you cannot change the locale while the program is running and have the UI update to reflect it.另请注意,此方法的一个主要限制是您无法在程序运行时更改区域设置并更新 UI 以反映它。 You have to set the locale at the beginning of the program.您必须在程序开始时设置语言环境。

There are other solutions which avoid this limitation, but they are waaay more complicated.还有其他解决方案可以避免这种限制,但它们更复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM