Creating Multilingual Applications in Delphi: A Guide to Using TMultiLang and TMultiResBitmap Components

Close-up view of computer keyboard with national flags of world countries on keys and translate button

Using multilanguage in Delphi without 3rd party plugins is actually quite simple. Here are the steps you can follow:

Step 1: Create a resource string file

Create a new text file and save it with a .res extension. This file will contain all the strings that you want to translate. Here’s an example:

STRINGTABLE
{
    101, "Hello, World!"
    102, "Goodbye, World!"
}

In this example, we have two strings: “Hello, World!” and “Goodbye, World!”. The numbers before each string (101 and 102) are the resource IDs. These IDs will be used later in the Delphi code to access the strings.

Step 2: Add the resource file to your project

In Delphi, go to Project > Add to Project > Resource file and select the .res file you just created.

Step 3: Add the TMultiLang component to your form

In the Delphi IDE, go to the Component Palette and find the TMultiLang component. Drag and drop it onto your form.

Step 4: Set the resource file and default language

Select the TMultiLang component on your form and go to the Object Inspector. Set the ResourceFile property to the name of your .res file (without the .res extension). For example, if your file is called “strings.res”, set ResourceFile to “strings”.

Next, set the DefaultLanguage property to the language you want to use as the default. For example, if you want to use English as the default language, set DefaultLanguage to “en”.

Step 5: Add the strings to your form

Select the TMultiLang component on your form and go to the Object Inspector. Click on the Strings property and add the resource IDs and their corresponding translations. For example:

101=Hello, World!
102=Goodbye, World!

Step 6: Access the strings in the code

To access the translated strings in code, use the TMultiLang.GetText method. For example:

Label1.Caption := MultiLang1.GetText(101);

This code will set the caption of Label1 to the translated string with the resource ID 101.

Here’s the full code for a simple Delphi form that uses multilanguage:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, MultiLang;

type
  TForm1 = class(TForm)
    MultiLang1: TMultiLang;
    Label1: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := MultiLang1.GetText(101);
end;

end.

The .dfm file for this form should contain the TMultiLang component, and the Strings property should be set to the translations you want to use.

To use multilanguage pictures in Delphi using TMultiResBitmap, you can follow these steps:

Step 1: Create a resource file for each image

Create a separate resource file for each image you want to use. Each file should contain the same image in different sizes. For example, if you have an image called “logo.png”, you can create three resource files called “logo16.res”, “logo32.res”, and “logo64.res”. Each file should contain the same image, but resized to 16×16, 32×32, and 64×64 pixels respectively.

Step 2: Add the resource files to your project

In Delphi, go to Project > Add to Project > Resource file and select each of the .res files you created in Step 1.

Step 3: Add a TMultiResBitmap component to your form

In the Delphi IDE, go to the Component Palette and find the TMultiResBitmap component. Drag and drop it onto your form.

Step 4: Set the resource files for the TMultiResBitmap component

Select the TMultiResBitmap component on your form and go to the Object Inspector. Click on the Bitmaps property and add a new bitmap for each of the resource files you created in Step 1. For example:

Bitmaps = <
  item
    Name = 'Logo16'
    ResourceName = 'logo16'
  end
  item
    Name = 'Logo32'
    ResourceName = 'logo32'
  end
  item
    Name = 'Logo64'
    ResourceName = 'logo64'
  end>

Step 5: Add the images to your form

Select the TImage component on your form and go to the Object Inspector. Set the MultiResBitmap property to the TMultiResBitmap component you added in Step 3. Then, set the ImageIndex property to the index of the image you want to use. For example, to use the 32×32 version of the logo, set ImageIndex to 1 (since the first image has an index of 0).

Step 6: Access the images in code

To access the images in code, use the TMultiResBitmap.GetBitmap method. For example:

Image1.Picture.Bitmap := MultiResBitmap1.GetBitmap('Logo32');

This code will set the picture of Image1 to the 32×32 version of the logo.

Here’s the updated code for the Delphi form that uses multilanguage pictures with TMultiLang and TMultiResBitmap:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, MultiLang, MultiResBitmap, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    MultiLang1: TMultiLang;
    MultiResBitmap1: TMultiResBitmap;
    Image1: TImage;
    Label1: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := MultiLang1.GetText(101);
  Image1.Picture.Bitmap := MultiResBitmap1.GetBitmap('Logo32');
end;

end.

Happy Coding!!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *