Click here to Skip to main content
15,881,898 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 279.2K   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

 
GeneralMy vote of 5 Pin
Member 1465884222-Dec-23 7:15
Member 1465884222-Dec-23 7:15 
GeneralMy vote of 5 Pin
Member 1465884222-Dec-23 7:14
Member 1465884222-Dec-23 7:14 
GeneralMy vote of 1 Pin
FriedCesar22-Nov-21 19:56
FriedCesar22-Nov-21 19:56 
GeneralMessage Closed Pin
24-Apr-21 9:32
udav-haui24-Apr-21 9:32 
Questionafter migration to C# 2019 Pin
HPvanLeeuwen23-Jul-20 0:23
HPvanLeeuwen23-Jul-20 0:23 
Questionconfuse Pin
Member 135033224-Nov-17 8:33
Member 135033224-Nov-17 8:33 
GeneralMy vote of 1 Pin
User 1106097925-Aug-17 11:23
User 1106097925-Aug-17 11:23 
BugForgot french toolstrip Pin
Member 1034183027-Apr-17 3:45
Member 1034183027-Apr-17 3:45 
Answerthanks Pin
Shrikrishna Chopade12-Nov-16 0:45
Shrikrishna Chopade12-Nov-16 0:45 
QuestionDoubts Res_Man Pin
Member 118588593-Sep-15 9:08
Member 118588593-Sep-15 9:08 
AnswerRe: Doubts Res_Man Pin
Member 1096153519-Jun-16 11:20
Member 1096153519-Jun-16 11:20 
AnswerRe: Doubts Res_Man Pin
MICELA SHIVVA17-Oct-19 20:37
MICELA SHIVVA17-Oct-19 20:37 
GeneralMy vote of 3 Pin
Ganesh KP24-Jan-15 21:54
professionalGanesh KP24-Jan-15 21:54 
Good for providing a good article with clean contents.

Will be much good, if you can provide an article on how to do with database also.
QuestionCode could have been simpler Pin
Mikel Finch11-Mar-14 4:19
Mikel Finch11-Mar-14 4:19 
GeneralMy vote of 5 Pin
Priya Ranjan Dubey25-Dec-13 2:11
Priya Ranjan Dubey25-Dec-13 2:11 
GeneralMy vote of 1 Pin
FrankXhaw19-Nov-13 16:22
professionalFrankXhaw19-Nov-13 16:22 
GeneralRe: My vote of 1 Pin
User 1106097924-Aug-17 6:30
User 1106097924-Aug-17 6:30 
SuggestionIt is highly improper and stupid to involve politics in your CodeProject Pin
Silan (Frank) Liu27-Oct-13 14:54
Silan (Frank) Liu27-Oct-13 14:54 
GeneralRe: It is highly improper and stupid to involve politics in your CodeProject Pin
rocket424-Nov-13 20:18
rocket424-Nov-13 20:18 
GeneralRe: It is highly improper and stupid to involve politics in your CodeProject Pin
TambourineMan8-Apr-15 5:59
TambourineMan8-Apr-15 5:59 
GeneralRe: It is highly improper and stupid to involve politics in your CodeProject Pin
davtx3013-Apr-15 5:35
davtx3013-Apr-15 5:35 
GeneralRe: It is highly improper and stupid to involve politics in your CodeProject Pin
HongOKC3-Mar-16 4:43
HongOKC3-Mar-16 4:43 
QuestionSabi Pin
Member 872103810-Oct-13 3:35
Member 872103810-Oct-13 3:35 
SuggestionGive you other Exmaple Pin
LannyFly4-Aug-13 16:26
LannyFly4-Aug-13 16:26 
GeneralRe: Give you other Exmaple Pin
nguyen Ngoc Tuan19-Aug-13 17:16
nguyen Ngoc Tuan19-Aug-13 17:16 

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.