如何在 WinForm 应用程序中尊重/继承用户的语言设置?
我过去曾使用过全球化设置,但没有在 .NET 环境中使用过,这是本问题的主题。我所看到的肯定是由于我尚未学习的知识所致,因此我希望对以下内容有所了解。
设置: 我的默认语言设置是英语(特别是 en-us)。我在我的开发系统(WinXP)上添加了第二种语言(丹麦语),然后打开语言栏,以便我可以随意选择。
我在语言栏上选择了丹麦语,然后打开记事本,发现语言栏上的语言恢复为英语。据我了解,语言设置是每个应用程序,因此记事本似乎将默认设置回英语。 (我发现这很奇怪,因为世界各地都使用 Windows,因此记事本也被使用。)关闭记事本将语言栏上的设置返回到丹麦语。然后,我启动了我的开放式自定义 WinForm 应用程序(我知道它不会设置语言),并且它在打开时也从英语恢复为丹麦语,然后在终止时恢复为丹麦语!
问题#1A:如何让我的 WinForm 应用程序在启动时继承语言栏的当前设置?我的实验似乎表明每个应用程序都以系统默认值启动,并要求用户在应用程序运行后手动更改它——这对于想要使用多种语言的任何人来说似乎都是一个很大的不便!
问题#1B:如果事实上必须在多语言场景中手动设置语言,我如何更改我的默认系统语言(例如丹麦语),以便我可以测试我的应用程序的启动另一种语言?
我在下一个实验的应用程序中添加了当前语言的显示。具体来说,我在标签上设置了一个 MouseEnter
处理程序,将其工具提示设置为 CultureInfo.CurrentCulture.Name
,因此每次我将鼠标悬停在上面时,我都认为应该看到当前的语言设置。由于在启动应用程序之前设置语言不起作用,因此我启动了应用程序,然后将语言设置为丹麦语。我发现有些事情(比如在文本框中输入)确实尊重这种丹麦设置。但将鼠标悬停在仪表标签上仍然显示 en-us!
问题 #2A:为什么 CultureInfo.CurrentCulture.Name
没有反映我的语言栏中的更改,而我的应用的其他部分似乎可以识别该更改? (尝试 CultureInfo.CurrentUICulture.Name
产生了相同的结果。)
问题 #2B: 是否有一个事件会在语言栏发生更改时触发,以便我可以在应用程序中进行识别语言设置何时更改?
2010.05.13 更新
Microsoft 的 Eric 提供的简短而甜蜜的信息(请参阅下面的答案)仅直接解决了我的四个问题之一 (#2A),但它提供了我进一步深入研究并找出其余问题所需的动力。为了其他可能对此感到困惑的人的利益,以下是我发现的内容:
答案#1A:应用程序继承默认输入语言的设置,而不您在语言栏上指定的语言。一旦您的应用程序运行,您的应用程序将立即注意到语言栏上的更改。
答案#1B:通过“区域和语言选项”控制面板>>设置默认输入语言。语言选项卡>>详情>>设置选项卡>>默认输入语言。
答案#2A:Eric回答,当前的文化与语言栏上反映的当前输入语言不同;在文本框中键入内容仅受当前输入语言的影响。
答案#2B:没有针对输入语言或当前文化更改通知的预定义事件。这里需要注意的一个重要事实是,输入语言的更改会立即自动识别,而当前的文化更改则不能。您必须重新启动应用程序才能使当前的文化更改生效 - 除非您自己能够注意到更改并采取行动。为此,我找到了一篇 MSDN 文章(CultureInfo 类的多面性),它提供了这样一个钩子来注意到变化。
I have worked with globalization settings in the past but not within the .NET environment, which is the topic of this question. What I am seeing is most certainly due to knowledge I have yet to learn so I would appreciate illumination on the following.
Setup:
My default language setting is English (en-us specifically). I added a second language (Danish) on my development system (WinXP) and then opened the language bar so I could select either at will.
I selected Danish on the language bar then opened Notepad and found the language reverted to English on the language bar. I understand that the language setting is per application, so it seemed that Notepad set the default back to English. (I found that strange since Windows and thus Notepad is used all over the world.) Closing Notepad returned the setting on the language bar to Danish. I then launched my open custom WinForm application--which I know does not set the language--and it also reverted from English to Danish when opened, then back to Danish when terminated!
Question #1A: How do I get my WinForm application upon launch to inherit the current setting of the language bar? My experiment seems to indicate that each application starts with the system default and requires the user to manually change it once the app is running--this would seem to be a major inconvenience for anyone that wants to work with more than one language!
Question #1B: If one must, in fact, set the language manually in a multi-language scenario, how do I change my default system language (e.g. to Danish) so I can test my app's launch in another language?
I added a display of the current language in my application for this next experiment. Specifically I set a MouseEnter
handler on a label that set its tooltip to CultureInfo.CurrentCulture.Name
so each time I mouse over I thought I should see the current language setting. Since setting the language before I launch my app did not work, I launched it then set the language to Danish. I found that some things (like typing in a TextBox) did honor this Danish setting. But mousing over the instrumented label still showed en-us!
Question #2A: Why does CultureInfo.CurrentCulture.Name
not reflect the change from my language bar while other parts of my app seem to recognize the change? (Trying CultureInfo.CurrentUICulture.Name
produced the same result.)
Question #2B: Is there an event that fires upon changes on the language bar so I could recognize within my app when the language setting changes?
2010.05.13 Update
The short but sweet information provided by Eric from Microsoft (see his answer below) directly addressed only one of my four questions (#2A) but it provided just the impetus I needed to delve further and figure out the rest. For the benefit of others who may also be befuddled by this, here is what I uncovered:
Answer #1A: An application inherits the setting of the default input language, not the language you specify on the language bar. Once your application is running, then changes on the language bar will be noticed immediately by your app.
Answer #1B: Setting the default input language is done via Regional and Language Options control panel >> Languages tab >> Details >> Settings tab >> default input language.
Answer #2A: Answered by Eric, the current culture is distinct from the current input language that is reflected on the language bar; typing in a text box is influenced only by the current input language.
Answer #2B: There is no predefined event for either input language or current culture change notification. An important fact to note here is that input language changes are automatically recognized immediately while current culture changes are not. You must restart your application for a current culture change to take effect--unless you can notice the change and act upon it yourself. To that end I found an MSDN article (The Many Faces of the CultureInfo Class) that provides just such a hook to notice the change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
存在三种不同的语言设置。语言栏控制输入语言 (InputLanguage),它不同于用于显示 UI 的语言 (CurrentUICulture),而用于显示 UI 的语言又不同于用于排序/格式化/解析的语言 (CurrentCulture)。
确实,这令人困惑。
There are three separate language settings at play. The language bar controls the input language (InputLanguage) which is distinct from the language for displaying UI (CurrentUICulture) which is distinct from the language for sorting/formatting/parsing (CurrentCulture).
It is true that this is confusing.