-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLocaleParser.pas
More file actions
503 lines (413 loc) · 11.5 KB
/
Copy pathLocaleParser.pas
File metadata and controls
503 lines (413 loc) · 11.5 KB
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
unit LocaleParser;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Declare;
type
TForm1 = class(TForm)
ButtonOpen: TButton;
ButtonSave: TButton;
OpenDialog1: TOpenDialog;
MemoFound: TMemo;
Label1: TLabel;
ButtonParse: TButton;
ButtonAvtoFM: TButton;
ButtonAvtoCE: TButton;
GroupBox1: TGroupBox;
Label2: TLabel;
Label3: TLabel;
etCommon: TEdit;
etLocalized: TEdit;
GroupBox2: TGroupBox;
Label4: TLabel;
Label5: TLabel;
etAddCommon: TEdit;
etAddLocalized: TEdit;
Label6: TLabel;
etAddReplacing: TEdit;
GroupBox3: TGroupBox;
Label7: TLabel;
Label8: TLabel;
etComment: TEdit;
etlanguageCode: TEdit;
Label9: TLabel;
etLanguageParentCode: TEdit;
GroupBox4: TGroupBox;
Label10: TLabel;
etFilename: TEdit;
chbAutoexit: TCheckBox;
ButtonClearMemo: TButton;
GroupBox5: TGroupBox;
chbTransable: TCheckBox;
procedure ButtonOpenClick(Sender: TObject);
procedure ButtonSaveClick(Sender: TObject);
procedure ButtonParseClick(Sender: TObject);
procedure ButtonAvtoFMClick(Sender: TObject);
procedure ButtonAvtoCEClick(Sender: TObject);
procedure FileToMemo();
procedure ValueOut();
procedure ValueDoXML();
procedure LogAdd(log: String);
procedure ParseString(strbuff: String);
procedure ParseLocales(strbuff: String);
procedure ParseAliasLocales(strbuff: String);
procedure ParseValues(strbuff: String);
procedure ParseRepeatValues(strbuff: String);
procedure ParseCommonValues(strbuff: String);
function ParseColumn(strbuff: String): TColumns;
procedure SetAliasValues();
procedure ParseSource();
procedure ButtonClearMemoClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ButtonAvtoCEClick(Sender: TObject);
begin
if not(fileChoosen) then
AssignFile(fileMemo, etFilename.Text);
parseAdditional := true;
ParseSource();
ValueDoXML();
if chbAutoexit.Checked then
Close();
end;
procedure TForm1.ButtonAvtoFMClick(Sender: TObject);
begin
if not(fileChoosen) then
AssignFile(fileMemo, etFilename.Text);
ParseSource();
ValueDoXML();
if chbAutoexit.Checked then
Close();
end;
procedure TForm1.ButtonClearMemoClick(Sender: TObject);
begin
MemoFound.Clear;
end;
procedure TForm1.ButtonOpenClick(Sender: TObject);
begin
FileToMemo();
end;
procedure TForm1.FileToMemo();
begin
OpenDialog1.Title := 'Choose file';
OpenDialog1.InitialDir := GetCurrentDir;
OpenDialog1.Filter := 'TSV files (*.tsv)|*.tsv|All files (*.*)|*.*';
if OpenDialog1.Execute then
begin
MemoFound.Lines.Clear;
MemoFound.Hint := 'File loaded successfull: ' + OpenDialog1.fileName;
MemoFound.ShowHint := true;
LogAdd('File loaded successfull:');
LogAdd(' ' + OpenDialog1.fileName);
AssignFile(fileMemo, OpenDialog1.fileName);
fileChoosen := true;
// ValueOut();
end
else
begin
LogAdd('File opening canceled!');
end;
end;
procedure TForm1.ValueOut();
var
i, j: integer;
str: string;
begin
for i := 1 to value_count - 1 do
begin
str := '';
if values[i].section <> '' then
str := str + 'sec[' + values[i].section + '] ';
if values[i].category <> '' then
str := str + 'cat[' + values[i].category + '] ';
str := str + 'name[' + values[i].name + ']';
for j := 1 to locale_count - 1 do
begin
str := str + ' ' + locales[j] + '[' + values[i].val[j] + ']';
end;
LogAdd(str);
end;
end;
procedure TForm1.ValueDoXML();
var
i, j: integer;
filecom, filexml: TextFile;
Path: String;
begin
CreateDir('res');
CreateDir('res\values');
AssignFile(filecom, 'res\values\string_common.xml');
ReWrite(filecom);
WriteLn(filecom, '<?xml version="1.0" encoding="utf-8"?>');
WriteLn(filecom, '<resources>');
for j := 1 to locale_count do
if loc_ready[j] then
begin
if (j = 1) then // base locale
AssignFile(filexml, 'res\values\string.xml')
else
begin
CreateDir('res\values-' + locales[j]);
AssignFile(filexml, 'res\values-' + locales[j] + '\string.xml');
end;
ReWrite(filexml);
WriteLn(filexml, '<?xml version="1.0" encoding="utf-8"?>');
WriteLn(filexml, '<resources>');
for i := 1 to value_count do
begin
if (values[i].val[j] <> '') then
begin
if values[i].typ = localed_type then
begin
if values[i].section <> '' then
WriteLn(filexml);
if values[i].section <> '' then
WriteLn(filexml, ' <!-- ' + values[i].section + ' -->');
if (values[i].category <> '') and (values[i].section = '') then
WriteLn(filexml);
if values[i].category <> '' then
WriteLn(filexml, ' <!-- ' + values[i].category + ' -->');
if values[i].name <> '' then
WriteLn(filexml,
' <string name="' + values[i].name + '">' + values[i].val[j] + '</string>');
end;
if (values[i].typ = common_type) and (j = 1) then // common text of base locale
begin
if values[i].section <> '' then
WriteLn(filecom);
if values[i].section <> '' then
WriteLn(filecom, ' <!-- ' + values[i].section + ' -->');
if (values[i].category <> '') and (values[i].section = '') then
WriteLn(filecom);
if values[i].category <> '' then
WriteLn(filecom, ' <!-- ' + values[i].category + ' -->');
if values[i].name <> '' then
begin
if chbTransable.Checked then
WriteLn(filecom,
' <string name="' + values[i].name + '" translatable="false">' + values[i].val
[j] + '</string>')
else
WriteLn(filecom,
' <string name="' + values[i].name + '">' + values[i].val[j] + '</string>');
end;
end;
end;
end;
WriteLn(filexml, '</resources>');
CloseFile(filexml);
LogAdd('Locale strings created!');
end;
WriteLn(filecom, '</resources>');
CloseFile(filecom);
for j := 1 to locale_count do
LogAdd('string-' + locales[j] + '.xml');
end;
procedure TForm1.LogAdd(log: String);
begin
MemoFound.Lines.Add(log);
end;
procedure TForm1.ParseString(strbuff: String);
var
c: char;
begin
// Form1.MemoFound.Lines.Add(log);
c := strbuff[1];
if (c = etlanguageCode.Text[1]) then
ParseLocales(strbuff);
if (c = etLanguageParentCode.Text[1]) then
ParseAliasLocales(strbuff);
if (c = etCommon.Text[1]) then
ParseCommonValues(strbuff);
if (c = etLocalized.Text[1]) then
ParseValues(strbuff);
c := strbuff[1];
if parseAdditional then
begin
// LogAdd(' CheckBoxAll Checked');
if (c = etAddCommon.Text[1]) then
ParseCommonValues(strbuff);
if (c = etAddLocalized.Text[1]) then
ParseValues(strbuff);
if (c = etAddReplacing.Text[1]) then
ParseRepeatValues(strbuff);
end;
end;
procedure TForm1.ParseLocales(strbuff: String);
var
c: char;
res: TColumns;
i: integer;
begin
res := ParseColumn(strbuff);
locale_count := length(res) - 6;
SetLength(locales, locale_count + 1);
SetLength(loc_ready, locale_count + 1);
for i := 1 to locale_count do
begin
locales[i] := res[i + 4];
if length(locales[i]) < 6 then
loc_ready[i] := true
else
loc_ready[i] := false;
end;
end;
procedure TForm1.ParseAliasLocales(strbuff: String);
var
c: char;
res: TColumns;
i: integer;
begin
res := ParseColumn(strbuff);
locale_count := length(res) - 6;
SetLength(alias_locales, locale_count + 1);
LogAdd('Found ' + IntToStr(locale_count) + ' locales');
for i := 1 to locale_count do
begin
alias_locales[i] := res[i + 4];
LogAdd('locale[' + IntToStr(i) + '] = ' + locales[i] + ' [' + alias_locales[i] + ']');
end;
end;
procedure TForm1.ParseValues(strbuff: String);
var
res: TColumns;
i: integer;
begin
res := ParseColumn(strbuff);
value_count := value_count + 1;
SetLength(values, value_count + 1);
SetLength(values[value_count].val, locale_count + 1);
values[value_count].typ := localed_type;
values[value_count].section := res[2];
values[value_count].category := res[3];
values[value_count].name := res[4];
for i := 1 to locale_count do
begin
values[value_count].val[i] := res[4 + i];
end;
end;
procedure TForm1.ParseRepeatValues(strbuff: String);
var
res: TColumns;
i, j: integer;
begin
res := ParseColumn(strbuff);
for i := 0 to value_count do
if (values[i].name = res[4]) then
begin
values[i].typ := localed_type;
values[i].section := res[2];
values[i].category := res[3];
values[i].name := res[4];
for j := 1 to locale_count do
begin
values[i].val[j] := res[4 + j];
end;
end;
end;
procedure TForm1.ParseCommonValues(strbuff: String);
var
res: TColumns;
i: integer;
begin
res := ParseColumn(strbuff);
value_count := value_count + 1;
SetLength(values, value_count + 1);
SetLength(values[value_count].val, locale_count + 1);
values[value_count].typ := common_type;
values[value_count].section := res[2];
values[value_count].category := res[3];
values[value_count].name := res[4];
for i := 1 to locale_count do
begin
values[value_count].val[i] := res[4 + i];
end;
end;
procedure TForm1.SetAliasValues();
var
res: TColumns;
i, j, k: integer;
begin
for i := 1 to value_count do
for j := 1 to locale_count do
if ((values[i].val[j] = '') AND ((values[i].typ = localed_type))) then
begin
// LogAdd('name [] = ' + values[i].name);
// LogAdd('alias_locales = ' + alias_locales[j]);
for k := 1 to locale_count do
begin
// LogAdd('locales[' + IntToStr(k) + ']' + locales[k]);
if ((locales[k] = alias_locales[j]) AND (locales[k] <> '')) then
values[i].val[j] := values[i].val[k];
end;
end;
end;
function TForm1.ParseColumn(strbuff: String): TColumns;
var
res: TColumns;
str: string;
i, col_count: integer;
begin
col_count := 1;
for i := 1 to length(strbuff) do
begin
if (strbuff[i] <> chr(9)) then
if (strbuff[i] <> chr(39)) then
str := str + strbuff[i]
else
begin
if ((strbuff[i - 1] <> '\') and (i > 0)) then
str := str + '\' + strbuff[i]
else
str := str + strbuff[i];
end
else
begin
SetLength(res, col_count + 1);
res[col_count] := str;
str := '';
col_count := col_count + 1;
end;
SetLength(res, col_count + 1);
res[col_count] := str;
end;
// LogAdd('Locales: ' + strbuff);
Result := res;
end;
procedure TForm1.ButtonParseClick(Sender: TObject);
begin
if parseAdditional then
ParseSource()
else
LogAdd('File not setted!');
end;
procedure TForm1.ParseSource();
Var
strbuff: string;
begin
Reset(fileMemo);
While not eof(fileMemo) do
begin
Readln(fileMemo, strbuff);
ParseString(strbuff);
end;
CloseFile(fileMemo);
DeleteFile('_'+etFilename.Text);
Rename(fileMemo, '_'+etFilename.Text);
DeleteFile(etFilename.Text);
LogAdd('File parsed successfull.');
SetAliasValues();
LogAdd('Setted alias values for missing resources.');
end;
procedure TForm1.ButtonSaveClick(Sender: TObject);
begin
ValueDoXML();
end;
end.