Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Supporting multiple languages in a Winforms application

I've almost finished my C# application; the only thing left to do is to implement multiple language support.

I've already created resource files which hold strings for a few languages for all the text displayed on screen.

Example of my English Resource file:

Name                    |  Value                       | Comment
------------------------------------------------------------------------------
lblName                 |   Name:                      |  Name EN

Example of my Dutch Resource file:

Name                    |  Value                       | Comment
------------------------------------------------------------------------------
lblName                 |  Naam:                       | Name NL

How can I bind the field Value to the Text property of my Label (lblName)?

I'm using .NET Framework 3.5.

like image 818
Max Avatar asked Nov 27 '22 19:11

Max


1 Answers

I know this question have been ask a long time ago but since there was no answer here's what I suggest:

To access a resource file in C# you can use a ResourceManager. First create your resource manager depending on the curent language. You have two options for this. You can use a switch or if statement (if the language is determined by a menu) or use localization to use the current culture of the computer. Finally, with both ways you can then call the GetString() method providing the key which I believe in your case is lblName.

N.B: In the example bellow I use the first method which is retrieving the language from a menu.

string selectedLanguage = comboBoxLang.Text; // Comes from a menu option
string resourceFile = string.Empty;

/***/
Logic to retrieve the proper resourceFile depending on the selectedLanguage.
/***/

ResourceManager rm = new ResourceManager(resourceFile, Assembly.GetExecutingAssembly());

// Set your label text.
lblName.Text = rm.GetString("lblName");
like image 182
SniperLegacy Avatar answered Jan 21 '23 03:01

SniperLegacy