Delphi XE2 FireMonkey entry (18) - TLang (multi language switching)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

A small TLang, multi language switch, good work it is.:

1,Through the first AddLang ('code') add language category, such as: AddLang('en'),AddLang('cn').

2,Each code corresponds to a TStrings list, acquisition methods such as: LangStr['en'],LangStr['cn'].

3,These data can be filled manually, through the LoadFromFile () method for loading before the SaveToFile () data (*.lng), the operation of the data may also be provided in the design of the interface.

4,When switching the Lang attribute can be modified, such as Lang := 'cn'.

5,Its scope is all forms and controls the current project, but does not include drawing control (such as TText), the text in the control and the form title.

Test:

1,First add TLang on the form;

2,Add three TRadioButton, is used to switch the language;

3,Add TButton, TCheckBox, TLabel for display;

4,The activation of RadioButton1, the default event Button1 and form.


unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.Objects;

type
  TForm1 = class(TForm)
    Lang1: TLang;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    Button1: TButton;
    CheckBox1: TCheckBox;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure RadioButton1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.FormCreate(Sender: TObject);
begin
  RadioButton1.Text := 'English';
  RadioButton2.Text := 'Simplified Chinese';
  RadioButton3.Text := 'Traditional Chinese';
  RadioButton1.Tag := 0;
  RadioButton2.Tag := 1;
  RadioButton3.Tag := 2;
  RadioButton2.OnChange := RadioButton1.OnChange;
  RadioButton3.OnChange := RadioButton1.OnChange;

  {The title should be related settings for TLang}
  Button1.Text := 'Button';
  CheckBox1.Text := 'CheckBox';
  Label1.Text := 'Test';
  Caption := 'Test';

  {Add language category}
  Lang1.AddLang('en');
  Lang1.AddLang('cn');
  Lang1.AddLang('big');

  {Original: the original TStrings not essential}
  with Lang1.Original do
  begin
    Add('Button');
    Add('CheckBox');
    Add('Test');
  end;

  {EN as the default can also be not provided}
  with Lang1.LangStr['en'] do
  begin
    Add('Button');
    Add('CheckBox');
    Add('Test');
  end;

  {Simplified Chinese}
  with Lang1.LangStr['cn'] do
  begin
    Values['Button'] := 'Button';
    Values['CheckBox'] := 'Check box';
    Values['Test'] := 'Test';
//    Values[Lang1.Original[0]] := 'Button';
//    Values[Lang1.Original[1]] := 'Check box';
//    Values[Lang1.Original[2]] := 'Test';
  end;

  {Traditional Chinese}
  with Lang1.LangStr['big'] do
  begin
    Values['Button'] := 'Button';
    Values['CheckBox'] := 'Check box';
    Values['Test'] := 'Test';
  end;
end;

{Switching}
procedure TForm1.RadioButton1Change(Sender: TObject);
begin
  case TRadioButton(Sender).Tag of
    0: Lang1.Lang := 'en';
    1: Lang1.Lang := 'cn';
    2: Lang1.Lang := 'big';
  end;
end;

{Linguistic data stored in the Resources property, which is a nested TStrings type}
procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to Lang1.Resources.Count - 1 do
    ShowMessage(TStrings(Lang1.Resources.Objects[i]).Text);
end;

end.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Shirley at November 22, 2013 - 5:16 PM