/
inilang.pas
executable file
·416 lines (374 loc) · 13.9 KB
/
inilang.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
{IniLang v 0.9
Freeware unit for Delphi 4 projects, provided 'As Is'
Frédéric Sigonneau <aFas member> 24/04/1999
e-mail : frederic.sigonneau@wanadoo.fr
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 based on ini files.
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, they have their part in
that work.
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 IniLang;
interface
uses Windows, SysUtils, Classes, Forms, stdCtrls, typInfo,
extCtrls, iniFiles, Controls;
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;
//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:integer;
fiche,cmpt:TComponent;
val,comp,nomComp:string;
begin
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;
end;
end;
{search for error or information messages stored as
const or resourcestring - see below}
// searchStr(ini);
ini.free;
end;
{Translates the forms 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.
See demo for a sample}
procedure fillProps(TC:array of TComponent;ini:TMemIniFile);
var
i,i2,i3,i4,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' then TCustomMemo(comp).lines[i2]:=s3;
end;
if comp is TRadioGroup then
for i3:=0 to TRadioGroup(comp).items.count-1 do
begin
s4:=readString(fiche.name,
fiche.name+'.'+comp.name+'.items['+intToStr(i3)+']','');
if s4<>'' then TRadioGroup(comp).items[i3]:=s4;
end;
if comp is TComboBox then
for i4:=0 to TComboBox(comp).items.count-1 do
begin
s5:=readString(fiche.name,
fiche.name+'.'+comp.name+'.items['+IntToStr(i4)+']','');
if s5<>'' then TComboBox(comp).items[i4]:=s5;
end;
end;
end;
end;
{Translates a miscellaneous message in the current language.
The VAL parameter is the identification name of your string in a const or
resourcestring section in any file of your project.
The KEY parameter is the keyname of your string in the 'Misc' ini file section.
VAL value is used if no ini language file is present or loaded
ie it's your design-time language).
KEY is used otherwise to display the message in the current ini language.
Synthax : showMessage(misc(Msg21,'Msg21'));
This function uses the separator *|* to restore multiline messages}
function misc(VAL,KEY:string):string;
var
res,tempRes:string;
TS:array of string;
x:integer;
begin
if CL=nil then
begin
result:=VAL;
exit;
end;
res:=CL.readString('Misc',KEY,'');
if pos(sep,res)=0 then //single line text
result:=res
else
begin
setLength(TS,15); //increase this value if necessary for you
x:=low(TS);
tempRes:='';
repeat
TS[x]:=copy(res,1,pos(sep,res)-1);
if tempRes='' then tempRes:=TS[x]
else tempRes:=tempRes+#13#10+TS[x];
res:=copy(res,pos(sep,res)+3,length(res));
until pos(sep,res)=0;
TS:=nil;
tempRes:=tempRes+#13#10+res;
result:=tempRes;
end;
end;
{Your text strings must be stored in 'const' or 'resourcestring' sections
in your source files to be found by this procedure and saved in the ini file.
Prepare a little bit your code to make the job easier. For instance don't
write : 'if edit1.text='' then'
but : 'if edit.text= '' then' (with a space between = and the first ')
Strings with CR/LF (#13#10) will be saved in a unique key value, the different
lines of the message being separated by a *|* sequence}
procedure searchStr(var ini:TIniFile);
var
rep:string;
sr:TSearchRec;
F:TextFile;
line,ligne,temp,tempL:string;
strList:TStringList;
x,y:integer;
begin
rep:=extractFileDir(application.exeName)+'\';
strList:=TStringList.create;
{Search string resources in pas files}
if findFirst(rep+'*.pas',faAnyFile,sr)=0 then
repeat
if (sr.name='IniLang.pas') then findNext(sr);
assignFile(F,sr.name);
reset(F);
while not eof(F) do
begin
readln(F,line);
line:=trim(line);
//look for string resources ie =' sequences
if pos('=''',line)>0 then strList.add(line);
//look if the line is a part of a multiline message
//ie ' (single quote), + or # at the beginning of the line.
//add what you want if I forgot something you need for.
//put a {} sequence at the beginning of a line you don't
//want to be added in the file.
if (pos('''',line)=1) or (pos('+',line)=1) or
(pos('#',line)=1) then
strList.add('bis'+line); //'bis'-->temporary prefix for
end; //multiline strings
closeFile(F);
fileClose(sr.findHandle);
until findNext(sr)<>0;
findClose(sr);
//copy multiline messages on a unique key value
for x:=strList.count-1 downto 0 do
if pos('bis',strList[x])=1 then
begin
tempL:=strList[x];
tempL:=strList[x-1]+copy(tempL,4,length(tempL));
strList[x-1]:=tempL;
strList.delete(x);
continue;
end;
{Deletes useless caracters in ini string format --> ; ' ''
Also separates multilines messages with the *|* separator}
for x:=0 to strList.count-1 do
begin
ligne:=strList[x];
delete(ligne,length(ligne)-1,2);
delete(ligne,pos('=',ligne)+1,1);
if pos('''''',ligne)>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.