利用INI檔實現介面無閃爍多語言切換 |
|
conundrum
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:893 回覆:1272 積分:643 註冊:2004-01-06 發送簡訊給我 |
http://www.cn-cio.org/show.php?article_id=2368&col=art Delphi 利用INI檔實現介面無閃爍多語言切換 提供者:jzj 發表日期:2003-12-22 ________________________________________ [Introduction] 對於軟體的多語言功能,作者描述了一種使用INI檔,並且無介面無閃爍感的解決方案 [Contant] 越來越多的程式使用了多國語言切換,雖然DELPHI自帶多語言包的添加和配置,但是那種方法在切換語言時介面會出現閃爍,而且實現起來很麻煩,這裏我介紹給大家的是利用INI檔來讀取介面的語種文字,用這種方法,不但簡單易行,而且在切換的時候不會出現介面的閃爍。 我們從一個例子出發,看看怎麼實現語言的切換。首先建立一個新工程。放置如下組件:(如圖) MainMenu1: TMainMenu; File1: TMenuItem; Exit1: TMenuItem; Label1: TLabel; Button1: TButton; CheckBox1: TCheckBox; CheckBox2: TCheckBox; Button2: TButton; Label2: TLabel; ComboBox1: TComboBox; Label3: TLabel; 由於要讀取Ini檔,所以在USES中加入聲明IniFiles; 然後將Button1和Button2的ShowHint屬性設置為True; 其中我們用ComboBox1來顯示可供選擇的語言和用來選擇語言。 我們在程式的目錄下編輯如下的Chinese GB.Ini文件: ;/////////////////////////////////////////////////////////////////// ; ; 翻譯的一些規則: ; 翻譯前,拷貝 Chinese GB.ini 改名到 yourlanguage.ini ; 僅僅翻譯符號'='後的文字 ; [Translations] ; Label1.Caption =文字1 Label2.Caption =文字2 Label3.Caption =語言 Button1.Caption =按鈕1 Button2.Caption =按鈕2 Button1.Hint =按鈕1_提示 Button2.Hint =按鈕2_提示 CheckBox1.Caption =核取方塊1 CheckBox2.Caption =核取方塊2 File1.Caption =文件 Exit1.Caption =退出 ; [Messages] ; M1 =信息框測試 ; ;////////////////////////////////////////////////////////////////// 同樣的方法編輯一個名為English.ini的檔,將“=”左邊的文字改為英文。 例如:Label1.Caption =Label1 程式運行時,我們查找當前目錄下所有的語言配置檔(*.ini),為了達到這個目的,我編寫了如下的函數搜索目錄下所有的語言配置檔的檔案名,然後將檔案名去掉ini副檔名保存返回: function TForm1.SearchLanguagePack:TStrings; var ResultStrings:TStrings; DosError:integer; SearchRec:TsearchRec; begin ResultStrings:=TStringList.Create; DosError:=FindFirst(ExtractFilePath(ParamStr(0))+'*.ini', faAnyFile, SearchRec); while DosError=0 do begin { 返回的檔案名並去掉末尾的.ini字元 } ResultStrings.Add(ChangeFileExt(SearchRec.Name,'')); DosError:=FindNext(SearchRec); end; FindClose(SearchRec); Result:=ResultStrings; end; 在Form建立的事件中添加代碼,將目錄下所有的語言檔案名加入選擇列表框中。 procedure TForm1.FormCreate(Sender: TObject); begin ComboBox1.Items.AddStrings(SearchLanguagePack); end; 程式的重點在如何切換語言,在ComboBox1的OnChange事件中進行切換操作。這裏我寫了SetActiveLanguage過程用於實現這一操作。 procedure TForm1.ComboBox1Change(Sender: TObject); begin SetActiveLanguage(ComboBox1.Text); end; 其中SetActiveLanguage代碼如下: procedure TForm1.SetActiveLanguage(LanguageName:string); const Translations='Translations'; Messages='Messages'; var frmComponent:TComponent; i:Integer; begin with TInifile.Create(ExtractFilePath(ParamStr(0))+LanguageName+'.ini') do begin for i:=0 to ComponentCount-1 do { 遍曆Form組件 } begin frmComponent:=Components[i]; if frmComponent is TLabel then { 如果元件為TLabel型則當作TLabel處理,以下同 } begin (frmComponent as TLabel).Caption:= ReadString(Translations,frmComponent.Name+'.Caption',(frmComponent as TLabel).Caption); end; if frmComponent is TCheckBox then begin (frmComponent as TCheckBox).Caption:= ReadString(Translations,frmComponent.Name+'.Caption',(frmComponent as TCheckBox).Caption); end; if frmComponent is TButton then begin (frmComponent as TButton).Caption:= ReadString(Translations,frmComponent.Name+'.Caption',(frmComponent as TButton).Caption); (frmComponent as TButton).Hint:= ReadString(Translations,frmComponent.Name+'.Hint',(frmComponent as TButton).Hint); end; if frmComponent is TMenuItem then begin (frmComponent as TMenuItem).Caption:= ReadString(Translations,frmComponent.Name+'.Caption',(frmComponent as TMenuItem).Caption); end; end; M1:=ReadString(Messages,'M1',M1); end; end; 在這個過程中,我們遍曆了Form中的所有元件,根據他們的類別和元件名動態的從ini配置檔中讀出應該顯示的語言文字。用遍曆元件的方法比一個一個寫出具體的元件維護起來要方便很多,代碼的適應性也更強。其中M1為一個字串變數,這樣提示消息也能切換,比如在Button1的Click事件中 procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(M1); end; 就可以根據不同的語言給出不同的提示文字 好了,整個工程就做完了,你可以運行測試一下,是不是切換迅速而且無閃爍 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |