Multilanguage Form

The below figure is a Japanese form,


the French form


and the English form


- How will i do?
My solution is resources.
1. I start Visual Studio, create a new project, name it as "MultiLanguage", in Solution Explorer, right click on project->Add->New Folder, named "Res".

2. Right click on "Res" folder-> Add -> New Item, i select "Resource file" in Templates list, create new "FormLanguage" resource. In resource editor, type "strHello" in name column, and type "Hello" in value column, it means my resource has "strHello" string that assigned "Hello" value. Repeat this step for other languages, change "Hello" string in value column depends on other languages.

3. In my form, i add a combobox that contains language items, a label that displays "Hello" string. All form code lines are below:

using System;

using System.Windows.Forms;

using System.Threading; //Thread

using System.Globalization; //CultureInfo

using System.Resources; //ResourceManager

using System.Collections; //Hashtable

namespace MultiLanguage

{

public partial class Form1 : Form

{

//Define a language array

string[] language = new string[] { "English", "French", "Germany", "Japanese", "Vietnamese" };

//Define a culture array

string[] culture = new string[] { "en-US", "fr-FR", "de-DE", "ja-JP", "vi-VN" };

//Hashtable that stores language culture (key = language, value = culture)

Hashtable ht = null;

public Form1()

{

InitializeComponent();

//Initial language hashtable

ht = new Hashtable();

for (int i = 0; i <>

ht.Add(language[i], culture[i]);

//Change to default language - "English"

ChangeLang("English");

}

void ChangeLang(string country)

{

string name = ht[country].ToString();

ChangeCulture(name);

ChangeUI();

}

void ChangeCulture(string name)

{

Thread.CurrentThread.CurrentUICulture = new CultureInfo(name);

}

void ChangeUI()

{

/*

* i use ResourceManager(string baseName, System.Reflection.Assembly assembly)

* baseName:

* namespace.Folder.Resource

* Multilanguage.Res.FormLanguage

*/

ResourceManager rm = new ResourceManager("MultiLanguage.Res.FormLanguage", typeof(Form1).Assembly);

//Get "Hello" string from resource using GetString method

//Assign to form title

this.Text = rm.GetString("strHello");

//Assigne to label

lblHello.Text = rm.GetString("strHello");

}

private void cboLang_SelectedIndexChanged(object sender, EventArgs e)

{

//Get string of selected item

string name = cboLang.SelectedItem.ToString();

ChangeLang(name);

}

}

}



More infomation about culture name, please visit in MSDN (thanks to MSDN):
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref8/html/T_System_Globalization_CultureInfo.htm

Happy coding! :)

Any comment from you will be highly appreciated.

Comments