C# WinForms 应用程序的正确本地化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1251225/
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
Proper localization of a WinForms application
提问by Michael Stum
I have a WinForms
application which I want to translate into multiple languages. However, I do not have any experience with localizing a WinForms
app, and I find very contradictory information about this subject.
我有一个WinForms
应用程序,我想将其翻译成多种语言。但是,我没有本地化WinForms
应用程序的任何经验,而且我发现有关此主题的信息非常矛盾。
Basically, what I want is:
基本上,我想要的是:
- In the source code, I want only one file per language
- This file gets compiled into the main application on compilation - no satellite assemblies or external data files after building the application
- The user can select the language, I do not need/want auto-detection based on the operating system
- This should mainly contain
strings
andints
, but also aCultureInfo
- 在源代码中,我只需要一种语言的文件
- 该文件在编译时被编译到主应用程序中 - 构建应用程序后没有附属程序集或外部数据文件
- 用户可以选择语言,我不需要/想要基于操作系统的自动检测
- 这应该主要包含
strings
和ints
,但也是一个CultureInfo
Most solutions I've seen either have one .resx
file perForm
and/or external satellite assemblies.
我见过的大多数解决方案每个和/或外部附属程序集都有一个.resx
文件。Form
- Do I have to roll my own?
- Or is there something in the framework already?
- 我必须自己滚动吗?
- 或者框架中已经有什么东西了吗?
.net Framework 3.5 SP1
if that matters.
.net Framework 3.5 SP1
如果这很重要。
Edit:
编辑:
For the most part, Visual Studio already offers support for what I want, but there are two issues. When I set Form.Localizable
to true
I have this nice Designer
support, but this generates one resx
per Form
. The idea of manually overriding it in InitializeComponent
fails because it's designer-written code that will regularly be overwritten.
大多数情况下,Visual Studio 已经为我想要的东西提供了支持,但有两个问题。当我设置Form.Localizable
为true
我有很好的Designer
支持时,但这会resx
为每个Form
. 手动覆盖它的想法InitializeComponent
失败了,因为它是设计者编写的代码,会定期被覆盖。
Theoretically, I only want to :
理论上,我只想:
- a) override the creation of the
ComponentResourceManager
to point it to my globalresx
and - b) change the call to
ApplyResources
to the overload that takes aCultureInfo
as third parameter.
- a) 覆盖的创建
ComponentResourceManager
指向我的全局resx
和 - b) 将调用更改为
ApplyResources
将 aCultureInfo
作为第三个参数的重载。
It seems as if I have to add a function call to my constructor that gets called after InitializeComponent()
and overrides its behaviour. That seems terribly inefficient, but Visual Studio is right when it warns about touching InitializeComponent()
.
似乎我必须向我的构造函数添加一个函数调用,该函数调用被调用InitializeComponent()
并覆盖其行为。这似乎非常低效,但 Visual Studio 在警告触摸InitializeComponent()
.
At the moment, I am indeed rolling my own WinForms localization Framework
...
目前,我确实在滚动自己的WinForms localization Framework
......
回答by marc_s
I was asking a similar question about ASP.NET and got a first answer - this tool and its workflow might also be something for you - have a look: Lingobit Localizer
我问了一个关于 ASP.NET 的类似问题并得到了第一个答案——这个工具及其工作流程可能也适合你——看看:Lingobit Localizer
It seems to be able to load your Winforms app and allows you to start translating your labels etc. and see the forms while you do it. Lots of other features, too, like incremental translation and translation memory (if you use the same terms over and over again).
它似乎能够加载您的 Winforms 应用程序,并允许您开始翻译您的标签等,并在您执行此操作时查看表单。还有许多其他功能,例如增量翻译和翻译记忆库(如果您一遍又一遍地使用相同的术语)。
Looks quite promising (for Winforms) - haven't used it myself, though.
看起来很有希望(对于 Winforms) - 不过我自己还没有使用过。
Here's an extensive list of potential .NET localization tools- not sure, how well they work and what they cover - have a look, maybe you'll find what you're looking for.
这是潜在的 .NET 本地化工具的广泛列表- 不确定,它们的工作情况和涵盖的内容 - 看看,也许你会找到你正在寻找的东西。
Marc
马克
回答by grenade
This is a huge subject and there are many ways to accomplish what you want. The framework does provide the basis but a complete solution requires that you implement certain elements yourself.
这是一个巨大的主题,有很多方法可以完成您想要的。该框架确实提供了基础,但完整的解决方案需要您自己实现某些元素。
For example the default framework implementation is to create a .resx file for every resource. In ASP.Net this means each user/server control or page. This doesn't lend itself to easy maintenance and if you want to move resources to a database you need to implement your own provider.
例如,默认框架实现是为每个资源创建一个 .resx 文件。在 ASP.Net 中,这意味着每个用户/服务器控件或页面。这并不便于维护,如果您想将资源移动到数据库,您需要实现自己的提供程序。
My familiarity with Winforms is limited but if you are using Silverlight or WPF then have a read of Guy Smith-Ferrier's work on the subject at: http://www.guysmithferrier.com/category/Internationalization.aspx. He also has some toolsets that can make your life easier at: http://www.dotneti18n.com/Downloads.aspx.
我对 Winforms 的熟悉程度有限,但如果您使用的是 Silverlight 或 WPF,那么请阅读 Guy Smith-Ferrier 在该主题上的工作:http: //www.guysmithferrier.com/category/Internationalization.aspx。他还有一些工具集可以让您的生活更轻松:http: //www.dotneti18n.com/Downloads.aspx。
I've worked with him before and have never come across anyone else with a better depth of understanding of the subject.
我以前和他一起工作过,从来没有遇到过对这个主题有更深入理解的人。
回答by Francis B.
I dont have a solution for your first and second requirement but keep in mind that localizing a form is not as simple as translating each word. You need to check that each translated text fits in their respective control. Also, maybe you have an icon or an image which need to be change in another culture.
我没有针对您的第一个和第二个要求的解决方案,但请记住,本地化表单并不像翻译每个单词那么简单。您需要检查每个翻译的文本是否适合它们各自的控件。此外,也许你有一个图标或图像需要在另一种文化中改变。
For your point three, you can change the language manually with the following lines:
对于第三点,您可以使用以下几行手动更改语言:
CultureInfo ci = new CultureInfo("fr");
Thread.CurrentThread.CurrentUICulture = ci;
回答by Charlie A
I've just completed a C# .Net 3.5 project with a similar problem. We were writing WinForms plugin for an existing multi-lingual application with 8 languages (including English).
我刚刚完成了一个有类似问题的 C# .Net 3.5 项目。我们正在为现有的 8 种语言(包括英语)的多语言应用程序编写 WinForms 插件。
This is how we did it:
我们是这样做的:
Create all our forms and UI in the default language, English.
Put all our internal strings in a resource file (stuff not tied directly to a form like custom error messages and dialog box titles etc)
以默认语言英语创建我们所有的表单和 UI。
将我们所有的内部字符串放在一个资源文件中(不直接绑定到自定义错误消息和对话框标题等表单的内容)
Once we had completed most of the work and testing we localised it.
一旦我们完成了大部分工作和测试,我们就对其进行了本地化。
Each form already had a .resx file but this was empty. We set the property 'Localizable' to true, the .resx file was filled with things like button sizes & strings.
For each of the other languages, we changed the 'Language' property on the form. We chose the basic version of each language eg: 'Spanish' instead of 'Spanish (Chile)' etc. so that it would work for every 'Spanish' dialect, I think.
Then we went through each control, translated its text and resized, if needed. This created a .resx per language and form combination.
每个表单已经有一个 .resx 文件,但这是空的。我们将属性 'Localizable' 设置为 true,.resx 文件中充满了按钮大小和字符串等内容。
对于其他每种语言,我们更改了表单上的“语言”属性。我们选择了每种语言的基本版本,例如:“西班牙语”而不是“西班牙语(智利)”等,这样我认为它适用于每种“西班牙语”方言。
然后我们检查每个控件,翻译其文本并根据需要调整大小。这为每种语言和表单组合创建了一个 .resx。
We were then left with, for 8 languages, 8 .resx for each form and 8 .resx for the general strings. When compiled the output folder had the .dll we were creating and then a sub folder for each language with a .resources.dll in it.
然后,对于 8 种语言,每种形式都有 8 个 .resx,通用字符串有 8 个 .resx。编译时,输出文件夹包含我们正在创建的 .dll,然后是每种语言的子文件夹,其中包含 .resources.dll。
We were able to test the versions of the UI in the designer by just changing the language property to check that we had the correct strings & layout.
我们只需更改语言属性即可在设计器中测试 UI 的版本,以检查我们是否具有正确的字符串和布局。
All in all once we got our heads around it, it was quite easy and painless.
总而言之,一旦我们了解了它,它就变得非常容易和无痛。
We didn't need to write any custom tweaks to the form loading
我们不需要为表单加载编写任何自定义调整
回答by Jesse Chisholm
What you are asking for:
您的要求是:
- no satellite resource files
- only one size and control placement per form.
- lots of languages embedded in the executable.
- 没有卫星资源文件
- 每个表单只有一种尺寸和控件位置。
- 可执行文件中嵌入了许多语言。
Is not do-able in vanilla Visual Studio's IDE.
在 vanilla Visual Studio 的 IDE 中是不可行的。
What it would require is some custom work, basically fulfilling all these steps:
它需要的是一些定制工作,基本上完成所有这些步骤:
- Acquire a custom resource manager that handles TMX resource files.
- Put all your localizable strings in a TMX file.
- Make this TMX file an embeddedresource in your project.
- In your Form constructor, create your TMX ResourceManager, loading the TMX file from your embedded resources.
- In your code, use your tmx ResourceManager instead of the default ResourceManager for getting localized strings.
- Let the Form use the default ResourceManager for getting all the designer things except the strings.
- Get your TMX file fleshed out with the new language translations.
- More can be added in the next release of your project, just by adding them to this TMX file before you compile.
- 获取处理 TMX 资源文件的自定义资源管理器。
- 将所有可本地化的字符串放在一个 TMX 文件中。
- 将此 TMX 文件作为项目中的嵌入资源。
- 在您的 Form 构造函数中,创建您的 TMX ResourceManager,从您嵌入的资源中加载 TMX 文件。
- 在您的代码中,使用 tmx ResourceManager 而不是默认的 ResourceManager 来获取本地化字符串。
- 让 Form 使用默认的 ResourceManager 来获取除字符串之外的所有设计器内容。
- 使用新的语言翻译充实您的 TMX 文件。
- 可以在项目的下一个版本中添加更多内容,只需在编译之前将它们添加到此 TMX 文件即可。
RESOURCES: (not an exhaustive list, by any means)
资源:(无论如何都不是详尽的清单)