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

{  For example :
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  SetActiveLanguage(ComboBox1.Text);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.Items.AddStrings(SearchLanguagePack);
end;
}

unit uLanguage;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Menus, IniFiles, ComCtrls
  ,AdvGlowButton, AdvNavBar, AdvOfficePager
  ;
//搜索ini文件生成语言列表
function SearchLanguagePack: TStrings;
//根据当前选择的语言设置指定窗体及其子控件的属性
procedure SetActiveLanguage(owner: TForm; const LanguageName: string);
//将指定窗体及其子控件的属性写入INI文件
procedure Writeproperty(owner: TForm; const LanguageName: string);

implementation

function SearchLanguagePack:TStrings;
var
  ResultStrings:TStrings;
  DosError:integer;
  SearchRec:TsearchRec;
begin
  ResultStrings:=TStringList.Create;
  DosError:=FindFirst(ExtractFilePath(Application.ExeName)+'language/'+'*.ini', faAnyFile, SearchRec);
  while DosError=0 do
  begin
    { 返回的文件名并去掉末尾的.ini字符 }
    ResultStrings.Add(ChangeFileExt(SearchRec.Name,''));
    DosError:=FindNext(SearchRec);
  end;
  FindClose(SearchRec);
  Result:=ResultStrings;
end;

procedure Writeproperty(owner: TForm; const LanguageName: string);
var
  c:TComponent;
  i:Integer;
begin
  if owner = nil then exit;
  with TInifile.Create(ExtractFilePath(Application.ExeName)+'language/'+LanguageName+'.ini') do
  begin
    WriteString(owner.Name, owner.Name+'.Caption', owner.Caption);
    for i:=0 to owner.ComponentCount-1 do
    begin
      c:=owner.Components[i];
      if c is TLabel then
      begin
        writeString(owner.Name, c.Name+'.Caption', (c as TLabel).Caption);
      end else
      if c is TCheckBox then
      begin
        writeString(owner.Name,c.Name+'.Caption',(c as TCheckBox).Caption);
      end else
      if c is TButton then
      begin
        writeString(owner.Name,c.Name+'.Caption',(c as TButton).Caption);
        writeString(owner.Name,c.Name+'.Hint',(c as TButton).Hint);
      end else
      if c is TMenuItem then
      begin
        writeString(owner.Name,c.Name+'.Caption',(c as TMenuItem).Caption);
      end else
      if c is TToolButton then
      begin
        writeString(owner.Name,c.Name+'.Caption',(c as TToolButton).Caption);
        writeString(owner.Name,c.Name+'.Hint',(c as TToolButton).Hint);
      end else
      if c is TAdvGlowButton then
      begin
        writeString(owner.Name,c.Name+'.Caption',(c as TAdvGlowButton).Caption);
        writeString(owner.Name,c.Name+'.Hint',(c as TAdvGlowButton).Hint);
      end else
      if c is TAdvOfficePage then
      begin
        writeString(owner.Name,c.Name+'.Caption',(c as TAdvOfficePage).Caption);
        writeString(owner.Name,c.Name+'.Hint',(c as TAdvOfficePage).Hint);
      end else
      if c is TAdvNavBarPanel then
      begin
        writeString(owner.Name,c.Name+'.Caption',(c as TAdvNavBarPanel).Caption);
      end;
    end;
    //处理字符串变量
  //  M1:=ReadString(Messages,'M1',M1);
  end;
end;

procedure SetActiveLanguage(owner: TForm; const LanguageName:string);
const
  Messages='Messages';    //字符串变量小节
var
  c:TComponent;
  i:Integer;
begin
  if owner = nil then exit;
  with TInifile.Create(ExtractFilePath(Application.ExeName)+'language/'+LanguageName+'.ini') do
  begin
     owner.Caption := readstring(owner.Name, owner.Name+'.Caption', owner.Caption);
    for i:=0 to owner.ComponentCount-1 do
    begin
      c:=owner.Components[i];
      if c is TLabel then
      begin
        (c as TLabel).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TLabel).Caption);
      end else
      if c is TCheckBox then
      begin
        (c as TCheckBox).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TCheckBox).Caption);
      end else
      if c is TButton then
      begin
        (c as TButton).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TButton).Caption);
        (c as TButton).Hint:=ReadString(owner.Name,c.Name+'.Hint',(c as TButton).Hint);
      end else
      if c is TMenuItem then
      begin
        (c as TMenuItem).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TMenuItem).Caption);
      end else
      if c is TToolButton then
      begin
        (c as TToolButton).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TToolButton).Caption);
        (c as TToolButton).Hint:=ReadString(owner.Name,c.Name+'.Hint',(c as TToolButton).Hint);
      end else
      if c is TAdvGlowButton then
      begin
        (c as TAdvGlowButton).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TAdvGlowButton).Caption);
        (c as TAdvGlowButton).Hint:=ReadString(owner.Name,c.Name+'.Hint',(c as TAdvGlowButton).Hint);
      end else
      if c is TAdvOfficePage then
      begin
        (c as TAdvOfficePage).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TAdvOfficePage).Caption);
        (c as TAdvOfficePage).Hint:=ReadString(owner.Name,c.Name+'.Hint',(c as TAdvOfficePage).Hint);
      end else
      if c is TAdvNavBarPanel then
      begin
        (c as TAdvNavBarPanel).Caption:=ReadString(owner.Name,c.Name+'.Caption',(c as TAdvNavBarPanel).Caption);
      end;
    end;
    //处理字符串变量
  //  M1:=ReadString(Messages,'M1',M1);
  end;
end;

end.

posted @ 2011-01-02 21:14  delphi中间件  阅读(351)  评论(0编辑  收藏  举报