📄 multilang.pas
字号:
unit MultiLang;
// multi language support
interface
uses Classes, SysUtils
{$IFDEF CLX}
, QGraphics
{$ELSE}
, Graphics
{$ENDIF}
, MultiDataFile{, dialogs};
type
TMultiLangSupport = class
protected
SourceFile: TMultiDataFile;
Names,
Contents: TStrings;
Images: TStrings;
ImgSegs: TStrings;
public
constructor Create;
destructor Destroy; override;
function Initialize(strseg: Integer; intimgseg: Integer; locimgseg: Integer): Boolean;
procedure SetFileName(FN: String);
function GetCodeString(u: String; id: Integer): String;
procedure GetCodeStringList(u: String; id: Integer; sl: TStrings);
function GetComponentString(id: String): String;
procedure GetComponentStringList(id: String; sl: TStrings);
procedure GetComponentImage(id: String; img: TGraphic);
end;
procedure InitMLSupport(defFile: String; defstrSeg: Integer; defintSeg: Integer; deflocSeg: Integer);
procedure DestroyMLSupport;
var
ml: TMultiLangSupport;
implementation
procedure InitMLSupport(defFile: String; defstrSeg: Integer; defintSeg: Integer; deflocSeg: Integer);
begin
ml:=TMultiLangSupport.Create;
ml.SetFileName(defFile);
ml.Initialize(defstrSeg,defintSeg,deflocSeg);
end;
procedure DestroyMLSupport;
begin
ml.Free;
end;
///////////////////////////////////////////////////////////////////////////
constructor TMultiLangSupport.Create;
begin
SourceFile:=TMultiDataFile.Create;
Names:=TStringList.Create;
Contents:=TStringList.Create;
Images:=TStringList.Create;
ImgSegs:=TStringList.Create;
end;
destructor TMultiLangSupport.Destroy;
begin
SourceFile.Free;
Names.Free;
Contents.Free;
Images.Free;
ImgSegs.Free;
inherited Destroy;
end;
function TMultiLangSupport.Initialize(strseg: Integer; intimgseg: Integer; locimgseg: Integer): Boolean;
var
all: String;
line: String;
n, c: String;
e: Integer;
p: Integer;
i: Integer;
begin
// local strings segment
all:=SourceFile.GetSegmentAsString(strseg);
if all<>'' then begin
// clean up
Names.Clear;
Contents.Clear;
// parse string and fill data structures
// seperate lines
p:=1;
for i:=1 to Length(all) do begin
if all[i]=Char($0d) then begin
line:=Copy(all,p,i-p);
// if comment ignore, else split up
if line[1]<>';' then begin
e:=Pos('=',line);
n:=Copy(line,1,e-1);
c:=Copy(line,e+2,Length(line)-e-2);
Names.Add(n);
Contents.Add(c);
end;
// next line
if (i+2)<Length(all) then p:=i+2
else p:=Length(all)+1;
end;
end;
// if nescessary, handle rest
if p<Length(all) then begin
line:=Copy(all,p,Length(all)-p+1);
if line[1]<>';' then begin
e:=Pos('=',line);
n:=Copy(line,1,e-1);
c:=Copy(line,e+2,Length(line)-e-2);
Names.Add(n);
Contents.Add(c);
end;
end;
//
Initialize:=True;
end
else Initialize:=True;
// international image segment
all:=SourceFile.GetSegmentAsString(intimgseg);
if all<>'' then begin
// clean up
Images.Clear;
ImgSegs.Clear;
// parse string and fill data structures
// seperate lines
p:=1;
for i:=1 to Length(all) do begin
if all[i]=Char($0d) then begin
line:=Copy(all,p,i-p);
// if comment ignore, else split up
if line[1]<>';' then begin
e:=Pos('=',line);
n:=Copy(line,1,e-1);
c:=Copy(line,e+1,Length(line)-e);
Images.Add(n);
ImgSegs.Add(c);
end;
// next line
if (i+2)<Length(all) then p:=i+2
else p:=Length(all)+1;
end;
end;
// if nescessary, handle rest
if p<Length(all) then begin
line:=Copy(all,p,Length(all)-p+1);
if line[1]<>';' then begin
e:=Pos('=',line);
n:=Copy(line,1,e-1);
c:=Copy(line,e+1,Length(line)-e);
Images.Add(n);
ImgSegs.Add(c);
end;
end;
//
Initialize:=True;
end
else Initialize:=True;
// local image segment
all:=SourceFile.GetSegmentAsString(locimgseg);
if all<>'' then begin
// parse string and fill data structures
// seperate lines
p:=1;
for i:=1 to Length(all) do begin
if all[i]=Char($0d) then begin
line:=Copy(all,p,i-p);
// if comment ignore, else split up
if line[1]<>';' then begin
e:=Pos('=',line);
n:=Copy(line,1,e-1);
c:=Copy(line,e+1,Length(line)-e);
Images.Add(n);
ImgSegs.Add(c);
end;
// next line
if (i+2)<Length(all) then p:=i+2
else p:=Length(all)+1;
end;
end;
// if nescessary, handle rest
if p<Length(all) then begin
line:=Copy(all,p,Length(all)-p+1);
if line[1]<>';' then begin
e:=Pos('=',line);
n:=Copy(line,1,e-1);
c:=Copy(line,e+1,Length(line)-e);
Images.Add(n);
ImgSegs.Add(c);
end;
end;
//
Initialize:=True;
end
else Initialize:=True;
end;
procedure TMultiLangSupport.SetFileName(FN: String);
begin
SourceFile.ReadFile(FN);
end;
function TMultiLangSupport.GetCodeString(u: String; id: Integer): String;
var
name2find: String;
idStr: String;
begin
idStr:=InttoStr(id);
if id<100 then idStr:='0'+idStr;
if id<10 then idStr:='0'+idStr;
name2find:=LowerCase(u+'.$code'+idStr);
GetCodeString:=GetComponentString(name2find);
end;
procedure TMultiLangSupport.GetCodeStringList(u: String; id: Integer; sl: TStrings);
var
name2find: String;
idStr: String;
begin
idStr:=InttoStr(id);
if id<100 then idStr:='0'+idStr;
if id<10 then idStr:='0'+idStr;
name2find:=LowerCase(u+'.$code'+idStr);
GetComponentStringList(name2find,sl);
end;
function TMultiLangSupport.GetComponentString(id: String): String;
var
i: Integer;
found: String;
begin
i:=0;
found:='';
while (i<Names.Count) and (found='') do begin
if LowerCase(Names.Strings[i])=Lowercase(id) then found:=Contents[i];
Inc(i);
end;
GetComponentString:=found;
end;
procedure TMultiLangSupport.GetComponentStringList(id: String; sl: TStrings);
var
all, line: String;
p, i: Integer;
esc: Boolean;
begin
sl.Clear;
all:=GetComponentString(id);
// parse for /n
p:=1;
esc:=False;
for i:=1 to Length(all) do begin
if all[i]='\' then esc:=True
else begin
if (all[i]='n') and esc then begin
esc:=False;
if i>p+1 then line:=Copy(all,p,i-p-1)
else line:='';
sl.Add(line);
p:=i+1;
end;
end;
end;
// handle rest
if p<=Length(all) then line:=Copy(all,p,Length(all)-p+1)
else line:='';
sl.Add(line);
end;
procedure TMultiLangSupport.GetComponentImage(id: String; img: TGraphic);
var
i: Integer;
found: String;
seg: Integer;
ABitmap: TBitmap;
begin
i:=0;
found:='';
while (i<Images.Count-1) and (found='') do begin
if LowerCase(Images.Strings[i])=Lowercase(id) then found:=ImgSegs[i];
Inc(i);
end;
Images.Add('---');
seg:=StrToInt(found);
ABitmap:=TBitmap.Create;
SourceFile.GetSegmentAsBitmap(seg,ABitmap);
img.Assign(ABitmap);
ABitmap.Free;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -