利用INI文件实现界面无闪烁多语言切换[1]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 mantousoft 的 blog

文档图片
文档内容
越来越多的程序使用了多国语言切换,虽然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;
就可以根据不同的语言给出不同的提示文字。

好了,整个工程就做完了,你可以运行测试一下,是不是切换迅速而且无闪烁。

  作者:万重(www.delphibox.com)  2000.6.9

本文关键:INI 界面 无闪烁 多语言 delphi
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top