Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to make a multi-language application in C#

0.00/5 (No votes)
18 Apr 2013 2  
How to make a multi-language application in C#.

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;    // declare Resource manager to access to specific cultureinfo
CultureInfo cul;            // declare culture info

Create new function to switch language

Create cultureinfo for each language:

void switch_language(void)
{
 if (vietnameseToolStripMenuItem.Checked == true)    //in vietnamese
 { 
   cul = CultureInfo.CreateSpecificCulture("vi");    //create culture for vietnamese
 }          
 else                                                //in english
 {
  cul = CultureInfo.CreateSpecificCulture("en");     //create culture for english
 }

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;    //default language is english
englishToolStripMenuItem.Checked = true;
res_man = new ResourceManager("MultiLanguageApp.Resource.Res", typeof(MainForm).Assembly);
        //switch to vietnamese
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)      //in french, switch to default language
{
    frenchToolStripMenuItem.Checked = false;
    vietnameseToolStripMenuItem.Checked = false;
    englishToolStripMenuItem.Checked = true;     //default language
}
else            //current language is not french, switch french
{
    frenchToolStripMenuItem.Checked = true;
    vietnameseToolStripMenuItem.Checked = false;
    englishToolStripMenuItem.Checked = false;
}
//switch language
switch_language();

Replace this code in switch_language() function:

if (vietnameseToolStripMenuItem.Checked == true)         //in vietnamese
{ 
     cul = CultureInfo.CreateSpecificCulture("vi");       //create culture for vietnamese
}      
else  //in english
{ cul = CultureInfo.CreateSpecificCulture("en");    //create culture for english}

by this code:

if (vietnameseToolStripMenuItem.Checked == true)       //in vietnamese
{
 cul = CultureInfo.CreateSpecificCulture("vi")//create culture for vietnamese
 else if (englishToolStripMenuItem.Checked == true)    //in english
 {
   cul = CultureInfo.CreateSpecificCulture("en");        //create culture for english
 }
 else
 {
   cul = CultureInfo.CreateSpecificCulture("fr");         //create culture for french
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here