Localization of MessageDlg in Delphi

Built-in MessageDlg function shows texts such as Yes, No, Confirmation only in English.

function MyMessageDlg(CONST Msg: string; DlgTypt: TmsgDlgType; button: TMsgDlgButtons;
 Caption: ARRAY OF string; dlgcaption: string): Integer;
var
  aMsgdlg: TForm;
  i: Integer;
  Dlgbutton: Tbutton;
  Captionindex: Integer;
begin
  aMsgdlg := createMessageDialog(Msg, DlgTypt, button);
  aMsgdlg.Caption := dlgcaption;
  //aMsgdlg.BiDiMode := bdRightToLeft;
  Captionindex := 0;
  for i := 0 to aMsgdlg.componentcount - 1 Do
  begin
    if (aMsgdlg.components[i] is Tbutton) then
    Begin
      Dlgbutton := Tbutton(aMsgdlg.components[i]);
      if Captionindex <= High(Caption) then Dlgbutton.Caption := Caption[Captionindex];
      inc(Captionindex);
    end;
  end;
  Result := aMsgdlg.Showmodal;
end;

// StayOnTop AWARE VERSION
function MyMessageDlgV2(CONST Msg: string; DlgTypt: TmsgDlgType; button: TMsgDlgButtons;
 Caption: ARRAY OF string; dlgcaption: string): Integer;
var
  aMsgdlg: TForm;
  i: Integer;
  Dlgbutton: Tbutton;
  Captionindex: Integer;
  IsStayOnTop: boolean;
begin
  if Form1.FormStyle=fsStayOnTop then
  begin
    IsStayOnTop:=True;
    Form1.FormStyle:=fsNormal;
  end else
  begin
    IsStayOnTop:=False;
  end;

  aMsgdlg := createMessageDialog(Msg, DlgTypt, button);
  aMsgdlg.Caption := dlgcaption;
  //aMsgdlg.BiDiMode := bdRightToLeft;
  Captionindex := 0;
  for i := 0 to aMsgdlg.componentcount - 1 Do
  begin
    if (aMsgdlg.components[i] is Tbutton) then
    Begin
      Dlgbutton := Tbutton(aMsgdlg.components[i]);
      if Captionindex <= High(Caption) then Dlgbutton.Caption := Caption[Captionindex];
      inc(Captionindex);
    end;
  end;
  Result := aMsgdlg.Showmodal;

  if IsStayOnTop then
  begin
    Form1.FormStyle:=fsStayOnTop;
  end;
end;

How to use it?

var
  buttonSelected : Integer;
begin
  buttonSelected := MyMessageDlg('Are you sure?',mtConfirmation,[mbYes, mbNo],['Yes','No'],'Confirmation');
  if buttonSelected=mrYes then
  begin
  end;
end;

Leave a comment