Monday, November 10, 2008

Creating Localized Hello World App using MUI Technology

Open up an new Project for creating a Win32 Console Application in Visual C++. Notice that the Solution Name is "Hello World" and Project Name is "en-us". To support multiple languages, our solution will contain projects in different languages. More on that later.



Choose Windows applications in the Applications Settings Window and click Finish

For clarity, we will rename en-us.h and en-us.cpp to HelloWorld.h and HelloWorld.cpp respectively. Don't forget to correspondingly change the name of the header file in HelloWorld.cpp. First we will create the app in English and later add more languages.



Coding in Hello World

All the localizable resources are kept in rc file. In this case it is en-us.rc. Double Click en-us.rc file to open up its resources. Under String table, we will declare a new String ID : IDS_HW with the caption "Hello World".



Declare a global variable gStrHW in HelloWorld.cpp and use LoadString to intialize it with the resourse. LoadString uses the Thread Preferred language and fetches the appropriate resources.




Under the function WndProc, add a TextOut to write this string out to the window. Build and Compile the project to see if it works.


Preparing a Resource Configuration File

A resource configuration file helps a resource compiler divide up localizable and neutral resources.Click File -> New -> File and select an XML file from the list and add the following code to the file.


Save the file as mui.rcconfig and add it to the project. I have removed the extension xml from the file but I don't think thats necessary.

Click the following link to find more info on creating Resource config file. http://msdn.microsoft.com/en-us/library/aa365064(VS.85).aspx

In the properties page, Under Resources add the following line to the Additional Options, so that it could generate helloworld.muires file in addition to en-us.res file. Also it will be able to use mui.rcconfig file to divide up the resources in these two files.



In the properties page under Linker->General, change Output File from $(OutDir)\$(ProjectName).exe to $(OutDir)\$(SolutionName).exe. Since we will end up with one binary for all the languages, this will ensure that our output file is named HelloWorld.exe and not en-us.exe.

Language specific muires file generated after build will have to be linked and converted to .exe.mui file. Also, we are arranging files into appropriate folders for localization to work. Add following commands to Command Line under Post Build Event under Properties.



Build the project. The solution will be compiled and files will be copied to HelloWorld\Win32\Debug\HelloWorld.exe. Language specific mui files will be copied to its corresponding location as well. Language neutral HelloWorld.exe will not run without its mui files and for this reason you won't be able to run it in Visual Studio anymore.


Adding Additional Language

Add another project (Win32 Console Application) to this solution and name it fr-fr. This time select Static Library. Delete stdafx.cpp file. Right click on Resource Files and add String Table as a new resource. Copy all the resource Strings from en-us.rc to fr-fr.rc String Table and change the captions from English to French. In the Properties dialog box of String Table, change the language to French (France). Add previously created mui.rcconfig file to this project as well.


In the properties page, Under Resources add the following line to the Additional Options, so that it could generate helloworld.muires file in addition to fr-fr.res file and divide up the resources as defined in mui.rcconfig. It is exactly the same as we did for en-us.

/r /fm "$(IntDir)"/helloworld.muires /q ../mui.rcconfig

Add the following lines to Post Build Event. Notice that in addition to linking the muires file and creating folders, we are copying over the checksum from Language Neutral .exe to French specific .mui.exe file.

Set Project dependency - fr-fr on en-us.

Build the application. To build a release application, all the additional steps will have to be manually copied over to Release Properties of each Project.

You can download the Visual Studio Project from here.

3 comments:

Anonymous said...

Much easier and simpler tutorial on MUI to follow than Microsoft but I cannot get it to work!

Two questions:

1) What are the exact commands that have to be added to the Command Line under Post Build Event under Properties for en-us project? (The graphics image is not clear).

2) What changes did you make to Windows for 'Bonjour' to appear?

I am running on Windows 7 and selected Control Panel, Region and Language to change Format, Location and finally system locale to French but the application does not pick up the correct resource to display 'Bonjour'.

Anonymous said...

French language resources e.g. Bonjour will be loaded and displayed in the following two scenarios:

1) If the application is deployed on machine with a French language version of the windows operating system.

2) If the application makes a specific call to SetThreadUILanguage( MAKELANGID( LANG_FRENCH, SUBLANG_FRENCH ) );

Anonymous said...

What if I have multiple .rc files, not just one as in the example? If I create us-us2.rc and fr-fr2.rc files with containing one localized string each and then try to build it i get error:
1>CVTRES : fatal error CVT1100: duplicate resource. type:MUI, name:1, language:0x0409
1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt.
I spent a lot of time googling the error but none of the solutions are working. Please help

Followers