MultiLingual User Interface

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.

Followers