Skip to main content
Email Password   helpLost your password?

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 DLL ID Code Language description
arabic.dll unknown - -
chinese_simplified.dll unknown - -
czech.dll 0x0405 CSY Czech
danish.dll 0x0406 DAN Danish
dutch.dll 0x0413
0x0813
NLD
NLB
Dutch (Standard)
Belgian (Flemish)
finnish.dll 0x040b FIN Finnish
french.dll 0x040c
0x080c
0x0c0c
0x100c
FRA
FRB
FRC
FRS
French (standard)
Belgian
Canadian
Swiss
german.dll 0x0407
0x0807
0x0c07
DEU
DES
DEA
German (standard)
Swiss
Austrian
greek.dll 0x0408 ELL Greek
hungarian.dll 0x040e HUN Hungarian
icelandic.dll 0x040f ISL Islandic
italian.dll 0x0410
0x0810
ITA
ITS
Italian (standard)
Swiss
lithuanion.dll unknown - -
norwegian.dll 0x0414
0x0814
NOR
NON
Norwegian (bokmal)
Norwegian (nynorsk)
polish.dll 0x0415 PLK Polish
portugues_br.dll 0x0416 PTB Portuguese (Brazilian)
russian.dll 0x0419 RUS Russian
slovak.dll 0x041b SKY Slovak
spanish.dll 0x040a
0x080a
0x0c0a
ESP
ESM
ESN
Spanish (standard/traditional)
Mexican
Spanish (modern)
swedish.dll 0x041D SVE Swedish
turkish.dll 0x041f TRK Turkish

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralIt's good! I have used this Class in my application to support simple Chinese, which language code is 0x0804. -- Thanks. Pin
Daniel.Li
16:54 1 Nov '09  
QuestionWill Japanese string supported? Pin
Vamsee_Krishna
21:10 4 May '08  
GeneralWill your application support chinese or japanese literals ? Pin
ganesa moorthy
20:49 3 Apr '08  
GeneralRe: Will your application support chinese or japanese literals ? Pin
Geert van Horrik
20:53 3 Apr '08  
GeneralOverkill solution Pin
KarstenK
2:36 1 Oct '07  
GeneralWhy re-enventing the wheel again and again? Pin
localizer
5:35 4 May '06  
GeneralCopy string table fails !!! Pin
jack2000de
7:09 27 Feb '06  
GeneralRe: Copy string table fails !!! Pin
Geert van Horrik
7:53 27 Feb '06  
GeneralRe: Copy string table fails !!! Pin
jack2000de
22:31 27 Feb '06  
GeneralRe: Copy string table fails !!! Pin
Geert van Horrik
1:08 28 Feb '06  
GeneralHelp Pin
sreejith ss nair
22:31 18 Sep '05  
GeneralRe: Help Pin
Geert van Horrik
0:09 19 Sep '05  
GeneralRe: Help Pin
sreejith ss nair
20:35 20 Sep '05  
GeneralRe: Help Pin
sreejith ss nair
20:35 20 Sep '05  
GeneralMultilingual support for application Pin
geoyar
13:29 25 Jun '05  
GeneralRe: Multilingual support for application Pin
StarlightNo1
9:36 27 Jun '05  
GeneralEasier way: few code changes and languge resource-only DLL. Pin
yuhaian
11:22 20 Jul '05  
GeneralGood work ! Pin
unionize
8:09 21 Jun '05  
GeneralA what about menu ?! Pin
Fredy Pumpicka
23:52 16 Jun '05  
GeneralRe: A what about menu ?! Pin
disabledman
21:27 19 Jul '05  
GeneralAnother another approach Pin
EzioXia
1:46 6 Jun '05  
GeneralRe: Another another approach Pin
Geert van Horrik
2:06 6 Jun '05  
GeneralRe: Another another approach Pin
Dimitris Vassiliades
0:25 14 Jun '05  
GeneralRe: Another another approach Pin
Geert van Horrik
21:33 13 Jun '05  
GeneralHere is "Another approach" Pin
yuhaian
11:30 20 Jul '05  


Last Updated 7 Jun 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009