Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: How to localize description for a menu shortcut?

Is there a way to get a localized description of a shortcut like Ctrl+Z so that I get "Ctrl+Z" if the app runs on an English system and "Strg+Z" on a German system?

The VCL function ShortCutToText isn't internationalized. The API function GetKeyNameText is a bit better but still not perfect: If one switches the regional settings of a German XP to English (US), it still produces German texts. Besides the results are in CAPITALS which is ugly.

Clarification: I know how I can replace ShortCutToText or the Smkc* resource strings with customized versions. But to use that I need the translated strings. And I would like to get these from the OS (or similar).

Update: It looks like Microsoft expects developers to do the translation on their own - see 2. in Associating a Menu Item with an Accelerator Key. Quote:

For example, to assign CTRL+O to the Open command on the File menu, you modify the menu item’s caption so that it looks like this:

Open\tCtrl+O

The menu item in the Menu editor is updated to reflect the new caption as you type it.

Note that the shortcut is manually appended to the caption.

like image 434
Uli Gerhardt Avatar asked Jan 05 '11 13:01

Uli Gerhardt


2 Answers

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

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

[edit]

Or you can download BigProcHook.pas, which I created too hook functions and replace them by your own. You could then write an override that calls the regular ShortCutToText function and replaces the text 'Ctrl' with 'Strg' (or vice versa) without the menu even knowing it. But I would use this as a last resort only, because I think it is better to just translate the resource. If you want to use the hook, download and include the unit, and add the following code in any unit (a separate, new unit if you like).

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 shortcut text with whatever text you like, without having to change any other code.

like image 101
GolezTrol Avatar answered Sep 28 '22 10:09

GolezTrol


I'll answer my own question so that I have something to accept: It looks like Microsoft expects developers to do the translation on their own - see 2. in Associating a Menu Item with an Accelerator Key. Quote:

For example, to assign CTRL+O to the Open command on the File menu, you modify the menu item’s caption so that it looks like this:

Open\tCtrl+O

The menu item in the Menu editor is updated to reflect the new caption as you type it.

Note that the shortcut is manually appended to the caption.

like image 25
Uli Gerhardt Avatar answered Sep 28 '22 08:09

Uli Gerhardt