Click here to Skip to main content
Click here to Skip to main content

How to make a multi-language application in C#

, 18 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

rocket42
Systems / Hardware Administrator
Vietnam Vietnam
I like making my own software...
 
Email: hung.rocket42@gmail.com

Comments and Discussions

 
QuestionCode could have been simpler PinmemberMikel Finch11-Mar-14 5:19 
GeneralMy vote of 5 PinmemberPriya Ranjan Dubey25-Dec-13 3:11 
GeneralMy vote of 1 PinmemberMember 1035967719-Nov-13 17:22 
SuggestionIt is highly improper and stupid to involve politics in your CodeProject Pinmemberglen_waverley27-Oct-13 15:54 
GeneralRe: It is highly improper and stupid to involve politics in your CodeProject Pinmemberrocket424-Nov-13 21:18 
QuestionSabi PinmemberMember 872103810-Oct-13 4:35 
SuggestionGive you other Exmaple PinmemberLannyFly4-Aug-13 17:26 
GeneralRe: Give you other Exmaple Pinmembernguyen Ngoc Tuan19-Aug-13 18:16 
GeneralRe: Give you other Exmaple Pinmembermpcp17-Dec-13 21:45 
GeneralRe: Give you other Exmaple PinprofessionalNguyen.H.H.Dang8-Jul-14 21:12 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 18 Apr 2013
Article Copyright 2013 by rocket42
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid