Translation of Shortcuts in Menus

This forum designated to discuss the TsiLang Components Suite.
Post Reply
kai
Posts: 2
Joined: Wed Aug 06, 2003 4:40 pm

Translation of Shortcuts in Menus

Post by kai »

Hi,

I am using the TSILang and I am very satisfied with them. I have only on problem: I can't translate the Shortcut names of Menuitems. That means, that "Strg+N" is replaced with "Ctrl+N". Is there a solution for this?

Kai

isiticov
Site Admin
Posts: 2208
Joined: Thu Nov 21, 2002 3:17 pm

Post by isiticov »

Unfortunately, Borland didn't make these strings (shortcut names) used as resource strings when translating shortcut values to string representation in menus unit.
So this is possible only by altering code in standard Menus.Pas unit and translating resource strings responsible for shortcut names using resource string wizard from expert. If this is acceptable for you we can provide step by step instructions. Let us know about your decision.

stem
Posts: 3
Joined: Thu Aug 07, 2003 11:00 am

Post by stem »

Hi,

I have the same question as kai.

I've developed internationalization functions
for BCB 4 on my own which work perfectly,
even for Japanese. The language file are
simple text files which are loaded at
run-time.

The *only* thing left to translate are the
menu shortcuts.

If anybody as any ideas, let me know! :D


Thanx, Stefan

isiticov
Site Admin
Posts: 2208
Joined: Thu Nov 21, 2002 3:17 pm

Post by isiticov »

In SiComp.pas unit there is procedure called:
procedure OverwriteProcedure(OldProcedure, NewProcedure: Pointer);

Just copy it into your code and use the following example code which allows translation menu shortcuts:

Code: Select all

function SiShortCutToText(ShortCut: TShortCut): string;

implementation
{$R *.dfm}

function GetSpecialName(ShortCut: TShortCut): string;
var
  ScanCode: Integer;
  KeyName: array[0..255] of Char;
begin
  Result := '';
  ScanCode := MapVirtualKey(WordRec(ShortCut).Lo, 0) shl 16;
  if ScanCode <> 0 then
  begin
    GetKeyNameText&#40;ScanCode, KeyName, SizeOf&#40;KeyName&#41;&#41;;
    GetSpecialName &#58;= KeyName;
  end;
end;

function SiShortCutToText&#40;ShortCut&#58; TShortCut&#41;&#58; string;
var
  Name&#58; string;
begin
  case WordRec&#40;ShortCut&#41;.Lo of
    $08&#58; Name &#58;= Form1.siLang1.GetTextOrDefault&#40;'IDS_0' &#40;* 'BkSp' *&#41; &#41;;
    $09&#58; Name &#58;= Form1.siLang1.GetTextOrDefault&#40;'IDS_1' &#40;* 'Tab' *&#41; &#41;;
    $0D&#58; Name &#58;= Form1.siLang1.GetTextOrDefault&#40;'IDS_2' &#40;* 'Enter' *&#41; &#41;;
    $1B&#58; Name &#58;= Form1.siLang1.GetTextOrDefault&#40;'IDS_3' &#40;* 'Esc' *&#41; &#41;;
    $20&#58; Name &#58;= Form1.siLang1.GetTextOrDefault&#40;'IDS_4' &#40;* 'Space' *&#41; &#41;;
    $21&#58; Name &#58;= Form1.siLang1.GetTextOrDefault&#40;'IDS_5' &#40;* 'PgUp' *&#41; &#41;;
    $22&#58; Name &#58;= Form1.siLang1.GetTextOrDefault&#40;'IDS_6' &#40;* 'PgDn' *&#41; &#41;;
    $23&#58; Name &#58;= Form1.siLang1.GetTextOrDefault&#40;'IDS_7' &#40;* 'End' *&#41; &#41;;
    $24&#58; Name &#58;= Form1.siLang1.GetTextOrDefault&#40;'IDS_8' &#40;* 'Home' *&#41; &#41;;
    $25&#58; Name &#58;= Form1.siLang1.GetTextOrDefault&#40;'IDS_9' &#40;* 'Left' *&#41; &#41;;
    $26&#58; Name &#58;= Form1.siLang1.GetTextOrDefault&#40;'IDS_10' &#40;* 'Up' *&#41; &#41;;
    $27&#58; Name &#58;= Form1.siLang1.GetTextOrDefault&#40;'IDS_11' &#40;* 'Right' *&#41; &#41;;
    $28&#58; Name &#58;= Form1.siLang1.GetTextOrDefault&#40;'IDS_12' &#40;* 'Down' *&#41; &#41;;
    $2D&#58; Name &#58;= Form1.siLang1.GetTextOrDefault&#40;'IDS_13' &#40;* 'Ins' *&#41; &#41;;
    $2E&#58; Name &#58;= Form1.siLang1.GetTextOrDefault&#40;'IDS_14' &#40;* 'Del' *&#41; &#41;;
    $30..$39&#58; Name &#58;= Chr&#40;WordRec&#40;ShortCut&#41;.Lo - $30 + Ord&#40;'0'&#41;&#41;;
    $41..$5A&#58; Name &#58;= Chr&#40;WordRec&#40;ShortCut&#41;.Lo - $41 + Ord&#40;'A'&#41;&#41;;
    $60..$69&#58; Name &#58;= Chr&#40;WordRec&#40;ShortCut&#41;.Lo - $60 + Ord&#40;'0'&#41;&#41;;
    $70..$87&#58; Name &#58;= 'F' + IntToStr&#40;WordRec&#40;ShortCut&#41;.Lo - $6F&#41;;
  else
    Name &#58;= GetSpecialName&#40;ShortCut&#41;;
  end;
  if Name <> '' then
  begin
    Result &#58;= '';
    if ShortCut and scShift <> 0 then Result &#58;= Result + Form1.siLang1.GetTextOrDefault&#40;'IDS_15' &#40;* 'Shift+' *&#41; &#41;;
    if ShortCut and scCtrl <> 0 then Result &#58;= Result + Form1.siLang1.GetTextOrDefault&#40;'IDS_16' &#40;* 'Ctrl+' *&#41; &#41;;
    if ShortCut and scAlt <> 0 then Result &#58;= Result + Form1.siLang1.GetTextOrDefault&#40;'IDS_17' &#40;* 'Alt+' *&#41; &#41;;
    Result &#58;= Result + Name;
  end
  else Result &#58;= '';
end;

type
    TAccessMenu = class&#40;TMenu&#41;;

procedure TForm1.Language1Click&#40;Sender&#58; TObject&#41;;
begin
  siLang1.ActiveLanguage &#58;= TComponent&#40;Sender&#41;.Tag;
  TAccessMenu&#40;MainMenu1&#41;.UpdateItems;
end;

procedure TForm1.FormCreate&#40;Sender&#58; TObject&#41;;
begin
  TAccessMenu&#40;MainMenu1&#41;.UpdateItems;
end;

initialization
  OverwriteProcedure&#40;@ShortCutToText, @SiShortCutToText&#41;;
end.

Hope this helps.

stem
Posts: 3
Joined: Thu Aug 07, 2003 11:00 am

Post by stem »

Hi,

I've read your answer but still can't see how the
translation of menu shortcuts in general works
(especially without TsiLang components).

It would be great if you can describe the steps
I need to do a little bit more in detail.


Thanx alot and greetings from *hot* Germany

Stefan

isiticov
Site Admin
Posts: 2208
Joined: Thu Nov 21, 2002 3:17 pm

Post by isiticov »

The answer was how to translate the shortcuts using TsiLang Components Suite.
In order to translate them without using TsiLang Components Suite I can suggest the following way:
1. Delphi/C++Builder show shortcuts in menu after converting the shortcut value (word) to text prezentation.
2. This is done using ShortCutToText() function from Menus.pas unit.
3. Unfortunately, Borland didn't make the strings that are used in this conversation used as resource strings. You can see this by investigating original source code of ShortCutToText() function from Menus.pas unit.
4. So in order to translate this you can make a "hack" by overwriting the ShortcutToText() function in your executable by yours implementation. And in your implementation just use strings corresponding to shortcut modifiers as Shift, CTRL... and using your translate algorithm translate them.

OverwriteProcedure is implemented like this:

Code: Select all

procedure OverwriteProcedure&#40;OldProcedure, NewProcedure&#58; pointer&#41;;
var
  x&#58; pchar;
  y&#58; integer;
  ov2, ov&#58; cardinal;
begin
  x &#58;= PChar&#40;OldProcedure&#41;;
  if not VirtualProtect&#40;Pointer&#40;x&#41;, 5, PAGE_EXECUTE_READWRITE, @ov&#41; then
    RaiseLastOSError;

  x&#91;0&#93; &#58;= char&#40;$E9&#41;;
  y &#58;= integer&#40;NewProcedure&#41; - integer&#40;OldProcedure&#41; - 5;
  x&#91;1&#93; &#58;= char&#40;y and 255&#41;;
  x&#91;2&#93; &#58;= char&#40;&#40;y shr 8&#41; and 255&#41;;
  x&#91;3&#93; &#58;= char&#40;&#40;y shr 16&#41; and 255&#41;;
  x&#91;4&#93; &#58;= char&#40;&#40;y shr 24&#41; and 255&#41;;

  if not VirtualProtect&#40;Pointer&#40;x&#41;, 5, ov, @ov2&#41; then
    RaiseLastOSError;
end;

stem
Posts: 3
Joined: Thu Aug 07, 2003 11:00 am

Post by stem »

Hi!

Thanx for your answer!

I've converted your code into C++. Does anybody find any
errors? At least it compiles without errors or warnings ... :D

Code: Select all

void OverwriteProcedure&#40;Pointer OldProcedure, Pointer NewProcedure&#41;
&#123;
   PChar x;
   int y;
   unsigned long ov2, ov;

   x = PChar&#40;OldProcedure&#41;;

   if &#40;!VirtualProtect&#40;Pointer&#40;x&#41;, 5, PAGE_EXECUTE_READWRITE, &ov&#41;&#41;
      RaiseLastWin32Error&#40;&#41;;

   x&#91;0&#93; = char&#40;'E9'&#41;;

   y = int&#40;NewProcedure&#41; - int&#40;OldProcedure&#41; - 5;

   x&#91;1&#93; = char&#40;y & 255&#41;;
   x&#91;2&#93; = char&#40;&#40;y >> 8&#41; & 255&#41;;
   x&#91;3&#93; = char&#40;&#40;y >> 16&#41; & 255&#41;;
   x&#91;4&#93; = char&#40;&#40;y >> 24&#41; & 255&#41;;

   if &#40;!VirtualProtect&#40;Pointer&#40;x&#41;, 5, ov, &ov2&#41;&#41;
      RaiseLastWin32Error&#40;&#41;;
&#125;
Unfortunately, Borland C++ Builder 4 doesn't have a file
called "Menus.pas". It only has some headers or already
compiled files.

Could anyone, please, post the ShortcutToText() function
here?

By the way: Where should I call the overwrite function?
Before any forms are created?

Thanx

Stefan

isiticov
Site Admin
Posts: 2208
Joined: Thu Nov 21, 2002 3:17 pm

Post by isiticov »

Below is example code from C++ Builder to perform discussed task:

Code: Select all

AnsiString __fastcall SiShortCutToText&#40;TShortCut ShortCut&#41;
&#123;
  AnsiString res = "AAA";
  return &#40;res&#41;;
&#125;

void OverwriteProcedure&#40;Pointer OldProcedure, Pointer NewProcedure&#41;
&#123;
   PChar x;
   int y;
   unsigned long ov2, ov;

   x = PChar&#40;OldProcedure&#41;;

   if &#40;!VirtualProtect&#40;Pointer&#40;x&#41;, 5, PAGE_EXECUTE_READWRITE, &ov&#41;&#41;
      RaiseLastWin32Error&#40;&#41;;

   x&#91;0&#93; = 0xE9;

   y = int&#40;NewProcedure&#41; - int&#40;OldProcedure&#41; - 5; 

   x&#91;1&#93; = char&#40;y & 255&#41;; 
   x&#91;2&#93; = char&#40;&#40;y >> 8&#41; & 255&#41;; 
   x&#91;3&#93; = char&#40;&#40;y >> 16&#41; & 255&#41;; 
   x&#91;4&#93; = char&#40;&#40;y >> 24&#41; & 255&#41;; 

   if &#40;!VirtualProtect&#40;Pointer&#40;x&#41;, 5, ov, &ov2&#41;&#41; 
      RaiseLastWin32Error&#40;&#41;; 
&#125; 


void __fastcall TForm1&#58;&#58;FormCreate&#40;TObject *Sender&#41;
&#123;
  OverwriteProcedure&#40;&ShortCutToText, &SiShortCutToText&#41;;
  // to update menu and recreate shortcuts
  MainMenu1->Images = NULL;
&#125;

Post Reply