• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

delphi从INI文件载入语言包

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

下面是一个通用过程,根据INI文件中的资源改变界面语言(仅Caption和Hint属性)

参数一:为一Component,一般为TForm,此过程将循环列举其子Component并查找是否具有相关属性,
有的话则从INI文件读取信息并设置属性
参数二:为INI文件全名

//引用TypInfo,IniFiles/TntInifiles单元
//声明的资源,默认的,防止变量出现空值
resourcestring
S_Menu            = 'Menu';
S_AppName         = 'Window View Tool';
S_Null            = '(Null)';
S_About           = '&About...';
S_Warning         = 'Warning';
S_Confirm         = 'Confirm';
S_Error           = 'Error';
S_Information     = 'Information';
//声明的常量
const
SCaption          = 'Caption';
SHint             = 'Hint';

SIniAppName       = 'Window View Tools';
SIniLanguage      = 'Language';
SLanguageName     = 'LanguageName';

{-----------------------------------------------------------------------------
Procedure: LoadLanguage
Author:      ysai
Date:        2003-7-20
Arguments: AComponent:TComponent;AFileName:string=''
-----------------------------------------------------------------------------}
procedure LoadLanguage(
     const AComponent    : TComponent;
     const AFileName     : widestring    = ''
     );
//设置语言

function PropertyExists(
       const AObject     : TObject;
       const APropName : wideString
       ):Boolean;
//判断一个属性是否存在
var
     PropInfo:PPropInfo;
begin
     PropInfo:=GetPropInfo(AObject.ClassInfo,APropName);
     Result:=Assigned(PropInfo);
end;

function SetStringPropertyIfExists(
       const AObject     : TObject;
       const APropName : widestring;
       const AValue      : widestring
       ):Boolean;
//给字符串属性赋值,如果存在
var
     PropInfo:PPropInfo;
begin
     PropInfo:=GetPropInfo(AObject.ClassInfo,APropName);
     if Assigned(PropInfo) and
         (PropInfo.PropType^.Kind in [tkString, tkLString, tkWString]) then
     begin
       SetWideStrProp(AObject,PropInfo,AValue);
       Result:=True;
     end else
       Result:=False;
end;

function GetStringProperty(
       const AObject     : TObject;
       const APropName : widestring
       ):wideString;
//取得字符串属性值
var
     PropInfo:PPropInfo;
begin
     Result    :=    '';
     PropInfo:=GetPropInfo(AObject.ClassInfo,APropName);
     if Assigned(PropInfo) and
         (PropInfo^.PropType^.Kind in [tkString, tkLString, tkWString]) then
       Result    :=    GetWideStrProp(AObject,PropInfo);
end;

function SetIntegerPropertyIfExists(
       const AObject     : TObject;
       const APropName : widestring;
       const AValue      : Longint
       ):Boolean;
//给整型属性赋值,如果存在
var
     PropInfo:PPropInfo;
begin
     PropInfo:=GetPropInfo(AObject.ClassInfo,APropName);
     if Assigned(PropInfo) and
         (PropInfo^.PropType^.Kind = tkInteger) then
     begin
       SetOrdProp(AObject,PropInfo,AValue);
       Result:=True;
     end else
       Result:=False;
end;

function GetObjectProperty(
       const AObject     : TObject;
       const APropName : widestring
       ):TObject;
var
     PropInfo:PPropInfo;
begin
     Result    :=    nil;
     PropInfo:=GetPropInfo(AObject.ClassInfo,APropName);
     if Assigned(PropInfo) and
         (PropInfo^.PropType^.Kind = tkClass) then
       Result    :=    GetObjectProp(AObject,PropInfo);
end;

var
i           : Integer;
s           : widestring;
FsLanguage,sFileName : widestring;
SAppName: widestring;
SMenu : widestring;
SNull : widestring;
SAbout: widestring;
SWarning : widestring;
SConfirm : widestring;
SError : widestring;
SInformation: widestring;
begin
if AFileName    = '' then
     sFileName :=FsLanguage    //FsLanguage为默认语言文件名
else
     sFileName :=AFileName;
if not FileExists(sFileName) then Exit;
with TTntIniFile.Create(sFileName) do
try
     //下面为读取变量
     SAppName          :=    ReadString(SIniLanguage,'AppName',S_AppName);
     SMenu             :=    ReadString(SIniLanguage,'Menu',S_Menu);
     SNull             :=    ReadString(SIniLanguage,'Null',S_Null);
     SAbout            :=    ReadString(SIniLanguage,'About',S_About);
     SWarning          :=    ReadString(SIniLanguage,'Warning',S_Warning);
     SConfirm          :=    ReadString(SIniLanguage,'Confirm',S_Confirm);
     SError            :=    ReadString(SIniLanguage,'Error',S_Error);
     SInformation      :=    ReadString(SIniLanguage,'Information',S_Information);

     //设置组件Caption
     if PropertyExists(AComponent,SCaption) then
     begin
       s :=    ReadString(SIniLanguage,AComponent.Name+'.'+SCaption,'');
       if s<>'' then
         SetStringPropertyIfExists(AComponent,SCaption,s);
     end;
     //读取字体
     if PropertyExists(AComponent,'Font') then
     begin
       s :=    ReadString(SIniLanguage,'FontName','');
       i :=    ReadInteger(SIniLanguage,'FontSize',0);
       if s<>'' then
         SetStringPropertyIfExists(
             GetObjectProperty(AComponent,'Font'),'Name',s);
       if i > 0 then
         SetIntegerPropertyIfExists(
             GetObjectProperty(AComponent,'Font'),'Size',i);
     end;

     //列举所有子组件并设置属性
     for i :=    0 to AComponent.ComponentCount - 1 do
     begin
       //设置Caption
       if PropertyExists(AComponent.Components[i],SCaption) then
       begin
         s :=    ReadString(SIniLanguage,AComponent.Name+'.'+
             AComponent.Components[i].Name+'.'+SCaption,'');
         if s<>'' then
           SetStringPropertyIfExists(AComponent.Components[i],SCaption,s);
       end;
       //设置Hint
       if PropertyExists(AComponent.Components[i],SHint) then
       begin
         s :=    ReadString(SIniLanguage,AComponent.Name+'.'+
             AComponent.Components[i].Name+'.'+SHint,'');
         if s = '' then //如果Hint为空,则改Hint同Caption
           s := GetStringProperty(AComponent.Components[i],SCaption);
         if s<>'' then
           SetStringPropertyIfExists(AComponent.Components[i],SHint,s);
       end;
     end;
     //更改默认语言文件为当前文件
     FsLanguage    :=    sFileName;
finally
     Free;
end;
end;

procedure TFrmWindowView.FormShow(Sender: TObject);
begin   //加载语言文件
if FileExists(ExtractFilePath(Application.exename)+'\Chinese.ini') then
     LoadLanguage(FrmWindowView,ExtractFilePath(Application.exename)+'\Chinese.ini');
end;

 

//示例INI文件Chinese.ini 用记事本编辑ini,保存为unicode编码格式 窗口及所有控件用TntWare2.3,以方便显示Unicode字符.


[Language]
LanguageName=中文

AppName=窗口查看器
Menu=菜单
Null=(空)
About=关于(&A)...
Warning=警告
Confirm=确认
Error=错误
Information=信息

FontName=宋体
FontSize=9

FrmWindowView.Caption=窗口查看器
FrmWindowView.tbFile.Caption=文件(&F)
FrmWindowView.tbFile.Hint=文件菜单
FrmWindowView.tbOption.Caption=选项(&O)
FrmWindowView.tbOption.Hint=选项菜单
FrmWindowView.tbTools.Caption=工具(&T)
FrmWindowView.tbTools.Hint=工具菜单
FrmWindowView.tbHelp.Caption=帮助(&H)
FrmWindowView.tbHelp.Hint=帮助菜单
FrmWindowView.panSearch.Caption=搜索文本:
FrmWindowView.panSearch.Hint=搜索文本
FrmWindowView.edtSearch.Hint=在此键入搜索文本
FrmWindowView.btnSearch.Caption=搜索
FrmWindowView.btnSearch.Hint=开始搜索
FrmWindowView.tv.Hint=捕获结果,按鼠标右键弹出动作菜单

FrmWindowView.acExit.Caption=退出(&X)
FrmWindowView.acExit.Hint=退出程序
FrmWindowView.acLockCapture.Caption=锁定捕获
FrmWindowView.acLockCapture.Hint=选定后将鼠标移动到目标对象上
FrmWindowView.acCapture.Caption=捕获
FrmWindowView.acCapture.Hint=按住鼠标然后拖曳到目标对象上
FrmWindowView.acCaptureAll.Caption=捕获所有
FrmWindowView.acCaptureAll.Hint=捕获所有可列举出的窗口
FrmWindowView.acAutoHide.Caption=自动隐藏
FrmWindowView.acAutoHide.Hint=在捕获时自动隐藏程序
FrmWindowView.acStayOnTop.Caption=总在最前
FrmWindowView.acStayOnTop.Caption=窗口总在最前
FrmWindowView.acLanguage.Caption=语言(&L)
FrmWindowView.acLanguage.Hint=选择语言
FrmWindowView.acProcessManage.Caption=进程管理
FrmWindowView.acProcessManage.Hint=打开进程管理工具
FrmWindowView.acAbout.Caption=关于(&A)...
FrmWindowView.acAbout.Hint=显示关于窗口

FrmWindowView.acCopy.Caption=复制(&C)
FrmWindowView.acCopy.Hint=复制所选节点内容
FrmWindowView.acEnable.Caption=使用(&E)
FrmWindowView.acEnable.Hint=使选中的对象可用
FrmWindowView.acDisable.Caption=禁用(&D)
FrmWindowView.acDisable.Hint=使选中的对象不可用
FrmWindowView.acShow.Caption=显示(&S)
FrmWindowView.acShow.Hint=显示选中的对象
FrmWindowView.acHide.Caption=隐藏(&H)
FrmWindowView.acHide.Hint=隐藏选中的对象
FrmWindowView.acDelete.Caption=删除(&L)
FrmWindowView.acDelete.Hint=删除选中的对象(不能恢复)
FrmWindowView.acExport.Caption=导出(&X)...
FrmWindowView.acExport.Hint=导出选中的节点及子节点树到文件

FrmProcessManage.Caption=进程管理
FrmProcessManage.labProcessList.Caption=进程列表(&L)
FrmProcessManage.labProcessList.Hint=当前进程列表
FrmProcessManage.lbProcessList.Hint=当前进程列表
FrmProcessManage.btnRefresh.Caption=刷新(&R)
FrmProcessManage.btnRefresh.Hint=刷新进程列表
FrmProcessManage.btnEndProcess.Caption=结束进程(&C)
FrmProcessManage.btnEndProcess.Hint=结束选择的多个进程(小心使用)
FrmProcessManage.btnClose.Caption=关闭(&X)
FrmProcessManage.btnClose.Hint=关闭窗口


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap