| 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. | |||||||||||||||||||
Are you aware?
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:
![]() What do you say about the next dialog: ![]() 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: ![]() *consts*.pas
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: 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... 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:
That's it And finally, if you have any questions or comments to the article, please post to the Delphi Programming Forum. |
|||||||||||||||||||




