WinForms应用程序的适当本地化

时间:2023-01-17 04:14:19

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 and ints, but also a CultureInfo
  • 这应该主要包含字符串和int,但也应该包含文化信息

Most solutions I've seen either have one .resx file per form and/or external satellite assemblies.

我所见过的大多数解决方案都是每个窗体和/或外部附属程序集都有一个.resx文件。

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. Theoretically, I only want to a) override the creation of the ComponentResourceManager to point it to my global resx and b) change the call to ApplyResources to the overload that takes a CultureInfo as third parameter.

编辑:在大多数情况下,Visual Studio已经为我想要的东西提供了支持,但是有两个问题。当我设置形式。Localizable to true,我有这个很好的设计器支持,但是每个窗体都会产生一个resx。在InitializeComponent中手动重写它的想法失败了,因为它是设计人员编写的代码,通常会被重写。理论上,我只希望a)重写ComponentResourceManager的创建,将其指向我的global resx和b))更改调用ApplyResources到接受CultureInfo作为第三个参数的重载。

It seems as if I have to add a function call to my constructor thatgets called after InitializeComponent() and overrides it's behaviour. That seems terribly inefficient, but Visual Studio is right when it warns about touching InitializeComponent. At the moment, I am indeed rolling my own WinForms localization Framework...

似乎我必须向构造函数添加一个函数调用,该函数在InitializeComponent()之后被调用,并重写它的行为。这似乎是非常低效的,但是当Visual Studio警告说要使用InitializeComponent时,它是正确的。目前,我确实在使用我自己的WinForms本地化框架……

5 个解决方案

#1


15  

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项目,遇到了类似的问题。我们正在为一个现有的多语言应用程序编写WinForms插件,该应用程序包含8种语言(包括英语)。

This is how we did it:

我们是这样做的:

  1. Create all our forms and UI in the default language, English.

    用默认语言英语创建所有的表单和UI。

  2. 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)

    将所有内部字符串放入资源文件中(不直接绑定到表单的内容,如自定义错误消息和对话框标题等)

Once we had completed most of the work and testing we localised it.

一旦我们完成了大部分的工作和测试,我们本地化了它。

  1. 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.

    每个表单都已经有一个.resx文件,但这是空的。我们将属性“Localizable”设为true, .resx文件中填充了按钮大小和字符串等内容。

  2. 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.

    对于其他每种语言,我们都更改了表单上的“语言”属性。我们选择了每一种语言的基本版本,如:“西班牙语”而不是“西班牙语(智利语)”等,我想它适用于每一种“西班牙语”方言。

  3. Then we went through each control, translated its text and resized, if needed. This created a .resx per language and form combination.

    然后我们遍历每个控件,翻译它的文本并在需要时调整大小。这为每种语言和表单组合创建了.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

我们不需要为表单加载编写任何自定义修改

#2


4  

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本地化。

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

马克

#3


3  

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。这意味着每个用户/服务器控件或页面。这并不容易维护,如果您想将资源转移到数据库,则需要实现自己的提供程序。

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/internationaliz.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.

我以前和他一起工作过,从来没有遇到过比他更深入地了解这个问题的人。

#4


2  

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; 

#5


1  

What you are asking for:

你所要求的:

  1. no satellite resource files
  2. 没有卫星资源文件
  3. only one size and control placement per form.
  4. 每个表单只有一个大小和控制位置。
  5. lots of languages embedded in the executable.
  6. 在可执行文件中嵌入了许多语言。

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.
  • 获取一个处理TMX资源文件的自定义资源管理器。
  • Put all your localizable strings in a TMX file.
  • 将所有可本地化的字符串放在TMX文件中。
    • Make this TMX file an embedded resource in your project.
    • 使这个TMX文件成为项目中的嵌入式资源。
  • 使这个TMX文件成为项目中的嵌入式资源。
  • In your Form constructor, create your TMX ResourceManager, loading the TMX file from your embedded resources.
  • 在表单构造函数中,创建TMX ResourceManager,从嵌入式资源中加载TMX文件。
  • In your code, use your tmx ResourceManager instead of the default ResourceManager for getting localized strings.
  • 在代码中,使用tmx ResourceManager而不是默认的ResourceManager来获取本地化的字符串。
    • Let the Form use the default ResourceManager for getting all the designer things except the strings.
    • 让表单使用默认的ResourceManager来获取除字符串之外的所有设计器内容。
  • 让表单使用默认的ResourceManager来获取除字符串之外的所有设计器内容。
  • Get your TMX file fleshed out with the new language translations.
  • 让您的TMX文件用新的语言翻译充实起来。
    • More can be added in the next release of your project, just by adding them to this TMX file before you compile.
    • 可以在项目的下一个版本中添加更多内容,只需在编译之前将它们添加到这个TMX文件。
  • 可以在项目的下一个版本中添加更多内容,只需在编译之前将它们添加到这个TMX文件。

RESOURCES: (not an exhaustive list, by any means)

参考资料:(无论如何不是详尽的列表)

#1


15  

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项目,遇到了类似的问题。我们正在为一个现有的多语言应用程序编写WinForms插件,该应用程序包含8种语言(包括英语)。

This is how we did it:

我们是这样做的:

  1. Create all our forms and UI in the default language, English.

    用默认语言英语创建所有的表单和UI。

  2. 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)

    将所有内部字符串放入资源文件中(不直接绑定到表单的内容,如自定义错误消息和对话框标题等)

Once we had completed most of the work and testing we localised it.

一旦我们完成了大部分的工作和测试,我们本地化了它。

  1. 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.

    每个表单都已经有一个.resx文件,但这是空的。我们将属性“Localizable”设为true, .resx文件中填充了按钮大小和字符串等内容。

  2. 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.

    对于其他每种语言,我们都更改了表单上的“语言”属性。我们选择了每一种语言的基本版本,如:“西班牙语”而不是“西班牙语(智利语)”等,我想它适用于每一种“西班牙语”方言。

  3. Then we went through each control, translated its text and resized, if needed. This created a .resx per language and form combination.

    然后我们遍历每个控件,翻译它的文本并在需要时调整大小。这为每种语言和表单组合创建了.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

我们不需要为表单加载编写任何自定义修改

#2


4  

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本地化。

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

马克

#3


3  

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。这意味着每个用户/服务器控件或页面。这并不容易维护,如果您想将资源转移到数据库,则需要实现自己的提供程序。

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/internationaliz.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.

我以前和他一起工作过,从来没有遇到过比他更深入地了解这个问题的人。

#4


2  

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; 

#5


1  

What you are asking for:

你所要求的:

  1. no satellite resource files
  2. 没有卫星资源文件
  3. only one size and control placement per form.
  4. 每个表单只有一个大小和控制位置。
  5. lots of languages embedded in the executable.
  6. 在可执行文件中嵌入了许多语言。

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.
  • 获取一个处理TMX资源文件的自定义资源管理器。
  • Put all your localizable strings in a TMX file.
  • 将所有可本地化的字符串放在TMX文件中。
    • Make this TMX file an embedded resource in your project.
    • 使这个TMX文件成为项目中的嵌入式资源。
  • 使这个TMX文件成为项目中的嵌入式资源。
  • In your Form constructor, create your TMX ResourceManager, loading the TMX file from your embedded resources.
  • 在表单构造函数中,创建TMX ResourceManager,从嵌入式资源中加载TMX文件。
  • In your code, use your tmx ResourceManager instead of the default ResourceManager for getting localized strings.
  • 在代码中,使用tmx ResourceManager而不是默认的ResourceManager来获取本地化的字符串。
    • Let the Form use the default ResourceManager for getting all the designer things except the strings.
    • 让表单使用默认的ResourceManager来获取除字符串之外的所有设计器内容。
  • 让表单使用默认的ResourceManager来获取除字符串之外的所有设计器内容。
  • Get your TMX file fleshed out with the new language translations.
  • 让您的TMX文件用新的语言翻译充实起来。
    • More can be added in the next release of your project, just by adding them to this TMX file before you compile.
    • 可以在项目的下一个版本中添加更多内容,只需在编译之前将它们添加到这个TMX文件。
  • 可以在项目的下一个版本中添加更多内容,只需在编译之前将它们添加到这个TMX文件。

RESOURCES: (not an exhaustive list, by any means)

参考资料:(无论如何不是详尽的列表)