给你一段别人做的代码吧。{越来越多的程序使用了多国语言切换,虽然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; 就可以根据不同的语言给出不同的提示文字。 好了,整个工程就做完了,你可以运行测试一下,是不是切换迅速而且无闪烁。}
请教一下各位,对于resourcestring的国际化大家一般怎么做的?
如果你的文字不想被人修改,就不适用ini了
Acro Multi-Language Suite for Delphi Win32/.Net论坛: http://multilanguage.tech.topzj.com/对Resourcestring,它使用Hook的方式,让你还是按ResourceString定义,不改变你原来的写法。 字典提供文本和2进制格式,可以外带也可以直接嵌到exe中。
http://www.2ccc.com/article.asp?articleid=10
干净搞定delphi多语言-兼论设计模式
delphi,多语言,元件,设计模式,乱码,CodePage阅读全文>
參看 ReportMachine 的做法,呵呵
http://www.silentsea.net/candybox/index.php?project=langine
但是那种方法在切换语言时界面会出现闪烁,而且实现起来很麻烦,这里我介绍给大家的是利用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;
就可以根据不同的语言给出不同的提示文字。 好了,整个工程就做完了,你可以运行测试一下,是不是切换迅速而且无闪烁。}
如果你的文字不想被人修改,就不适用ini了
http://multilanguage.tech.topzj.com/对Resourcestring,它使用Hook的方式,让你还是按ResourceString定义,不改变你原来的写法。
字典提供文本和2进制格式,可以外带也可以直接嵌到exe中。
能有什么好办法呢?