Click here to Skip to main content
15,900,907 members
Articles / Programming Languages / C#
Tip/Trick

How to make a multi-language application in C#

Rate me:
Please Sign up or sign in to vote.
3.20/5 (53 votes)
18 Apr 2013CPOL2 min read 280.4K   15.7K   34   27
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.

Image 1

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:

Image 2

Go to Code

Add namespaces 

C#
using System.Globalization;
using System.Resources;

Declare resource manager and culture info to access Resources

C#
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:

C#
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:

C#
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):

C#
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.

Image 3

Add a menu item (name = French) in language menu. And create click event, add code: 

C#
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:

C#
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems / Hardware Administrator
Vietnam Vietnam
I like making my own software...

Email: hung.rocket42@gmail.com

Comments and Discussions

 
GeneralRe: Give you other Exmaple Pin
mpcp17-Dec-13 20:45
mpcp17-Dec-13 20:45 
GeneralRe: Give you other Exmaple Pin
Nguyen.H.H.Dang8-Jul-14 20:12
professionalNguyen.H.H.Dang8-Jul-14 20:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.