传说中的Delphi做多国语言有很多种,
个人我只会这一种, 小弟的初学浅薄怕忘记。所以也就记下来了。
假设有语言文件 Multilingual.ini 内容如下:
[language]
current=4
[中文]
Name=名字
Time=时间
Language=语言
China=中国
[繁體]
Name=名字
Time=時間
Language=語言
China=中國
[English]
Name=Name
Time=Time
Language=Language
China=China
[Japanese]
Name=の名前
Time=タイム
Language=言語
China=中国の
[Russian]
Name=имя
Time=время
Language=язык
China=Китай
然后只要读出用英文对应的语言即可. Delphi源码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, Grids, DBGrids, DBClient;
type
TForm1 = class(TForm)
ComboBox1: TComboBox;
Label1: TLabel;
procedure FormShow(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
ADOQuery: TADOQuery;
ADOConnection:TADOConnection;
end;
var
Form1: TForm1;
implementation
uses IniFiles;//读写ini要加入的
//这是我写的一个读写ini 文件的子函数 只要调用就行了
//_Ini:=TIniFIle.Create(extractFilePath(application.exeName)+'*.ini');//关联ini要用绝对路径
function Multilingual(RW:Boolean; PathIni:string; titl:string; keywords:string; price:string ;method:string):string;//多国语言
{ INI文件的结构 (true读~~~~~ 绝对路径~~~~~~~ 标题~~~~~~~ 关健字~~~~~~~~~~ 值~~~~~~~~~ 方法~~~~~~~~ )返回内容
[小节名]
关键字1=值1
关键子2=值2
}
Var
_ini:Tinifile;
Str:string;
Tstrs:TStrings;
begin
if not FileExists(PathIni) then
begin
ShowMessage(PathIni+'文件不存在');
application.Terminate;//退出
end
else
begin
//RenameFile('Multilingual.res','Multilingual.ini');//原名->新名
//_Ini:=TIniFIle.Create(extractFilePath(application.exeName)+'Multilingual.ini');
_Ini:=TIniFIle.Create(PathIni);
if RW then
begin//读读读读读读读读读读读读读读读读读读读读读读读读读读读读读读读读
if method = 'Readstring' then
begin
Str:=_Ini.Readstring(titl,keywords,price);//得到指定小节名,关键字的指定值 最后一个为缺省值
end
else
begin
Tstrs:=tstringlist.create;
if method = 'ReadSections' then
begin
_ini.ReadSections(Tstrs);//得到所有小节名
end
else if method = 'readsection' then
begin
_ini.readsection(titl,Tstrs);//得到指定小节中的所有关键字名
end
else if method = 'ReadSectionValues' then
begin
_ini.ReadSectionValues(titl,Tstrs);//指定小节的所有行(包括关键字、=、值)
end;
Str:=Tstrs.Text;
Tstrs.Free;//释放
end;
end
else //写写写写写写写写写写写写写写写写写写写写写写写写写写写写写写写写写写
begin
if method='writestring' then
begin
_Ini.writestring(titl,keywords,price);
end
else
if method='writeinteger' then
begin
// _Ini.writeinteger(titl,keywords,变量或者整数数值);
end
else
if method='writenbool' then
begin
// _Ini.writenbool(titl,keywords,变量或者True/False);
end
else
if method='DeleteKey' then
begin
_Ini.DeleteKey(titl,keywords);//删除关键字
end
else
if method='EraseSection' then
begin
_Ini.EraseSection(titl);//删除小节
end;
{else
if method='UpdateFile' then
begin
_Ini.UpdateFile;//保存到文件
end;//添加小节与关键字或修改值, 直接写入即可
}
end;
Result:=Str;
_Ini.Free;//释放
//RenameFile('Multilingual.ini','Multilingual.res');//原名->新名
end;
end;
{$R *.dfm}
procedure TForm1.FormShow(Sender: TObject); //在开始时读入所对应的语言
var
paths:string;
LanguageStr:string;
i:Integer;
begin
paths:=extractFilePath(application.exeName)+'Multilingual.ini';
LanguageStr:=Multilingual(True,paths,'language','current','字符值缺省','ReadSections');
for i:=1 to Length('language') do //删单词
LanguageStr[i]:=Char(' ');
ComboBox1.Items.Text:=Trim(LanguageStr);//得到全部语言
ComboBox1.ItemIndex:=StrToInt(Multilingual(True,paths,'language','current','字符值缺省','Readstring'));//得到当前语言
ComboBox1Change(Self);
end;
procedure TForm1.ComboBox1Change(Sender: TObject);//通过 ComboBox1 控件可以选择加入语言
var
paths:string;
begin
paths:=extractFilePath(application.exeName)+'Multilingual.ini';//路径
Label1.Caption:=Multilingual(True,paths,ComboBox1.Text,'Language','字符值缺省','Readstring');//得到所要的语言
Multilingual(False,paths,'language','current', IntToStr(ComboBox1.ItemIndex),'writestring');//修改当前语言
end;
end.
个人认为这样做的好处可以让别人添加他们自己的语言,还可以加入我们中国不同的方言
源码数下载地址
http://download.csdn.net/detail/key_ok/3769591
用读写ini文件来加入多国语言,方便,没有抖动,没有闪烁,简单