Introduction
We know that a software prerequisite is that if it is being used all over the world, it must have a multi-language interface because not everyone speaks English. The problem is, how does one create a multi-language software which can edit and add other languages easily?
This article presents a solution to solving this problem using C# and Visual Studio 2008.
First we will present how to create an application in Vietnamese and English. Then we will present the procedure to add other languages (for instance, French).
Using the Code
Creating an Application in Vietnamese and English
Creating a new project as the following:
Drag & drop the menuStrip toolbox. Add a File ToolStripMenuItem with an Exit sub-menu. Then add a Language ToolStripMenuItem with Vietnamese and English sub-menus
Drag & drop the textbox toolbox with properties: multi lines = true, dock = fill.
Add new folder “Resource" to project ( project>New Folder) to contain Language Files.
Add two new Resource File to Resource folder (just created above): right-click on Resource folder icon> Add>New Items> Resources File with File1 name is "vi.resx", File2 name is "en.resx". Then Create Items in this Resource files as following:
Go to Code
Add namespaces
using System.Globalization;
using System.Resources;
Declare resource manager and culture info to access Resources
ResourceManager res_man;
CultureInfo cul;
Create new function to switch language
Create cultureinfo for each language:
void switch_language(void)
{
if (vietnameseToolStripMenuItem.Checked == true)
{
cul = CultureInfo.CreateSpecificCulture("vi");
}
else
{
cul = CultureInfo.CreateSpecificCulture("en");
}
Extract the "MainForm_text
" value in Res.vi.resx or Res.en.resx, respectively:
this.Text = res_man.GetString("MainForm_text", cul);
...
With this code line when we choose the English this.text
equal column's value named "MainForm_text
" is multi-language. Same to when we choose Vietnamese.
Default language: create a main form Load
event to set default language (here is Vietnamese):
vietnameseToolStripMenuItem.Checked = false;
englishToolStripMenuItem.Checked = true;
res_man = new ResourceManager("MultiLanguageApp.Resource.Res", typeof(MainForm).Assembly);
switch_language();
Now, IF you want to edit TEXT, only must edit in Res.vi.resx or Res.en.resx, respectively. Next, add another language (for instance, French):
Create a new Resource File: Right-click on Resource icon>Add>New Item…then Enter name is Res.fr.resx
Copy all of content in Res.en.resx into Res.fr.resx, then translate "value column" into French.
Add a menu item (name = French) in language menu. And create click event, add code:
if (frenchToolStripMenuItem.Checked == true)
{
frenchToolStripMenuItem.Checked = false;
vietnameseToolStripMenuItem.Checked = false;
englishToolStripMenuItem.Checked = true;
}
else
{
frenchToolStripMenuItem.Checked = true;
vietnameseToolStripMenuItem.Checked = false;
englishToolStripMenuItem.Checked = false;
}
switch_language();
Replace this code in switch_language()
function:
if (vietnameseToolStripMenuItem.Checked == true)
{
cul = CultureInfo.CreateSpecificCulture("vi");
}
else
{ cul = CultureInfo.CreateSpecificCulture("en");
by this code:
if (vietnameseToolStripMenuItem.Checked == true)
{
cul = CultureInfo.CreateSpecificCulture("vi")
else if (englishToolStripMenuItem.Checked == true)
{
cul = CultureInfo.CreateSpecificCulture("en");
}
else
{
cul = CultureInfo.CreateSpecificCulture("fr");
}