6

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.

2
  • Re, the Edit: The counter question is: why don't you use the default FormLoad system? It's built in and invisible. Aug 25, 2009 at 18:38
  • 1
    Because I was wondering if there was a better way.
    – Svish
    Aug 26, 2009 at 10:06

6 Answers 6

4

I understand, it's an old question, but still, I had the same issue (VS 2010), and one of the first links in google is this topic.

In order to move all the texts to forms resource file - you need to set the winform Localizable property to True. And that's it. :) (c) Cap. O.

3

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");
3
  • 3
    That would kind of be the same way I already do it, except a lot more complex :p
    – Svish
    Aug 24, 2009 at 13:52
  • @Thomas: I don't think the poster meant binding as in "data binding". Aug 24, 2009 at 13:52
  • 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. Aug 24, 2009 at 18:37
1

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.

2
  • That's a reasonable approach to this problem. If only they supported Portuguese ... Aug 24, 2009 at 20:17
  • 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
    Aug 25, 2009 at 20:12
1

This will extract the the value of Home keyword and populate into the Text1 Box.

Text1.Text= Resource.Home.ToString();
0

Try this:

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.