unit janLanguage;
{ This component is an orginal component created 17-october 1999 by Jan Verhoeven and is offered as freeware for free use in any application including commercial applications. You are free to copy and modify the source code provided you do not remove this comment.
email: jan1.verhoeven@wxs.nl
website: http://members.xoom.com/JanVee/jfdelphi.htm } interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,TypInfo,Inifiles;
type TjanLanguage = class(TComponent) private LanguageFile:string; procedure LoadLanguage(AForm: TForm; Afile: string); procedure SaveLanguage(AForm: TForm; Afile: string); function appldir: string; function inifile: string; { Private declarations } protected { Protected declarations } public { Public declarations } procedure InitLanguage(AForm:TForm); procedure ChangeLanguage(AForm:Tform); published { Published declarations } end;
procedure Register;
implementation
const cr = chr(13)+chr(10); tab = chr(9);
procedure Register; begin RegisterComponents('Jans 2', [TjanLanguage]); end;
function TjanLanguage.appldir:string; begin result:=extractfilepath(application.exename); end;
function TjanLanguage.inifile:string; begin result:=changefileext(application.exename,'.ini'); end;
procedure TjanLanguage.ChangeLanguage(AForm: Tform); var ini:TIniFile; od:TOpendialog; begin od:=TOpendialog.Create(self) ; od.InitialDir :=appldir; od.Filter :='Language Files |*.lng'; if od.Execute then begin LanguageFile:=od.FileName ; ini:=TInifile.Create (inifile); ini.WriteString ('Language',Aform.name,LanguageFile); ini.free; LoadLanguage (Aform,LanguageFile) end; od.free; end;
procedure TjanLanguage.InitLanguage(AForm: TForm); var ini:TIniFile; begin LanguageFile:=appldir+'Language.lng'; SaveLanguage (Aform,LanguageFile); ini:=TInifile.Create (inifile); LanguageFile:=ini.readString ('Language',Aform.name,LanguageFile); if fileexists(LanguageFile) then LoadLanguage (Aform,LanguageFile); ini.WriteString ('Language',Aform.name,LanguageFile); ini.free; end;
procedure TjanLanguage.LoadLanguage(AForm:TForm;Afile:string); var c:Tcomponent; ini:tinifile; langfile:string; langs:TStringlist; i,p:integer; aname,avalue:string; afrm,acomp,aprop:string; AComponent:Tcomponent; PropInfo:PPropInfo;
procedure split(s:string); var p:integer; begin p:=pos('=',s); aname:=copy(s,1,p-1); avalue:=copy(s,p+1,length(s)); avalue:=stringreplace(avalue,'~~',cr,[rfreplaceall]); p:=pos('.',aname); afrm:=copy(aname,1,p-1); aname:=copy(aname,p+1,length(aname)); p:=pos('.',aname); acomp:=copy(aname,1,p-1); aprop:=copy(aname,p+1,length(aname)); end; begin langfile:=Afile; ini:=tinifile.Create (langfile); langs:=tstringlist.create; ini.ReadSectionValues ('TRANSLATIONS',langs); if langs.Count > 0 then begin for i:=0 to langs.count-1 do begin split(langs[i]); if Aform.name< > Afrm then continue; AComponent:=Aform.findcomponent(acomp); if AComponent= nil then continue; PropInfo:=GetPropInfo(Acomponent,aprop); if PropInfo< > nil then if propinfo^.PropType^.Kind= tkLString then setStrProp(Acomponent,aprop,avalue); end; end; ini.free; langs.free; end;
procedure TjanLanguage.SaveLanguage(AForm:TForm;Afile:string); var i:integer; langini:TInifile; LangFile:string; acap:string; ahint:string; atext:string; PropInfo:PPropInfo; m:Tcomponent; begin LangFile:=Afile; langini:=TInifile.Create(LangFile); for i:=0 to Aform.ComponentCount-1 do begin m:=Aform.components[i]; PropInfo:=GetPropInfo(m,'caption'); if PropInfo< > nil then begin acap:=GetStrProp(m,PropInfo); acap:=stringreplace(acap,cr,'~~',[rfreplaceall]); if ((acap< > '')and(acap< > '-')) then Langini.WriteString ('Translations',AForm.name+'.'+m.name+'.Caption',acap) end; PropInfo:=GetPropInfo(m,'hint'); if PropInfo< > nil then begin ahint:=GetStrProp(m,PropInfo); ahint:=stringreplace(ahint,cr,'~~',[rfreplaceall]); if ahint< > '' then Langini.WriteString ('Translations',AForm.name+'.'+m.name+'.Hint',ahint) end; PropInfo:=GetPropInfo(m,'text'); if PropInfo< > nil then begin atext:=GetStrProp(m,PropInfo); atext:=stringreplace(atext,cr,'~~',[rfreplaceall]); if atext< > '' then Langini.WriteString ('Translations',AForm.name+'.'+m.name+'.Text',atext) end; end; Langini.Free; end;
end.
|