Action

Localize your first application

Assuming that you have created a simple application in Delphi, this section will show you how to localize it.

First, you must add gnugettext.pas to your project. It is recommended to copy this file to your project directory, making it part of your application.

In order to translate your forms and datamodules, you must add gnugettext to the uses-clause of all units that have a form or datamodule, and put this line into the OnCreate event of your forms and datamodules:

TranslateComponent (self);

In this line, self refers to the form or datamodule to which this event belongs. The TranslateComponent procedure then translates all string properties of all components on the form or datamodule. Please note that all subcomponents are translated - for a TDataset component this includes the TField subcomponents etc.

Unfortunately, some string properties will raise an Exception or create unwanted side effects if they are translated. For instance, if you translate the IndexFieldName property of a TClientDataset to something that is not the name of an index, it will raise an Exception. In order to instruct the TranslateComponent procedure that it should not translate certain string properties, you should add procedure calls like the ones specified in the appendix called How to handle specific classes> just before the TranslateComponent(self) call in your main form or in the .dpr file. You can see the source code for a small, localized application in the appendix called "Hello, World" source code>

Now, you must ensure that all strings inside your source code are translated properly. If you have a line like this:

ShowMessage ('Hello, World');

Then you must add _() around the string, like this:

ShowMessage (_('Hello, World'));

Now your program is internationalized, but it hasn't been localized, yet. This means that your program can run in another language, if a translation would be present, but we didn't make a translation, yet. In order to make a translation, we have to get a list of messages first.

Figure 1. Explorer integration of string extraction

In order to get this list, click with the right mouse button on the file folder that contains your Delphi source code, and choose "Extract translations to template", just as you can see it in Figure 1>.

This will create a file named default.po with all the texts to be translated. Because it has no translations in it, it is named a "translation template". You should download poEdit and use this program to translate the messages in the po file.

When you have translated the po file, click with your right mouse button on the filename in the Windows Explorer, and choose "Compile to mo file". This will compile the po file and generate the binary mo file needed by your application. If your application is located in c:\my\program\path\myprogram.exe, you must put the default.mo file in c:\my\program\path\locale\##\LC_MESSAGES\default.mo. In this path, ## represents the two-letter language code that you can find in the Section called ISO 639 language codes in the appendix called Standards>.

That should be it - your application now uses the translation when the Windows settings corresponds to the two-letter language code that you chose.