如何从现有程序升级到支持多国语言的程序!
CSDN上有朋友问起如何从已经完成的项目升级到使用多国语言的程序,相信很多朋友也并非从项目的一开始就使用TLMPack的(也确实没有这个必要),下面我就简单的介绍一下怎么样将已有的项目升级为支持多国语言的项目。



代码:
procedure TForm1.Button1Click(Sender:TObject);
begin
MessageBox(Handle, PChar('Say hello to the user!'), 'Message', MB_ICONINFORMATION);
end;
那么你需要将文字的部分使用Translate函数包装起来,包装后就像这样:
代码:
procedure TForm1.Button1Click(Sender:TObject);
begin
MessageBox(Handle, PChar(tlmController1.Translate('SayHello', 'Say hello to the user!')), 'Message', MB_ICONINFORMATION);
end;
前面的‘SayHello’是对文字的标识,通过这个标识程序才能从语言文件中找到正确的资源,后面的文字作用是当语言文件中不存在需要寻找的资源(或者语言文件不存在时),该函数默认所返回的内容。
另外,Translate函数还支持同Format()函数一样的参数显示,如这样:
代码:
procedure TForm1.Button1Click(Sender:TObject);
begin
MessageBox(Handle, PChar(tlmController1.Translate('ComponentCount', 'ComponentCount = %d', [ComponentCount])), 'Message', MB_ICONINFORMATION);
end;

以下这个类是个小型的功能类。
代码:
interface
uses
....;
type
TFunctionClass = class(TObject)
public
procedure ShowMessage();
end;
impenmentation
procedure TFunctionClass.ShowMessage();
begin
ShowMessage(nil, 'Say hello to the user!', 'Message‘, MB_ICONINFORMATION);
end;
end.
使用TtlmObject类后,该类被改造为:
代码:
interface
uses
...., TLMObject;
type
TFunctionClass = class(TObject)
private
FtlmObject:TtlmObject;
public
constructor Create();
destructor Destroy();override;
procedure ShowMessage();
end;
impenmentation
constructor TFunctionClass.Create();
begin
inherited Create();
FtlmObject:=TtlmObject.Create(self);
end;
destructor TFunctionClass.Destroy();
begin
FtlmObject.Free;
inherited Destroy();
end;
procedure TFunctionClass.ShowMessage();
begin
MessageBox(Handle, PChar(tlmController1.Translate('ComponentCount', 'ComponentCount = %d', [ComponentCount])), 'Message', MB_ICONINFORMATION);
end;
end.
这样每当使用该功能类中需要文字显示的函数时,真正使用的文字就是语言文件中记载的内容了。

所谓的向导控件是指:TtlmIniGenerator和TtlmXmlGenerator这两个控件,它们会在程序运行时,将所有原本需要 TtlmController和TtlmObject读取的文字,反向输出到语言文件中,你所要做的就是在程序的主窗体上放置一个 TtlmIniGenerator(这里假设你要输出的是Ini格式的语言文件),然后运行程序,程序运行的过程中会首先出现一个设置对话框,询问生成语言文件的路径,以及字符集格式等,一般使用默认的设置直接OK就可以了,该提示对话框如下图:

以后每当程序中有需要转换文字的代码被调用到,TtlmIniGenerato都会出现一个提示对话框,提示你实际保存到文件中的实际文字内容是什么,一般情况下在这一步都不需要修改任何文字,直接让它输出就可以了。你所要做的就是保证程序的每一个需要文字显示的代码都被走到一遍,不然文字是不会被输出的,该对话框如下图所示:




TLMPack可以从这里下载: