Click here to Skip to main content
15,867,141 members
Articles / Programming Languages / C++
Article

Multilingual support for applications

Rate me:
Please Sign up or sign in to vote.
4.33/5 (29 votes)
6 Jun 20055 min read 210.5K   4.3K   114   55
An easy solution to add multilingual support to your application in 10 minutes.

Why another article about multilingual applications?

Well, about a year ago, I needed my application to support multiple languages. I thought it would be very easy to find out how to do this. There are a lot of articles that explain how to do this, but none of them fit my needs.

What can I find here?

Introduction

What I really needed were simple .h and .cpp files which are able to load a language DLL and extract the strings I need. None of the articles I found provided this. At this point, I decided to write my own classes which could do the task I wanted them to do. I am a little active on some forums, and I see this question keeps coming back of how to implement multiple languages to an application. What I see is that it is really hard to understand how it works, the first time. So I decided to write a little article and add my language files to CodeProject.

How to use?

I tried to implement the files so that they are really easy to use. Let's explain how the class works.

Use string tables

Some people are still hard-coding the strings of their application in their code. This isn't the best solution on earth. So use string tables, because this is needed if you want to add multiple language support to your application.

Add files to project

First of all, you have to add the files Language.cpp and Language.h to your project. Now you are almost ready adding multilingual support to your application. The class I developed is a so-called singleton class. This means that you don't have to create the class every time. You only have to load and release the language at startup, and you can use the class everywhere.

How to load a language

Now, you are ready to load your first language. Normally, you load language at the startup of your application. Here is some example code that will try to load the system's default language:

BOOL CMyApp::InitInstance()
{
    .. some default code here

    // Init language object 
    CLanguage * pLanguage = CLanguage::Instance(); 

    // If you want, you can init the location of the dll's and the default language
    pLanguage->Init("lng", "dutch");

    // Load system default language 
    if (!pLanguage->LoadLanguage(GetSystemDefaultLangID())) 
    { 
        // Show error
        AfxMessageBox("Unable to load system language, "
                                  "default language is used");
    }

    // Load dialog
    CMyDialog dlg;
    dlg.DoModal();
    
    // Release language
    pLanguage->ReleaseLanguage();

    .. rest of code
}

It was pretty easy to load a language, wasn't it? Keep in mind that the Init function is not really needed. If you don't call this function, the defaults "lng" and "english" are used. But this is not all, you also have to load the strings.

How to load strings

You have loaded a language, but you also want to load strings. For example, you have a dialog with a nice static control. This is how you load the language:

BOOL CMyDialog::OnInitDialog()
{
    // Call original function
    CDialog::OnInitDialog();

    // Init language object
    CLanguage * pLanguage = CLanguage::Instance();

    // Set text for static control
    m_lblMyStatic.SetWindowText(pLanguage->GetString(IDS_MYSTATICTEXT).c_str());

    .. rest of code
}

The language object will see if the user has loaded a valid DLL. If so, the string will be loaded from the DLL, if not, the default string table will be used. Still easy isn't it? That is all you have to do in your application.

How to create the language DLLs

There are a few easy steps to take:

  1. Create project.

    First, you have to create a project for your DLL. Start Visual Studio, and choose File -> New.... Then select Win32 Dynamic-Link Library. Give your project a new name and click OK. You now have seen a wizard, select a DLL that exports some symbols. The default files for the DLL are now created.

  2. Copy string table from original project.

    Now the default files are created, close the current (language DLL) project. Open your real application in the IDE now and open the string table. Use CTRL + A to select all items and then copy them. Close project again.

  3. Add string table to language project.

    Open your language project again. Select File -> New... and select resource script. Give the resource script a name and click OK. Now you can add resources to your project. Right click on the resource file and select Insert..., then select String table. Now copy the string table of your application to the language DLL (just use CTRL + V). As you will see, all values are now in the language DLL. You can start translating all the sentences now. If you are ready, you can compile the project. The DLL is now ready to use!

Languages

There are a lot of languages automatically supported by the language class. Of course, you can edit these settings yourself in Language.cpp (see InitLanguages function). A list of languages supported and the name of the DLL which is expected is given below:

Language DLLIDCodeLanguage description
arabic.dllunknown--
chinese_simplified.dllunknown--
czech.dll0x0405CSYCzech
danish.dll0x0406DANDanish
dutch.dll0x0413
0x0813
NLD
NLB
Dutch (Standard)
Belgian (Flemish)
finnish.dll0x040bFINFinnish
french.dll0x040c
0x080c
0x0c0c
0x100c
FRA
FRB
FRC
FRS
French (standard)
Belgian
Canadian
Swiss
german.dll0x0407
0x0807
0x0c07
DEU
DES
DEA
German (standard)
Swiss
Austrian
greek.dll0x0408ELLGreek
hungarian.dll0x040eHUNHungarian
icelandic.dll0x040fISLIslandic
italian.dll0x0410
0x0810
ITA
ITS
Italian (standard)
Swiss
lithuanion.dllunknown--
norwegian.dll0x0414
0x0814
NOR
NON
Norwegian (bokmal)
Norwegian (nynorsk)
polish.dll0x0415PLKPolish
portugues_br.dll0x0416PTBPortuguese (Brazilian)
russian.dll0x0419RUSRussian
slovak.dll0x041bSKYSlovak
spanish.dll0x040a
0x080a
0x0c0a
ESP
ESM
ESN
Spanish (standard/traditional)
Mexican
Spanish (modern)
swedish.dll0x041DSVESwedish
turkish.dll0x041fTRKTurkish

History

  • June 3rd, 2005
    • Updated source code, constants can be changed at run-time;
    • Updated source code, you don't have to link to libraries anymore;
    • Removed MFC version, Win32 version will work in MFC applications;
  • June 1st, 2005
    • Added example application;
    • Added MFC and non-MFC versions;
    • Fixed bug when including multiple files;
  • May 26th, 2005
    • First release.

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


Written By
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralHere is "Another approach" Pin
Herbert Yu20-Jul-05 10:30
Herbert Yu20-Jul-05 10:30 
GeneralRe: Another another approach Pin
HeartFriend7-Sep-05 20:37
HeartFriend7-Sep-05 20:37 
GeneralThe right way to do it Pin
Anonymous1-Jun-05 20:07
Anonymous1-Jun-05 20:07 
GeneralRe: The right way to do it Pin
Geert van Horrik1-Jun-05 20:35
Geert van Horrik1-Jun-05 20:35 
GeneralRe: The right way to do it Pin
Member 10191321-Jun-05 22:42
Member 10191321-Jun-05 22:42 
GeneralRe: The right way to do it Pin
Mihai Nita2-Jun-05 19:08
Mihai Nita2-Jun-05 19:08 
GeneralRe: The right way to do it Pin
Geert van Horrik3-Jun-05 0:59
Geert van Horrik3-Jun-05 0:59 
GeneralRe: The right way to do it Pin
Blake Miller2-Jun-05 6:53
Blake Miller2-Jun-05 6:53 
GeneralRe: The right way to do it Pin
Nat pear23-Jun-05 0:01
sussNat pear23-Jun-05 0:01 
GeneralHow about this way. Pin
Herbert Yu20-Jul-05 10:35
Herbert Yu20-Jul-05 10:35 
GeneralAnother approach Pin
1-Jun-05 2:36
suss1-Jun-05 2:36 
GeneralRe: Another approach Pin
Blake Miller2-Jun-05 6:32
Blake Miller2-Jun-05 6:32 
GeneralRe: Another approach Pin
Member 10191322-Jun-05 23:56
Member 10191322-Jun-05 23:56 
GeneralMultiple defined symbols! Pin
Bob Stanneveld31-May-05 10:04
Bob Stanneveld31-May-05 10:04 
GeneralRe: Multiple defined symbols! Pin
Geert van Horrik31-May-05 20:31
Geert van Horrik31-May-05 20:31 
QuestionIs there any way to operate with other resourses? Pin
dim1330-May-05 3:01
dim1330-May-05 3:01 
AnswerRe: Is there any way to operate with other resourses? Pin
Geert van Horrik30-May-05 6:18
Geert van Horrik30-May-05 6:18 
GeneralRe: Is there any way to operate with other resourses? Pin
Blake Miller2-Jun-05 6:22
Blake Miller2-Jun-05 6:22 
GeneralRe: Is there any way to operate with other resourses? Pin
Geert van Horrik2-Jun-05 20:13
Geert van Horrik2-Jun-05 20:13 
QuestionGreat works, But can you do it without MFC? Pin
Rocom26-May-05 18:35
Rocom26-May-05 18:35 
AnswerRe: Great works, But can you do it without MFC? Pin
Geert van Horrik27-May-05 18:31
Geert van Horrik27-May-05 18:31 
GeneralRe: Great works, But can you do it without MFC? Pin
Rocom1-Jun-05 15:26
Rocom1-Jun-05 15:26 
QuestionHow about Chinese? Pin
transoft26-May-05 17:22
transoft26-May-05 17:22 
AnswerRe: How about Chinese? Pin
Geert van Horrik27-May-05 18:30
Geert van Horrik27-May-05 18:30 
GeneralRe: How about Chinese? Pin
mn_guy17-Oct-06 4:17
mn_guy17-Oct-06 4:17 

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.