在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一个小小的 TLang 类, 实现多语言切换, 挺好的. 它的工作思路是:
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 := '简体中文'; RadioButton3.Text := '繁体中文'; RadioButton1.Tag := 0; RadioButton2.Tag := 1; RadioButton3.Tag := 2; RadioButton2.OnChange := RadioButton1.OnChange; RadioButton3.OnChange := RadioButton1.OnChange; {这些标题应对应着 TLang 的相关设置} Button1.Text := 'Button'; CheckBox1.Text := 'CheckBox'; Label1.Text := 'Test'; Caption := 'Test'; {添加语言类别} Lang1.AddLang('en'); Lang1.AddLang('cn'); Lang1.AddLang('big'); {Original: 这个原始的 TStrings 可有可无} with Lang1.Original do begin Add('Button'); Add('CheckBox'); Add('Test'); end; {en 作为默认也可以不设置} with Lang1.LangStr['en'] do begin Add('Button'); Add('CheckBox'); Add('Test'); end; {简体中文} with Lang1.LangStr['cn'] do begin Values['Button'] := '按钮'; Values['CheckBox'] := '复选框'; Values['Test'] := '测试'; // Values[Lang1.Original[0]] := '按钮'; // Values[Lang1.Original[1]] := '复选框'; // Values[Lang1.Original[2]] := '测试'; end; {繁体中文} with Lang1.LangStr['big'] do begin Values['Button'] := '按鈕'; Values['CheckBox'] := '復選框'; Values['Test'] := '測試'; end; end; {切换} 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; {语言数据保存在 Resources 属性中, 它是嵌套的 TStrings 类型} 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. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论