利用INI文件實現界面無閃爍多語言切換 |
|
flyup
資深會員 發表:280 回覆:508 積分:385 註冊:2002-04-15 發送簡訊給我 |
越來越多的程序使用了多國語言切換,雖然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;
就可以根據不同的語言給出不同的提示文字。 好了,整個工程就做完了,你可以運行測試一下,是不是切換迅速而且無閃爍 我的主頁上有完整的例子和代碼下載:http://mantousoft.51.net
附加檔案:23151_多國語言製作範例.zip
|
mustapha.wang
資深會員 發表:89 回覆:409 積分:274 註冊:2002-03-13 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |