1. Home
  2. Computing & Technology
  3. Delphi Programming
The fastest path to Delphi localization
How to change the captions on [Yes], [No], [Cancel] and similar buttons that appear on dialog and message boxes in Delphi applications. Plus how to personalize error, warning and other messages in those dialogs.
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Using the Integrated Translation Environment
• Localization tools and VCL
• How to Make Multilingual VCL Components
 Elsewhere on the Web
• Translated messages for various languages

   Are you aware?
If you are the owner of the Professional or Enterprise version of Delphi you can do what any other developers using some other languages may not: you may see (and change) the source code of all the components that ship with Delphi.

Even though it is not advisable to change the code Delphi is made of, there is (at least) one situation when this can be really handy - centralization and localization of the VCL and RTL messages and various message dialog boxes. This gives Delphi developers an easy and productive way to personalize and localize their applications.

Suppose you are writing a database application. It is highly possible that you'll have a form with a DBNavigator component connected to a DBGrid displaying data from a dataset. When the user tries to delete the current record (by clicking the Delete button) a message box like this will pop up:

Standard Confirm Delete

What do you say about the next dialog:

Personalized confirm delete

Or, suppose you have an english (or some other, not your country localized) version of Delphi and want to create applications showing dialogs with localized messages and buttons. The next picture shows Croatian confirm dialog:

Croatian confirm dialog

   *consts*.pas
If you look in the $(DELPHI)\Source\VCL folder you'll find a number of PAS (unit) files with the word consts in the file name. If you look inside, for example, the CONSTS.PAS unit you'll find out that it consist only of a large number of string constants defined under the resourcestring keyword.
For example, below is a section from the consts.pas unit

unit Consts;

interface

resourcestring
...
  SMsgDlgWarning = 'Warning';
  SMsgDlgError = 'Error';
  SMsgDlgInformation = 'Information';
  SMsgDlgConfirm = 'Confirm';
  SMsgDlgYes = '&Yes';
  SMsgDlgNo = '&No';
  SMsgDlgOK = 'OK';
  SMsgDlgCancel = 'Cancel';
...
implementation

end.

As you can imagine, all those constants are used with the MessageDlg function. If you localize those string values you'll be having your own version of the message box.

On the other hand, if you look at the OnClick event handler procedure for the TDBNavigator component you'll see that it uses the MessageDlg function to call a dialog box with the next code:
MessageDlg(SDeleteRecordQuestion, mtConfirmation, mbOKCancel, 0)

The SDeleteRecordQuestion constant is defined in the VDBConsts.pas unit. If you change its default value 'Delete record' with 'Are you sure you want to delete this record' you'll have a dialog box like the one above.

Some other units you'll find in the VCL folder are: ADOConsts (specific ADO messages), DBConsts, BDEConsts, and VDBConsts (variety of database/BDE messages), IBXConsts.pas (InterBase Express related messages), etc...

   Modifications...
Let's now see how to safely change those string constants to better suite your needs. For example to translate the button captions displayed by the ShowMessage / MessageDlg functions:

1) Copy Consts.pas from $(DELPHI)\Source\VCL to your projects folder. It is very important to make a copy of the files with resource strings - not to change those in the VCL folder. When you, for example, update Delphi with a new version, the *.pas files could be overwritten.

2) Translate the SMsgDlg... strings. Be sure NOT to change the name of the string constant, only change the value. If you find that it contains something like #d or #s be sure not to delete those character as they are used in formated message boxes - when a message box displays a string value plus some more info passed with it.

3) Put Consts.pas in the project search path. Or use Project|Add to project...

4) Recompile the project.

You may also want to localize all other necessary messages from other resource string units by copying and modifying them as well and including in your project.

Note: another way of showing messages in your application involves using the Message.MessageBox Windows API function to have the buttons in the language of the operating system. However, all the dialog boxes from the VCL, like ShowMessage and InputQuery, etc do not support language of the operating system - they use the string constants we explore in this article. The next code is an example of using the MessageBox function:

var
  Msg: string;
  Title: string;
begin
  Msg := 'Delete the record?';
  Title := 'Question';

  if Windows.MessageBox(
     Handle, 
     PChar(Msg), 
     PChar(Title), 
     MB_YESNO or MB_ICONQUESTION
  ) = ID_NO then
    // code to not delete
end;

   That's it
This is why you love Delphi. Ease of use and the power to do whatever you want. Beside translating messages and dialog buttons you could try using the tool specially designed to help you build multi lingual applications: the ITE manager. And of course, there is always a question of how numeric or date/time values are formatted.

And finally, if you have any questions or comments to the article, please post to the Delphi Programming Forum.

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.