在 C#/WinForms 中制作多语言应用程序的最佳实践?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/119568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best practice to make a multi language application in C#/WinForms?
提问by
I've been looking into making applications suitable for multiple languages in C# since I need to work on a small project where this is the case. I have found basically two ways to do this:
我一直在研究使应用程序适用于 C# 中的多种语言,因为我需要在这种情况下处理一个小项目。我发现基本上有两种方法可以做到这一点:
Set a form's Localizable property to true, set the Language property, fill all the labels and such, and you're 'done'. The major drawback I see in this is: how to make other stuff which is not part of a form ready for multiple languages (e.g. pop-up windows, log files or windows, etc).
将表单的 Localizable 属性设置为 true,设置 Language 属性,填充所有标签等,然后就完成了。我在这方面看到的主要缺点是:如何为多种语言准备不属于表单的其他内容(例如弹出窗口、日志文件或窗口等)。
Create a resource file, for example 'Lang.en-us.resx' and one for every language, for example 'Lang.nl-nl.resx' and fill it up with Strings. The IDE seems to generate a class for me automatically, so in the code I can just use Lang.SomeText. The biggest drawback I see in this is: for every form I need to set all the labels and other captions myself in the code (and it doesn't seem data binding works with these resources).
创建一个资源文件,例如“Lang.en-us.resx”,为每种语言创建一个资源文件,例如“Lang.nl-nl.resx”并用字符串填充它。IDE 似乎会自动为我生成一个类,所以在代码中我可以只使用 Lang.SomeText。我在这方面看到的最大缺点是:对于每个表单,我都需要自己在代码中设置所有标签和其他标题(而且似乎数据绑定不适用于这些资源)。
I'm sure, however, that there are other methods to do this as well.
但是,我确信还有其他方法可以做到这一点。
So, what is the best practice? What's the easiest for small applications (a few forms, database connection etc) and what scales best for larger applications?
那么,最佳实践是什么?对于小型应用程序(一些表单、数据库连接等)来说,什么是最简单的,对于大型应用程序来说,什么是最好的?
回答by ThatBloke
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).
最后一点,我用英语和法语编写程序,我使用“en”和“fr”而不是“en-US”和“fr-FR”。不要把事情复杂化,英语的不同方言(美式、英语、澳大利亚等)几乎没有足够的差异,只能使用一种(法语也是如此)。
回答by Rick Minerich
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.
我最近编写了一个支持德语和英语的程序。我惊讶地发现,如果我简单地将我的英语资源命名为 LanguageResources.resx 和我的德语资源 LanguageResources.de.resx,它会自动选择正确的语言。ResXFileCodeGenerator 为我处理了这一切。
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).
请注意,这两个文件中的字段是相同的,任何尚未输入的德语字段将在应用程序中显示为英语,因为最非特定的文件语言是默认文件。在查找字符串时,它从最具体(例如 .de-DE.resx)到最不具体(例如 .resx)。
To get at your strings use the ResourceManager.GetString or ResourceManager.GetObject calls. The application should give you the ResourceManager for free.
要获取您的字符串,请使用 ResourceManager.GetString 或 ResourceManager.GetObject 调用。该应用程序应该免费为您提供 ResourceManager。
回答by Larry
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
为了可能遇到此问题的其他人(在上一篇文章之后 1 年以上)的利益,我是专业本地化产品的作者,该产品使整个翻译过程变得非常简单。它是一个 Visual Studio 插件,可以从任意解决方案中提取所有“.resx”字符串,并将它们加载到可以使用免费的独立应用程序进行翻译的单个文件中(翻译人员可以从我的网站下载)。然后,同一个加载项会将翻译后的字符串重新导入到您的解决方案中。非常易于使用,具有许多内置保护措施、大量花里胡哨和在线帮助(您不会太需要它)。见http://www.hexadigm.com