在界面实时切换语言,代码挺简单的,一看就明白:
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; //取当前系统语言 if lan='zh' then begin ComboBox1.ItemIndex:=ComboBox1.Items.IndexOf ('zh_CN(中文)'); lan:='zh_CN'; end else lan:='en'; SetDefaultLang(lan,'','');//切换为当前系统使用的语言 end; end.
 
 
                    
                     
                    
                 
                    
                 

 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号