开发者

Multiple Form Globalization

开发者 https://www.devze.com 2023-02-08 21:50 出处:网络
We have a large solution with 100+ forms which all have labels and buttons etc... in English. All the forms have common text on them, say for example there is the word \"Job\" on at least 20 forms.

We have a large solution with 100+ forms which all have labels and buttons etc... in English.

All the forms have common text on them, say for example there is the word "Job" on at least 20 forms.

We are looking to move towards a different market and require that the forms are all in different languages. Having looking into .NET globalization techniques we have found that the standard approach does not suit us that well; That approach being a coded solution. We want to avoid replicating

button.Text = ResourceManager.getString("job");`

in 20+ forms and were looking to see if there is a simpler solution. (We set the culture on application startup)

One that we had an idea of was a resx (resource file) that we can bind to the forms and then select a value in that resource file to bind to a label or button text. As far as we can tell we can't find a simple solution like this.

We know that we can create .resx files behind each form, but this is too much overhead if we want to include more languages in the future - and also there is unnecessary duplic开发者_Go百科ation when the word "Job" is on more than one form.

Does a solution exist where we can bind a resource file (or Satellite Assembly) to each form/component (without code) and then just select what value we want displayed? Or are there alternative solutions which are similar?


There's a way to do this very simply. Start by creating a list of the Text properties of all the controls on all your forms that you wish to have translated (so you'd have "OK", "Cancel", "Job", "Delete" etc.). Then get someone to translate each term in whatever language(s) you wish to support (and add the translations in a separate column for each supported language). Then embed/include this list (as a CSV, database, XML file or whatever you like) with your application.

In each form's Load event, call a method that iterates through each control on the form (recursively, so you also pick up any controls that are sitting on container controls), looks up the control's Text property in your translation list, and changes the text to the translated value (based on the current language, which you determine and/or set elsewhere).

I always used an Excel spreadsheet for this, as it was easy to embed and also easy to send off to have additional terms and/or additional languages added.

Using the built-in globalization feature in .Net is an exercize in pain, IMHO, and generally requires that a programmer be proficient in the language to be supported.


In the end, because of the size of the project, we had to use a coded solution.

We added in a new solution and this just contained resource files that contained all the languages we used as well as a default set (English).

In the constructor of each page we changed all the component text values by using

job_label.Text = LanguageResources.job; //example to display "job" on a label

Because Visual Studio creates strong-typed classes for the resource files we can directly call the resource file LanguageResources and access any field in it. Then we created LanguageResources.de-DE.resx and populated that with all the same keys as the English version but with German values.

Thanks to .NET's Globalization techniques, we can set the language in the .config file so that when the application runs it can check to see what language it needs to display and accesses the appropriate resource file. If the language is unrecognized it will default to English.

0

精彩评论

暂无评论...
验证码 换一张
取 消