forked from Ravie/quice
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Translate.pas
395 lines (363 loc) · 12.3 KB
/
Translate.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
unit Translate;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls, Buttons, Menus, JvToolEdit;
const
LANG_NONE_FORM = 0;
LANG_CHECK_FORM = 1;
LANG_CREATUREORGO_FORM = 2;
LANG_ITEM_FORM = 3;
LANG_LIST_FORM = 4;
LANG_MAIN_FORM = 5;
LANG_CONNECT_FORM = 6;
LANG_SETTINGS_FORM = 7;
LANG_SPELLS_FORM = 8;
LANG_WHOQUEST_FORM = 9;
LANG_ITEMLOOT_FORM = 10;
LANG_CUSTOM = 11;
LANG_ITEMPAGE_FORM = 12;
LANG_GUID_FORM = 13;
LANG_UNITFLAGS_FORM = 14;
LANG_AREATABLE_FORM = 15;
LANG_COUNT = 16;
LANG_MAX_STRINGS = 3000;
type
TTranslate = class
private
{
// 0 - internal language, 1 - external language
}
FLanguage: array [0..1, 0..LANG_COUNT, 0..LANG_MAX_STRINGS] of string;
FPartNames: array [0..LANG_COUNT] of string;
{$IFNDEF DEBUG}
function GetPartByName(Name: string): integer;
function GetFirst(s: string): string;
function GetSecond(s: string): string;
{$ENDIF}
procedure Init;
public
constructor Create;
procedure TranslateForm(Form: TForm);
procedure LoadTranslations(FileName: string);
procedure CreateDefaultTranslation(Form: TForm);
procedure CreateCustomTranslation;
function GetTranslation(s: string; Part: integer; olds: string): string;
procedure TranslateCustomText;
end;
implementation
uses StrUtils, MainUnit, CheckUnit, AreaTableUnit,
CreatureOrGOUnit, ItemUnit, ListUnit, SettingsUnit,
SpellsUnit, WhoUnit, MyDataModule, ItemLootUnit,
ItemPageUnit, GUIDUnit, UnitFlagsUnit, MeConnectForm;
{ TTranslate }
constructor TTranslate.Create;
begin
Init;
end;
{$IFNDEF DEBUG}
function TTranslate.GetFirst(s: string): string;
begin
if pos('=', s)<>0 then
Result:=MidStr(s, 1, pos('=', s)-1)
else
Result:=s;
end;
function TTranslate.GetSecond(s: string): string;
begin
if pos('=', s)<>0 then
Result:=MidStr(s, pos('=', s)+1, 1000)
else
Result:='';
end;
function TTranslate.GetPartByName(Name: string): integer;
var
i: integer;
begin
Result:=0;
for i:=0 to LANG_COUNT - 1 do
begin
if FPartNames[i] = UpperCase(Name) then
begin
Result := i;
Exit;
end;
end;
end;
{$ENDIF}
procedure TTranslate.Init;
var
i, j,k: integer;
begin
FPartNames[LANG_NONE_FORM] := '[NONE]';
FPartNames[LANG_CHECK_FORM] := '[CHECK]';
FPartNames[LANG_CREATUREORGO_FORM]:= '[CREATUREORGO]';
FPartNames[LANG_ITEM_FORM] := '[ITEM]';
FPartNames[LANG_LIST_FORM] := '[LIST]';
FPartNames[LANG_MAIN_FORM] := '[MAIN]';
FPartNames[LANG_CONNECT_FORM] := '[CONNECT]';
FPartNames[LANG_SETTINGS_FORM] := '[SETTINGS]';
FPartNames[LANG_SPELLS_FORM] := '[SPELLS]';
FPartNames[LANG_WHOQUEST_FORM] := '[WHOQUEST]';
FPartNames[LANG_ITEMLOOT_FORM] := '[ITEMLOOT]';
FPartNames[LANG_CUSTOM] := '[CUSTOM]';
FPartNames[LANG_ITEMPAGE_FORM] := '[ITEMPAGE]';
FPartNames[LANG_GUID_FORM] := '[GETGUID]';
FPartNames[LANG_UNITFLAGS_FORM] := '[UNITFLAGS]';
FPartNames[LANG_AREATABLE_FORM] := '[AREATABLE]';
for i:=0 to 1 do
for j:=0 to LANG_COUNT - 1 do
for k:=0 to LANG_MAX_STRINGS do
FLanguage[i, j, k]:='';
end;
procedure TTranslate.LoadTranslations(FileName: string);
{$IFNDEF DEBUG}
var
LangFile: TStringList;
i, j: integer;
s: string;
Part: integer;
{$ENDIF}
begin
{$IFNDEF DEBUG}
if not FileExists(FileName) then Exit;
LangFile:=TStringList.Create;
try
LangFile.LoadFromFile(FileName);
Part:=0;
j:=0;
for i:=0 to LangFile.Count - 1 do
begin
s:=Trim(LangFile[i]);
if s='' then continue;
if pos('[', s)=1 then
begin
Part:=GetPartByName(s);
j:=0;
end
else
begin
FLanguage[0,Part,J]:=GetFirst(s);
FLanguage[1,Part,J]:=GetSecond(s);
inc(j);
end;
end;
finally
LangFile.Free;
end;
{$ENDIF}
end;
procedure TTranslate.TranslateForm(Form: TForm);
var
Part: integer;
i: integer;
begin
Part := LANG_NONE_FORM;
if Form is TMainForm then Part := LANG_MAIN_FORM;
if Form is TMeConnectForm then Part := LANG_CONNECT_FORM;
if Form is TCheckForm then Part := LANG_CHECK_FORM;
if Form is TCreatureOrGOForm then Part := LANG_CREATUREORGO_FORM;
if Form is TItemForm then Part := LANG_ITEM_FORM;
if Form is TListForm then Part := LANG_LIST_FORM;
if Form is TSettingsForm then Part := LANG_SETTINGS_FORM;
if Form is TSpellsForm then Part := LANG_SPELLS_FORM;
if Form is TWhoQuestForm then Part := LANG_WHOQUEST_FORM;
if Form is TItemLootForm then Part := LANG_ITEMLOOT_FORM;
if Form is TItemPageForm then Part := LANG_ITEMPAGE_FORM;
if Form is TGUIDForm then Part := LANG_GUID_FORM;
if Form is TUnitFlagsForm then Part := LANG_UNITFLAGS_FORM;
if Form is TAreaTableForm then Part := LANG_AREATABLE_FORM;
if Part = LANG_NONE_FORM then Exit;
Form.Caption := GetTranslation('Caption', Part, Form.Caption);
for i:=0 to Form.ComponentCount - 1 do
begin
if Form.Components[i] is TLabel then
TLabel(Form.Components[i]).Caption:=
GetTranslation(Form.Components[i].Name, Part, TLabel(Form.Components[i]).Caption);
if (Form.Components[i] is MainUnit.TLabeledEdit) or (Form.Components[i] is ExtCtrls.TLabeledEdit) then
begin
TLabeledEdit(Form.Components[i]).EditLabel.Caption:=
GetTranslation(Form.Components[i].Name, Part, TLabeledEdit(Form.Components[i]).EditLabel.Caption);
TLabeledEdit(Form.Components[i]).Hint:= StringReplace(
GetTranslation('^'+Form.Components[i].Name, Part, TLabeledEdit(Form.Components[i]).Hint),
'$B$B',#13#10,[rfReplaceAll]);
TLabeledEdit(Form.Components[i]).ShowHint := true;
end;
if Form.Components[i] is TJvComboEdit then
begin
TJvComboEdit(Form.Components[i]).Hint:= StringReplace(
GetTranslation('^'+Form.Components[i].Name, Part, TJvComboEdit(Form.Components[i]).Hint),
'$B$B',#13#10,[rfReplaceAll]);
TJvComboEdit(Form.Components[i]).ShowHint := true;
end;
if Form.Components[i] is TMemo then
begin
TMemo(Form.Components[i]).Hint:= StringReplace(
GetTranslation('^'+Form.Components[i].Name, Part, TMemo(Form.Components[i]).Hint),
'$B$B',#13#10,[rfReplaceAll]);
TMemo(Form.Components[i]).ShowHint := true;
end;
if Form.Components[i] is TButton then
TButton(Form.Components[i]).Caption:=
GetTranslation(Form.Components[i].Name, Part, TButton(Form.Components[i]).Caption);
if Form.Components[i] is TGroupBox then
TGroupBox(Form.Components[i]).Caption:=
GetTranslation(Form.Components[i].Name, Part, TGroupBox(Form.Components[i]).Caption);
if Form.Components[i] is TTabSheet then
TTabSheet(Form.Components[i]).Caption:=
GetTranslation(Form.Components[i].Name, Part, TTabSheet(Form.Components[i]).Caption);
if Form.Components[i] is TCheckBox then
TCheckBox(Form.Components[i]).Caption:=
GetTranslation(Form.Components[i].Name, Part, TCheckBox(Form.Components[i]).Caption);
if Form.Components[i] is TRadioButton then
TRadioButton(Form.Components[i]).Caption:=
GetTranslation(Form.Components[i].Name, Part, TRadioButton(Form.Components[i]).Caption);
if Form.Components[i] is TRadioGroup then
TRadioButton(Form.Components[i]).Caption:=
GetTranslation(Form.Components[i].Name, Part, TRadioGroup(Form.Components[i]).Caption);
if Form.Components[i] is TMenuItem then
TMenuItem(Form.Components[i]).Caption:=
GetTranslation(Form.Components[i].Name, Part, TMenuItem(Form.Components[i]).Caption);
end;
end;
function TTranslate.GetTranslation(s: string; Part: integer; olds: string): string;
var
i: integer;
begin
Result:=olds;
for i:=0 to LANG_MAX_STRINGS do
begin
if FLanguage[0,Part,i]=s then
begin
Result:=FLanguage[1,Part,i];
Exit;
end;
if FLanguage[0,Part,i]='' then Exit;
end;
end;
procedure TTranslate.CreateDefaultTranslation(Form: TForm);
{$IFDEF C}
var
i: integer;
s, s1, s2: string;
list: TStringList;
{$ENDIF}
begin
{$IFDEF C}
list:=TStringList.Create;
try
s:='';
if Form is TMainForm then s:=FPartNames[LANG_MAIN_FORM];
if Form is TMeConnectForm then s:=FPartNames[LANG_CONNECT_FORM];
if Form is TCheckForm then s:=FPartNames[LANG_CHECK_FORM];
if Form is TCreatureOrGOForm then s:=FPartNames[LANG_CREATUREORGO_FORM];
if Form is TItemForm then s:=FPartNames[LANG_ITEM_FORM];
if Form is TListForm then s:=FPartNames[LANG_LIST_FORM];
if Form is TSettingsForm then s:=FPartNames[LANG_SETTINGS_FORM];
if Form is TSpellsForm then s:=FPartNames[LANG_SPELLS_FORM];
if Form is TWhoQuestForm then s:=FPartNames[LANG_WHOQUEST_FORM];
if Form is TItemLootForm then s:=FPartNames[LANG_ITEMLOOT_FORM];
if Form is TItemPageForm then s:=FPartNames[LANG_ITEMPAGE_FORM];
if Form is TGUIDForm then s:=FPartNames[LANG_GUID_FORM];
if Form is TUnitFlagsForm then s:=FPartNames[LANG_UNITFLAGS_FORM];
if Form is TAreaTableForm then s:=FPartNames[LANG_AREATABLE_FORM];
if s='' then exit;
list.Add(s);
if not (Form is TMeConnectForm) then
list.Add(Format('Caption=%s',[Form.Caption]));
for i:=0 to Form.ComponentCount - 1 do
begin
s1:=Form.Components[i].Name;
s2:='';
if Form.Components[i] is TLabel then
begin
s2:=TLabel(Form.Components[i]).Caption;
if pos('----', s2)>0 then s2:='';
end;
if (Form.Components[i] is MainUnit.TLabeledEdit) or (Form.Components[i] is ExtCtrls.TLabeledEdit) then
begin
s2:=TLabeledEdit(Form.Components[i]).EditLabel.Caption;
list.Add(Format('%s=%s',[s1,s2]));
s2:=TLabeledEdit(Form.Components[i]).Hint;
if s2<>'' then
list.Add(Format('^%s=%s', [s1, StringReplace(s2,#13#10,'$B$B', [rfReplaceAll])]));
s2:='';
end;
if Form.Components[i] is TJvComboEdit then
begin
s2:=TJvComboEdit(Form.Components[i]).Hint;
if s2<>'' then
list.Add(Format('^%s=%s', [s1, StringReplace(s2,#13#10,'$B$B', [rfReplaceAll])]));
s2:='';
end;
if Form.Components[i] is TMemo then
begin
s2:=TMemo(Form.Components[i]).Hint;
if s2<>'' then
list.Add(Format('^%s=%s', [s1, StringReplace(s2,#13#10,'$B$B', [rfReplaceAll])]));
s2:='';
end;
if Form.Components[i] is TButton then
s2:=TButton(Form.Components[i]).Caption;
if Form.Components[i] is TGroupBox then
s2:=TGroupBox(Form.Components[i]).Caption;
if Form.Components[i] is TTabSheet then
s2:=TTabSheet(Form.Components[i]).Caption;
if Form.Components[i] is TCheckBox then
s2:=TCheckBox(Form.Components[i]).Caption;
if Form.Components[i] is TRadioButton then
s2:=TRadioButton(Form.Components[i]).Caption;
if Form.Components[i] is TRadioGroup then
s2:=TRadioGroup(Form.Components[i]).Caption;
if Form.Components[i] is TMenuItem then
begin
s2 := TMenuItem(Form.Components[i]).Caption;
if s2='-' then s2:='';
end;
if s2<>'' then
list.Add(Format('%s=%s',[s1,s2]));
end;
list.Add('');
if Form.Name = 'MainForm' then
list.SaveToFile(Format('%sCREATELANG\zzz%s.lng',[dmMain.ProgramDir, Form.Name]))
else
list.SaveToFile(Format('%sCREATELANG\%s.lng',[dmMain.ProgramDir, Form.Name]));
finally
List.free;
end;
{$ENDIF}
end;
procedure TTranslate.TranslateCustomText;
var
i: integer;
begin
for i:=0 to LANG_MAX_STRINGS do
begin
if dmMain.Text[i]='' then Exit;
dmMain.Text[i]:=StringReplace(GetTranslation(Format('TEXT%d',[i]),15,dmMain.Text[i]), '$B', #13#10, [rfReplaceAll]);
end;
end;
procedure TTranslate.CreateCustomTranslation;
{$IFDEF C}
var
i: integer;
list : TStringList;
{$ENDIF}
begin
{$IFDEF C}
ForceDirectories(dmMain.ProgramDir+'CREATELANG');
list := TStringList.Create;
try
list.Add(FPartNames[LANG_CUSTOM]);
for i:=1 to LANG_MAX_STRINGS do
begin
if dmMain.Text[i] <> '' then
list.Add(Format('TEXT%d=%s',[i, dmMain.Text[i]]));
end;
list.Add('');
list.SaveToFile(Format('%sCREATELANG\zzaCustom.lng',[dmMain.ProgramDir]));
finally
list.Free;
end;
{$ENDIF}
end;
end.