惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
IT之家
IT之家
S
SegmentFault 最新的问题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Threatpost
Cyberwarzone
Cyberwarzone
AWS News Blog
AWS News Blog
博客园 - 司徒正美
C
Cyber Attacks, Cyber Crime and Cyber Security
Latest news
Latest news
S
Secure Thoughts
S
Schneier on Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Cisco Blogs
Attack and Defense Labs
Attack and Defense Labs
S
Security Affairs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hacker News: Ask HN
Hacker News: Ask HN
大猫的无限游戏
大猫的无限游戏
博客园_首页
S
Security @ Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
A
Arctic Wolf
AI
AI
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
Project Zero
Project Zero
P
Proofpoint News Feed
T
Tor Project blog
P
Privacy International News Feed
阮一峰的网络日志
阮一峰的网络日志
博客园 - 聂微东
月光博客
月光博客
Forbes - Security
Forbes - Security
量子位
I
Intezer
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Y
Y Combinator Blog
P
Proofpoint News Feed
T
The Exploit Database - CXSecurity.com
腾讯CDC
N
Netflix TechBlog - Medium
www.infosecurity-magazine.com
www.infosecurity-magazine.com
L
Lohrmann on Cybersecurity
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - chance

SQLServer 2008的数据库镜像实施笔记 报表服务器无法打印 人生值得学习的81句话 人件之名言警句 将 BLOB 值写入 SQL Server 时保留资源 Reporting Service 在打印或预览时可能会导致出现空白页的原因 asp.net中如何打印ReportViewer报表 - chance - 博客园 [翻译]使用ASP.NET2.0的ReportViewer查看RDLC报表 基于消息与.Net Remoting的分布式处理架构 C#程序实现动态调用DLL的研究 javascript事件列表解说 js 操作IE游览器 window.external... 微型项目实践感悟 DELPHI:利用INI文件实现界面无闪烁多语言切换 SQLServer2000的数据库容量是多大? MessageBox对话框 项目经理是这样当的 SQL Server 存储过程的分页方案比拼 动态调用Webservice
Delphi - 利用INI文件实现界面多语言切换
chance · 2006-09-10 · via 博客园 - chance
越来越多的程序使用多国语言切换,虽然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;
就可以根据不同的语言给出不同的提示文字。