Hola compañeros,
He implementado algunas modificaciones al IniLang2, que amplian el abanico de componentes que permte traducir (DBgrid, Panel, TabControl)
dejo el codigo para quien lo quiera utilizar:
Código:
{ This is a text added for IniLang2.
The difference in IniLang2 is that has been included the procedure "Before_SearchStr"
and that the file "custom.ini" is deleted (at the beggining of the procedure
"fillcustomini").
IniLang2 (and IniLang too) works with Delphi 4, 5, 6 and 7. May be that it works well
in later versions of Delphi (it has not been checked).
}
{Freeware unit for Delphi 4 projects, provided 'As Is'
Frédéric Sigonneau <aFas> 24/04/1999
e-mail : [email protected]
IniLang can help you to build at design-time, in a single pass, a .ini file
filled with all properties and messages you have to localize for a
multilingual distribution.
Useful for large projects including many forms and lot of messages
(and, you know, a little project grows very quickly!).
At run-time, allows you to change your interface localization 'on the fly', as
long as you translated the .ini file created with IniLang in as many languages
you want to be available. Yet, that's the only ::)))) job you have to do
by yourself.
I wrote that unit because the components or units written by Serge Sushko,
Aldo Ghigliano or José Maria Gías didn't exactly do what I needed (ie less
work as possible!), but their ideas and their code helped me several times
to turn off some problems. So, thanks to all of them.
Finally, please, read commentaries before use. Then try, there is no danger !
If you use and improve this unit, just send me a copy if possible. Thanks. FS}
{Copy this file in the directory of your project and add IniLang to
the 'uses' implementation clause of units which use it, as usual}
unit IniLang2;
interface
uses Windows, SysUtils, Classes, Forms, stdCtrls, typInfo,
extCtrls, iniFiles, Controls, DBGrids, ComCtrls;
const
sep='*|*'; //separator for multiline messages
FR='French.ini'; FRENCH=2;
EN='English.ini'; ENGLISH=0;
SP='Spanish.ini'; SPANISH=1;
CU='Custom.ini'; CUSTOM=3;
//add what you want
var
CL:TMemIniFile; //Global variable for current language
//must be inited in the onShow event of your main form.
// Synthax : CL:=loadIni(XX);
//where XX is the const name of the ini file you want to load.
//You can use either a string or an integer const to load a language file.
//user procs
function loadIni(nom:string):TMemIniFile;overload;
function loadIni(ID:integer):TMemIniFile;overload;
procedure fillCustomIni;
procedure fillProps(TC:array of TComponent;ini:TMemIniFile);
function misc(VAL,KEY:string):string;
procedure Before_SearchStr ; // This procedure was added in IniLang2
//utilities
procedure searchStr(var ini:TIniFile);
function getProp(comp:TComponent;prop:string):string;
procedure setProp(comp:TComponent;{const }prop,value:string);
function HasProperty(comp:TComponent;prop:string):boolean;
function str2IntID(s:string):integer;
function intID2Str(ID:integer):string;
implementation
function loadIni(nom:string):TMemIniFile;
var
chemin:string;
begin
chemin:=extractFileDir(application.exeName)+'\'+nom;
if not fileExists(chemin) then
result:=nil
else
result:=TMemIniFile.create(chemin);
end;
//other way for the same result
function loadIni(ID:integer):TMemIniFile;overload;
var
chemin,nom:string;
begin
case ID of
-1:nom:= '';
FRENCH:nom:=FR;
ENGLISH:nom:=EN;
SPANISH:nom:=SP;
CUSTOM:nom:=CU;
end;
chemin:=extractFileDir(application.exeName)+'\'+nom;
if not fileExists(chemin) then
result:=nil
else
result:=TMemIniFile.create(chemin);
end;
{Creates the original iniFile 'Custom.ini' and fills it first
with string properties and their values (captions, hints,
items -TRadioGroup and TComboBox-
and lines -TMemo and TRichEdit- values).
One section for each form in the project.
Then fills a 'Misc' section with strings declared with 'const' or
'resourcestring' keywords (customized messages to inform your users or
properties dynamically renamed in your code).
Call 'fillCustomIni' from the onCreate event of the last created form
in your project.
At this time, ALL FORMS MUST BE IN THE 'AUTOMATICALLY
CREATED FORMS' list of your Project\Options\Forms tab .
Then just run form IDE (F9) and close your project, and it's done !}
procedure fillCustomIni;
var
ini:TIniFile;
i,j,k,l,m,n:integer;
fiche,cmpt:TComponent;
val,comp,nomComp:string;
begin
// Delete the old file "custom.ini" if it exists
// This code was added in Inilang2
// Borra el anterior archivo "custom.ini" si existía
// Este código fue añadido en Inilang2
if FileExists ( 'custom.ini' )
then DeleteFile ( 'custom.ini' ) ;
ini:=TIniFile.create(extractFileDir(application.exeName)+'\'+CU);
{First search the properties that will be translated}
for i := 0 to application.ComponentCount - 1 do
begin
fiche:=application.Components[i];
nomComp:=fiche.Name;
if HasProperty(fiche,'Caption') and
not (nomComp=getProp(fiche,'Caption')) then
ini.writeString(nomComp,nomComp+'.Caption',
getProp(fiche,'Caption'));
for j:=0 to fiche.componentCount-1 do
begin
cmpt:=fiche.Components[j];
nomComp:=cmpt.name;
//use a 'out' prefixed name if you want the component won't appear
//in the list (ie : outLabel6:TLabel) so as the list won't be too big
if copy(nomComp,1,3)='out' then continue;
if HasProperty(cmpt,'Caption') and
not (nomComp=getProp(cmpt,'Caption')) and
not (getProp(cmpt,'Caption')='-') and
not (getProp(cmpt,'Caption')='') then
ini.writeString(fiche.Name,fiche.Name+'.'+nomComp+'.Caption',
getProp(cmpt,'Caption'));
if HasProperty(cmpt,'Hint') and (getProp(cmpt,'Hint')<>'')then
ini.writeString(fiche.Name,fiche.Name+'.'+nomComp+'.Hint',
getProp(cmpt,'Hint'));
if (cmpt is TCustomMemo) then
for k:=0 to TCustomMemo(cmpt).Lines.count-1 do
begin
val:=TCustomMemo(cmpt).Lines[k];
if val=nomComp then break;
comp:=nomComp+'.Lines['+intToStr(k)+']';
ini.writeString(fiche.Name,fiche.Name+'.'+comp,val);
end;
if (cmpt is TRadioGroup) then
for l:= 0 to TRadioGroup(cmpt).Items.Count-1 do
begin
val:=TRadioGroup(cmpt).Items[l];
comp:=nomComp+'.Items['+IntToStr(l)+']';
ini.writeString(fiche.Name,fiche.Name+'.'+comp,val);
end;
if (cmpt is TComboBox) then
for m:=0 to TComboBox(cmpt).Items.Count-1 do
begin
val:=TComboBox(cmpt).Items[m];
comp:=nomComp+'.Items['+IntToStr(m)+']';
ini.writeString(fiche.Name,fiche.Name+'.'+comp,val);
end;
if (cmpt is TDBGrid) then
for n:=0 to TDBGrid(cmpt).Columns.Count-1 do
begin
val:=TDBGrid(cmpt).Columns.Items[n].Title.Caption;
comp:=nomComp+'.Items['+IntToStr(n)+']';
ini.writeString(fiche.Name,fiche.Name+'.'+comp,val);
end;
if (cmpt is TTabControl) then
for n:=0 to TTabControl(cmpt).Tabs.Count-1 do
begin
val:=TTabControl(cmpt).Tabs[n];
comp:=nomComp+'.Items['+IntToStr(n)+']';
ini.writeString(fiche.Name,fiche.Name+'.'+comp,val);
end;
end;
end;
{ In the code of "*.pas" files (after 'implementation'), if find a line that beggins
with ( ' + # ), then add comments at the beggining of the line for avoid mistakes
on the procedure 'searchStr(ini);' } // This is the code added whith Inilang2
{ En el código de ficheros "*.pas" (después de 'implementation'), si alguna línea
empieza por ( ' + # ), se añade comentarios al inicio de esa línea para evitar errores
en el procedimiento 'searchStr(ini);' } // Este es el código añadido con Inilang2
Before_SearchStr ;
{search for error or information messages stored as
const or resourcestring - see below}
searchStr(ini);
ini.free;
end;
procedure Before_SearchStr ;
var
rep:string;
sr:TSearchRec;
line :string;
Archivo :TStringList;
cc :integer;
Actuar, cambios : boolean ;
begin
rep:=extractFileDir(application.exeName)+'\';
Archivo:=TStringList.create;
if findFirst(rep+'*.pas', faAnyFile, sr)=0
then
repeat
if (sr.name='IniLang2.pas')
then findNext(sr);
Archivo.Clear ;
Archivo.LoadFromFile( sr.Name );
Actuar := false ;
Cambios := false ;
for cc := 0 to Archivo.Count -1 do
begin
// It works only after 'implementation'
// solo actúa después de 'implementation'
if LowerCase( copy ( trim ( Archivo [ cc ] ), 1, 14 ) ) = 'implementation'
then Actuar := true ;
if Actuar
then
begin
line := trim ( Archivo [ cc ] ) ;
// if the first character is ( ' + # ), then it add '{}' at the beginning
// Si empieza por ( ' + # ), entonces le añade '{}' al principio
if (pos('''',line)=1)
or (pos('+',line)=1)
or (pos('#',line)=1)
then
begin
Cambios := true ;
if copy ( Archivo [ cc ], 1, 2 ) = ' '
then
Archivo [ cc ] := '{}'
{} + copy ( Archivo [ cc ], 3, length ( Archivo [ cc ] ) )
else
Archivo [ cc ] := '{}' + Archivo [ cc ] ;
end ;
end ;
end;
if cambios
then
Archivo.SaveToFile( SR.Name );
fileClose(sr.findHandle);
until findNext(sr)<>0;
findClose(sr);
end;
{Translates the form you choose in the language called in ini.
Only created forms are translated with fillProps. Call it in the onShow
event of your main form whith names of all automatically created forms
at the start-up of your application in the TC parameter.
In runtime, call it when you create dynamically a form. For example :
procedure TForm1.Button3Click(Sender: TObject);
begin
if form2=nil then
form2:=TForm2.create(application);
if CL<>nil then fillProps([form2],CL);
form2.show;
end;}
procedure fillProps(TC:array of TComponent;ini:TMemIniFile);
var
i,i2,i3,i4,i5,tab:integer;
comp,fiche:TComponent;
s,s1,s2,s3,s4,s5:string;
begin
with ini do
for tab:=0 to high(TC) do
begin
fiche:=TC[tab];
if fiche=nil then continue;
s:=readString(fiche.name,fiche.name+'.Caption','');
if s<>'' then TForm(fiche).caption:=s;
for i:=0 to fiche.componentCount-1 do
begin
comp:=fiche.Components[i];
s1:=readString(fiche.name,fiche.name+'.'+comp.name+'.Caption','');
if s1<>'' then setProp(comp,'Caption',s1);
s2:=readString(fiche.name,fiche.name+'.'+comp.name+'.Hint','');
if s2<>'' then setProp(comp,'Hint',s2);
if comp is TCustomMemo then
for i2:=0 to TCustomMemo(comp).lines.count-1 do
begin
s3:=readString(fiche.name,
fiche.name+'.'+comp.name+'.lines['+intToStr(i2)+']','fsdef');
//in TMemo or TRichEdit, you may have to leave some lines empty
if s3<fsdef>0 then
repeat
delete(ligne,pos('''''',ligne),1);
until pos('''''',ligne)=0;
strList[x]:=ligne;
temp:=strList.values[strList.names[x]];
if pos('''+',temp)=0 then
ini.writeString('Misc',strList.names[x],temp)
else
begin
repeat
y:=pos('''+',temp);
repeat
delete(temp,y,1);
until temp[y]='''';
temp:=copy(temp,1,y-1)+sep+copy(temp,y+1,length(temp));
until pos('''+',temp)=0;
ini.writeString('Misc',strList.names[x],temp);
end;
end;
strList.free;
end;
//Backs up the prop property value of the comp component
function getProp(comp:TComponent;prop:string):string;
var
ppi:PPropInfo;
begin
ppi:=getPropInfo(comp.classInfo,prop);
if ppi<>nil then
result:=getStrProp(comp,ppi)
else
result:='';
end;
//Assign the value value to prop property of comp component
procedure setProp(comp:TComponent;{const }prop,value:string);
var
ppi:PPropInfo;
begin
if value<>'' then
begin
ppi:=getPropInfo(comp.classInfo,prop);
if ppi<>nil then setStrProp(comp,ppi,value);
end;
end;
//True if prop property exists for comp component
function HasProperty(comp:TComponent;prop:string):boolean;
begin
result:=(getPropInfo(comp.classInfo,prop)<>nil) and (comp.name<>'');
end;
//Converts a const string language name into a const integer language name
function str2IntID(s:string):integer;
begin
result:=-1;
if s=FR then result:=FRENCH //2
else if s=EN then result:=ENGLISH //0
else if s=SP then result:=SPANISH //1
else if s=CU then result:=CUSTOM; //3;
end;
//Converts a const integer language name into a const string language name
function intID2Str(ID:integer):string;
begin
result:='';
if ID=2 then result:=FR
else if ID=0 then result:=EN
else if ID=1 then result:=SP
else if ID=3 then result:=CU;
end;
end.
Saludos,
__________________
Saludos,
Bitman
|