在界面實時切換語言,代碼挺簡單的,一看就明白:
unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, DefaultTranslator,LCLTranslator,Translations ,LazUTF8 {$IFDEF linux} , Unix {$else} ,windows {$endif} ; type { TForm1 } TForm1 = class(TForm) Button1: TButton; ComboBox1: TComboBox; Label1: TLabel; Label2: TLabel; ToggleBox1: TToggleBox; procedure ComboBox1Change(Sender: TObject); procedure FormCreate(Sender: TObject); private public end; var Form1: TForm1; implementation {$R *.lfm} function GetOSLanguage: string; var l, fbl: string; {$IFDEF LCLCarbon} theLocaleRef: CFLocaleRef; locale: CFStringRef; buffer: StringPtr; bufferSize: CFIndex; encoding: CFStringEncoding; success: boolean; {$ENDIF} begin {$IFDEF LCLCarbon} theLocaleRef := CFLocaleCopyCurrent; locale := CFLocaleGetIdentifier(theLocaleRef); encoding := 0; bufferSize := 256; buffer := new(StringPtr); success := CFStringGetPascalString(locale, buffer, bufferSize, encoding); if success then l := string(buffer^) else l := ''; fbl := Copy(l, 1, 2); dispose(buffer); {$ELSE} {$IFDEF LINUX} fbl := Copy(GetEnvironmentVariable('LANG'), 1, 2); {$ELSE} lazGetLanguageIDs(l, fbl); {$ENDIF} {$ENDIF} Result := fbl; end; { TForm1 } procedure TForm1.ComboBox1Change(Sender: TObject); var lan:String; begin if ComboBox1.Text='en' then lan:='en' else lan:='zh_CN'; SetDefaultLang(lan,'',''); end; procedure TForm1.FormCreate(Sender: TObject); var lan:String; begin lan:=GetOSLanguage; //取當前系統(tǒng)語言 if lan='zh' then begin ComboBox1.ItemIndex:=ComboBox1.Items.IndexOf ('zh_CN(中文)'); lan:='zh_CN'; end else lan:='en'; SetDefaultLang(lan,'','');//切換為當前系統(tǒng)使用的語言 end; end.


浙公網(wǎng)安備 33010602011771號