Localizing Delphi Applications
Localization is the process of converting an application so it functions in a specific locale. At a basic level this requires translation of all text that a user interacts with - in the form of menu items, button captions, prompts etc - to the locale language. We present a simple technique for accomplishing this. This technique has the advantage that it does not require separate versions of all or part of the source code to be maintained. Most user interface elements can be translated to a new target language without recompiling the application. To use this technique follow the steps listed below
- Download and install the latest version of the Delphi TNT Unicode components. They are available for a modest fee from TMSSoftware.
- Now do the following
- Place a reference to LocForm.PAS - available as part of the download from the link below - in every form in your application.
- Change every
type TMyForm = class(TForm)declaration in your code to readTMyForm = class(TLocForm) - Ensure that there is a reference to LangData.PAS in the lower uses clause of the main form in your application.
- In that form, create a main or popup menu entry to be populated dynamically with user interface language entries. At run time you can populate this menu with language options by examining the entries in the _languages global stringlist object, declared in LocForm.PAS. You should attach a suitable OnClick event handler to each of these menu items so you can perform instant application localization in response to language selection by the user. For the most part this only involves calling the TLocForm.Localize method.
- Once this is done, define the global conditional _buildloc in your Projects|Options. This defines the
Builder
mode for the application. In this mode, all information required for localization is collated and put into a text file, AppName.TXT - whereAppName
is the name of your application. - Build your application and run it. Make sure that every form in the application is opened at least once
- Open the AppName.TXT file. It consists of three parts which you can customize to deliver application localization
- FormData :This section contains entires bearing the form
OwnerName:ControlName:|caption|hint|. OwnerName:ControlName: fully identifies the control. The |caption|hint| portion contains the current caption and popup hint for the control. Edit the file to provide localized versions of these entries. - MsgData :This section contains the text for messages displayed in dialog boxes. Once again, you should edit this text for the target locale language.
- BtnData:Finally, edit this third section to provide button captions and hints in the target locale language.
- FormData :This section contains entires bearing the form
- After all file edits have been done save the file under a suitable name. Typically this should be the name of the target locale language. For instance, if you have created a German translation, you could name the file German.TXT
- Run ETLLock - supplied in the download available from the link below - and create locked versions of each of your language translation files. The locked files, bearing the extension .ETL, ensure that your application will only see valid language translation files.
- Remove the _buildloc global define and rebuild your application. This defines the
user
mode for your application - this is the version you should distribute to your end users. - Along with your application you should distribute .ETL files for every locale that you want to support. Needless to say, you can make additional locales available at any time by simply providing more .ETL files.
Usage: Run the demonstration application, Localizer.EXE. See how various user interface elements get localized when you select one of the three available languages - English, French or Tamil. Now open Localizer.TXT and edit its entries to provide localizations in your own language. Save the edited file under a suitable name, e.g. Turkish.TXT and then lock it using ETLLock. Run Localizer again and verify that your new localizations are working correctly.
DownloadNow examine the source code in Localizer.dpr and adapt it to your own needs if necessary.