Soon Santos
Soon Santos

Reputation: 2219

Change MessageDlg language delphi

When I use MessageDlg() function like the code below:

if MessageDlg(SWarningWishToDelete + ' ' + PersonName + '?',
  mtWarning, [mbNo, mbYes], 0) = mrYes then

The Yes/No buttons appears in English language instead of my OS language. I searched about it on the internet, however, I just found big and old solutions. I am wondering if there is an easy/new way to change it?

Upvotes: 5

Views: 4521

Answers (1)

Ilyes
Ilyes

Reputation: 14928

You can use CreateMessageDialog() as

procedure TForm1.Button1Click(Sender: TObject);
begin
  with CreateMessageDialog('Msg', mtConfirmation, [mbYes, mbNo], mbNo) do
    begin
      try
        TButton(FindComponent('Yes')).Caption:= 'نعم';
        TButton(FindComponent('No')).Caption:= 'لا';
        ShowModal;
      finally
        case ModalResult of
          mrYes: ShowMessage('You click نعم');
          mrNo: ShowMessage('You click لا');
          mrCancel: ShowMessage('Dialog cancled!');
        end;
        Free;
      end;
    end;
end;

Using TTaskDialog as

procedure TForm1.Button2Click(Sender: TObject);
Var
  Dlg: TTaskDialog;
begin
  Dlg := TTaskDialog.Create(Self);
  try
    with Dlg do
      begin
        CommonButtons:= [];
        Caption:= 'Caption here';
        Text:= 'Text here';
        with Buttons.Add do
          begin
            Caption:= 'نعم';
            ModalResult:= mrYes;
          end;
        with Buttons.Add do
          begin
            Caption:= 'لا';
            ModalResult:= mrNo;
          end;
        if Execute then
          case ModalResult of
            mrYes: ShowMessage('You click نعم');
            mrNo: ShowMessage('You click لا');
            mrCancel: ShowMessage('Dialog cancled!');
          end;
      end;
  finally
    Dlg.Free;
  end;
end;

Note: TTaskDialog is specific to a platform, and require Vista, Windows 7, or later Windows operating systems.

Just for fun, you can even create you own function as:

function TForm1.MyDialog(const DlgCaption, DlgText: String; DlgButtons: array of string;
  DlgType: TMsgDlgType; Buttons: TMsgDlgButtons): TForm;
Var
  I: Integer;
  F: TForm;
begin
  F:= CreateMessageDialog(Text, DlgType, Buttons);
  F.Caption:= DlgCaption;
  TLabel(F.Components[1]).Caption:= DlgText;
  for I := Low(DlgButtons) to High(DlgButtons) do
    begin
      TButton(F.Components[I+2]).Caption:= DlgButtons[I];
    end;
  Result:= F;
end;

and test it as

MyDialog('Caption', 'Text', ['Yes do', 'No don''t'], mtConfirmation, [mbYes, mbNo]).ShowModal;

Here is also another version for the function

function MyDialog(Const DlgCaption, DlgText: string; DlgButtons: array of string;
             DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; 
             BidiMode: TBiDiMode = bdLeftToRight;
             DefultButton: TMsgDlgBtn = mbOK):TForm;

Code of the function

function TForm1.MyDialog(const DlgCaption, DlgText: string;
  DlgButtons: array of string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons;
  BidiMode: TBiDiMode; DefultButton: TMsgDlgBtn): TForm;
Var
  I, CntBtns: Integer;
  F: TForm;
  Btn: TMsgDlgBtn;
begin
  // Check for strings
  CntBtns:= 0;
  for Btn in Buttons do
  begin
    Inc(CntBtns);
  end;
  if Length(DlgButtons) <> CntBtns then
    raise Exception.Create('DlgButtons and Buttons params should have the same count');
  //Get the Form created by CreateMessageDialog function
  F:= CreateMessageDialog(DlgText, DlgType, Buttons, DefultButton);
  // Set the Caption of th form
  F.Caption:= DlgCaption;
  // Change the buttons Captions
  for I := Low(DlgButtons) to High(DlgButtons) do
    TButton(F.Components[I+2]).Caption:= DlgButtons[I];
  // Check for bidimode
  if BidiMode = bdRightToLeft then
    begin
      TImage(F.Components[0]).Left:= (F.Width - (TImage(F.Components[0]).Left + TImage(F.Components[0]).Width));
      TLabel(F.Components[1]).Left:= (TImage(F.Components[0]).Left - (TLabel(F.Components[0]).Width + 10));
    end;
  // Return the form
  Result:= F;
end;

Call the function as

MyDialog('العنوان', 'النص', ['نعم', 'لا'], mtWarning, [mbYes, mbNo], bdRightToLeft, mbNo).ShowModal;

Enjoy.

Upvotes: 11

Related Questions