Svish
Svish

Reputation: 158321

C#: How to bind the text of a winforms button to a resource

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.

Upvotes: 6

Views: 10817

Answers (6)

Vital
Vital

Reputation: 91

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.

Upvotes: 4

Mohammad Shahnawaz
Mohammad Shahnawaz

Reputation: 11

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

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

Upvotes: 1

Will Marcouiller
Will Marcouiller

Reputation: 24142

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.

Upvotes: 1

MusiGenesis
MusiGenesis

Reputation: 75396

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.

Upvotes: 0

zweihander
zweihander

Reputation: 6315

Try this:

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

Upvotes: 0

Bhaskar
Bhaskar

Reputation: 10691

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");

Upvotes: 3

Related Questions