Technology Programming

Dirty Delphi Localization

Tip code and idea by Jens Borrisholt
The "fastest path to Delphi localization" article describes how to do a simple and dirty localization of various VCL messages used by the MessageDlg function and alike.

The idea was to locate the VCL's consts.pas unit (and related) and manually change (translate) English resource strings to those that are specific to your locale - your country.

HookResourceString

Another approach by Jens Borrisholt is far more better - you do not mess with Delphi's original consts.pas - but rather you write your own unit and translate the strings that you need in another language.
The unit simply calls the HookResourceString function by passing in the name of the resource, for example "@SMsgDlgWarning" along with your language translation, for example "Upozorenje" (Croatian).


All the calls to MessageDlg will now have buttons and captions and default messages translated to your language (in this example: Croatian)

unit LanguageControl;interfaceimplementationuses   Windows, Consts;// Assign new value to a resource stringprocedure HookResourceString(ResStringRec: pResStringRec; NewStr: pChar) ; var   OldProtect: DWORD; begin   VirtualProtect(ResStringRec, SizeOf(ResStringRec^), PAGE_EXECUTE_READWRITE, @OldProtect) ;   ResStringRec^.Identifier := Integer(NewStr) ;   VirtualProtect(ResStringRec, SizeOf(ResStringRec^), OldProtect, @OldProtect) ; end;initialization   HookResourceString(@SCannotOpenClipboard, 'Nije moguce otvoriti Clipboard') ;   HookResourceString(@SMsgDlgWarning, 'Upozorenje') ;   HookResourceString(@SMsgDlgError, 'Greska') ;   HookResourceString(@SMsgDlgInformation, 'Obavijest') ;   HookResourceString(@SMsgDlgConfirm, 'Potvrda') ;   HookResourceString(@SMsgDlgYes, 'Da') ;   HookResourceString(@SMsgDlgNo, 'Ne') ;   HookResourceString(@SMsgDlgOK, 'OK') ;   HookResourceString(@SMsgDlgCancel, 'Odustani') ;   HookResourceString(@SMsgDlgHelp, 'Upute') ;   HookResourceString(@SMsgDlgHelpNone, 'Ne postoje Upute') ;   HookResourceString(@SMsgDlgHelpHelp, 'Upute') ;   HookResourceString(@SMsgDlgAbort, 'Prekini') ;   HookResourceString(@SMsgDlgRetry, 'Ponovi') ;   HookResourceString(@SMsgDlgIgnore, 'Zanemari') ;   HookResourceString(@SMsgDlgAll, 'Sve') ;   HookResourceString(@SMsgDlgNoToAll, 'Ne za sve') ;   HookResourceString(@SMsgDlgYesToAll, 'Da za sve') ; end.

Simply include this unit to your project and have the above messages translated to your language. You do not need to manually add this unit to the uses clause of any unit in your project (except the DPR file where it will be listed by design).
Delphi tips navigator:
» Get Subdirectories of a specified Directory from Delphi
« Reposition Inherited Control Back to Its Original Position - Revert To Inherited in Delphi IDE

Leave a reply