用 C #/WinForms 编写多语言应用程序的最佳实践?

我一直在研究如何使应用程序适用于 C # 中的多种语言,因为我需要在一个小项目中进行这方面的工作。我基本上找到了两种方法:

设置表单的 Localability 属性为 true,设置 Language 属性,填充所有标签等,然后就可以“完成”了。我在这里看到的主要缺点是: 如何让表单中没有的其他内容为多种语言做好准备(例如弹出窗口、日志文件或窗口等)。

创建一个资源文件,例如‘ Lang.en-us。Resx’和每种语言一个,例如‘ Lang.nl-nl。然后用字符串填满。IDE 似乎自动为我生成了一个类,所以在代码中我可以直接使用 Lang。一些短信。我在这里看到的最大缺点是: 对于每个表单,我需要自己在代码中设置所有的标签和其他标题(而且似乎数据绑定不适用于这些资源)。

不过,我相信还有其他方法可以做到这一点。

那么,什么是最佳实践呢?对于小型应用程序(一些表单、数据库连接等) ,什么是最容易的? 对于大型应用程序,什么是最好的扩展?

99895 次浏览

I have always used resource files for multi-language applications.
The are many articles on the web explaining how to use them.

I have used two different ways:

  • A resource file per form
  • A global resource file

The resource file / form, is easier to implement, you only need to enter the values in the resource file, but I find this approach harder to maintain, since the labels are dispersed throughout the application.

The global resource file allows you to centralise all the labels (images etc.) in one file (per language), but it means manually setting the labels in the form load. This file can also be used for error messages etc.

A question of taste...

One last point, I write programs in English and French, I use "en" and "fr" and not "en-US" and "fr-FR". Do not complicate things, the different dilelects of English (American, English, Australian etc) have few enough differences to use only one (the same goes for French).

I recently wrote a program with both German and English language support. I was surprised to find out that if I simply named my english resources LanguageResources.resx and my German resources LanguageResources.de.resx, it automatically selected the correct language. The ResXFileCodeGenerator took care of it all for me.

Note that the fields in the two files were the same and any not yet entered German fields would show up in the application as English as the most non specific file language wise is the default file. When looking for a string it goes from most specific (ex .de-DE.resx) to least specific (ex. .resx).

To get at your strings use the ResourceManager.GetString or ResourceManager.GetObject calls. The application should give you the ResourceManager for free.

For the benefit of others who may come across this (1+ years after the last post), I'm the author of a professional localization product that makes the entire translation process extremely easy. It's a Visual Studio add-in that will extract all ".resx" strings from any arbitrary solution and load them into a single file that can be translated using a free standalone application (translators can download this from my site). The same add-in will then import the translated strings back into your solution. Extremely easy to use with many built-in safeguards, lots of bells and whistles, and online help (you won't need it much). See http://www.hexadigm.com