views:

572

answers:

4

We have a resource file with lots of translated strings used various places in our application. Is there a better way for binding for example the text of a button to a certain string?

Currently we are usually just doing something like this in the constructor, in a Load event handler or in a method called by one of those:

someButton.Text = CommonTexts.SomeString;
someMenuItem.Text = CommonTexts.SomeOtherString;

Is there a better way to do it? Maybe in the designer? Or is this the recommended way of doing it?


Just to let you know how we do the actual translation: We have one Class Library project which only contains resx files. The main resx files are written in english (of course). We then open up those base resx files in an application called ResEx where we (or someone else) does the translation to other languages. When compiled Visual Studio automatically creates assemblies for each language which are used automatically depending on the current culture set. This works pretty well, so I don't really need info on how to do the translation and such (although I am always curious to improvements of course). What I am asking is if there is a better way for getting those translated strings from the resource assembly and into all the various Text properties.

+1  A: 

You can do:

using System.Resources;
using System.Reflection;

Assembly assembly = this.GetType().Assembly;
resman = new ResourceManager("StringResources.Strings", assembly);
btnButton.Text = resman.GetString("ButtonName");
Bhaskar
Where is the binding ?
Thomas Levesque
That would kind of be the same way I already do it, except a lot more complex :p
Svish
@Thomas: I don't think the poster meant binding as in "data binding".
MusiGenesis
In VS2008 (and probably 2005) the default resource manager you get from adding a resources file to your project exposes properties named for the key value, so that would just be "btnButton.Text = resman.ButtonName", saving worries about typos in the string.
Steve Gilham
A: 

Your way is the best way to do this if you have developers who are not personally fluent in the languages you're translating your application into. I've done this before with an English application that had to be translated into Spanish, Portuguese and Chinese (I only speak one of these semi-fluently). The original forms were all created in English, and in the form's Load event the code iterated through every control and searched for each control's Text property in a translations spreadsheet, and replaced it with the appropriate translation.

The built-in way of internationalizing a form (which is basically a variant of form inheritance) assumes that the programmer is fluent in the language you need to translate to, which is pretty much of a stretch, and it requires you to manually enter all the translated text values for each form and each language. It also makes your binary larger (potentially much larger), since it adds a .resx file for each form for each language that you support.

MusiGenesis
A: 

Try this:

someButton.DataBindings.Add("Text", CommonTexts, "SomeString");

erelender
+1  A: 

There is a good tool called LingoBit Localizer that does the job for the fraction of the time it would take to build all the reasources files.

You don't have to care about other languages while in development process, you simply code and set properties as you would if you were programming for a unilingual software. After you're done, or whenever you wish, you run LingoBit Localizer over your DLL or Windows Form application. This will get user-displayable strings out to a grid for you within its GUI. Now, perhaps a professional translator could use to translate the words if your programmers don't know the language for which the applicaiton have to be translated. Then, you simply save the project when you're done. This will create a DLL file which you simply add to your binary deployment directory, then your application will automatically set itself to the right language depending on the current culture information on which the app. is installed or so. This saves a lot of programming time and headaches.

Hope this helps even though it is not resource-based solution.

Will Marcouiller
That's a reasonable approach to this problem. If only they supported Portuguese ...
MusiGenesis
You can also use VisualLocalize. We use that tool very often. Fine thing here is that you can later on check all your dialogs if the translated text fits into the controls and if not you can resize the control only in this language and the new sizes will be stored aside the localized text in a sattelite-assembly.
PVitt