Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

c# - Proper localization of a WinForms application

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.

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

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

  • Do I have to roll my own?
  • Or is there something in the framework already?

.net Framework 3.5 SP1 if that matters.

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.

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

At the moment, I am indeed rolling my own WinForms localization Framework...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

This is how we did it:

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

  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.

  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.

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.

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...