Delphi: how to localize the description for a menu shortcut?

Is there a way to get a localized shortcut description, for example Ctrl + Z , so that I get " Ctrl + Z " if the application works in the English system and " Strg + Z " in the German system?

The VCL ShortCutToText function is not internationalized. The GetKeyNameText API function is slightly better, but still not perfect: if you switch the regional settings of German XP to English (USA), it still produces German texts. Also, the results are in CAPITALS, which are ugly.

Explanation: I know how I can replace the ShortCutToText or Smkc * strings with custom versions. But for use I need translated strings. And I would like to get them from the OS (or the like).

Update: It seems that Microsoft expects the developers to do the translation themselves - see 2. in Associating a menu item with an accelerator key . Quote:

For example, to assign CTRL + O to the Open command in the File menu, you change the title of the menu items so that it looks like this:

Open \ tCtrl + O

The menu item in the menu editor has been updated to display the new title as you type it.

Note that the shortcut is manually added to the title.

+6
delphi internationalization localization keyboard-shortcuts shortcut
source share
2 answers

I will answer my question so that I have something to accept: it looks like Microsoft expects developers to do the translation themselves - see 2. in Associating a menu item with an accelerator key . Quote:

For example, to assign CTRL + O to the Open command in the File menu, you change the title of the menu items so that it looks like this:

Open \ tCtrl + O

The menu item in the menu editor has been updated to display the new title as you type it.

Note that the shortcut is manually added to the title.

+1
source share

ShortCutToText uses the MenuKeyCaps array. This cannot be changed directly (because it is in the implementation of the Menus block), but the array is filled with resourcestrings , which can be translated using various translation tools.

You need to translate the constant constant SmkcCtrl, which is located in consts.pas (depending on the version of Delphi).

[edit]

Or you can download BigProcHook.pas , which I created too complex functions and replaced them myself. Then you can write an override that calls the regular ShortCutToText function and replaces the text "Ctrl" with "Strg" (or vice versa) without a menu, even knowing it. But I would use this only as a last resort, because I think it’s better to just transfer the resource. If you want to use the hook, download and turn on the device and add the following code to any block (separate, if you want) a new block.

uses BigProcHook, Menus; var FHook: TBigProcHook; // The replacement function function MyShortCutToText(ShortCut: TShortCut): string; begin FHook.Hooked := False; try Result := ShortCutToText(ShortCut); Result := StringReplace(Result, 'Ctrl', 'Whatever', []); finally FHook.Hooked := True; end; end; initialization FHook := TBigProcHook.Create(@ShortCutToText, @MyShortCutToText); finalization FHook.Hooked := False; FHook.Free; end. 

It will replace Ctrl in the text of the label with any text that you like, without the need to change any other code.

+3
source share

All Articles