-
Notifications
You must be signed in to change notification settings - Fork 11
/
fTLangTool.pas
434 lines (385 loc) · 12.2 KB
/
fTLangTool.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
unit fTLangTool; // TFormTLangTool
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Graphics, FMX.Forms, FMX.Dialogs, FMX.TabControl, System.Actions, FMX.ActnList,
FMX.Objects, FMX.StdCtrls, System.Rtti, FMX.Grid.Style, FMX.Grid, Fmx.Platform, FMX.Styles,
FMX.ScrollBox, FMX.Layouts, FMX.Controls.Presentation, FMX.Edit;
type
TFormTLangTool = class(TForm)
ActionList1: TActionList;
PreviousTabAction1: TPreviousTabAction;
TitleAction: TControlAction;
NextTabAction1: TNextTabAction;
TopToolBar: TToolBar;
btnBack: TSpeedButton;
ToolBarLabel: TLabel;
btnNext: TSpeedButton;
TabControl1: TTabControl;
TabItem1: TTabItem;
TabItem2: TTabItem;
BottomToolBar: TToolBar;
layoutTopLangPg1: TLayout;
TransGrid: TStringGrid;
StringColumn1: TStringColumn;
StringColumn2: TStringColumn;
StringColumn3: TStringColumn;
StringColumn4: TStringColumn;
StringColumn5: TStringColumn;
StringColumn6: TStringColumn;
StringColumn7: TStringColumn;
btnLoadLngFile: TButton;
Lang1: TLang;
btnPasteLanguage: TButton;
SaveDialog1: TSaveDialog;
OpenDialog1: TOpenDialog;
btnSaveFileAs: TButton;
btnClearAll: TButton;
StringColumn8: TStringColumn;
StringColumn9: TStringColumn;
StringColumn10: TStringColumn;
StringColumn11: TStringColumn;
StringColumn12: TStringColumn;
StringColumn13: TStringColumn;
StringColumn14: TStringColumn;
StringColumn15: TStringColumn;
btnAddLanguage: TButton;
btnCopyTextsToClipboard: TButton;
rectAboutLangTool: TRectangle;
labVersion: TLabel;
Label1: TLabel;
linkGit: TLabel;
Label2: TLabel;
Label3: TLabel;
btnDelLanguage: TButton;
labHeaderLangTool: TLabel;
btnTestTrans: TButton;
btnOk: TButton;
btnCancel: TButton;
btnOpenOldEditor: TButton;
edTranslationTest: TEdit;
labVersion2: TLabel;
procedure FormCreate(Sender: TObject);
procedure TitleActionUpdate(Sender: TObject);
procedure FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
procedure btnLoadLngFileClick(Sender: TObject);
procedure btnSaveFileAsClick(Sender: TObject);
procedure btnClearAllClick(Sender: TObject);
procedure btnAddLanguageClick(Sender: TObject);
procedure btnCopyTextsToClipboardClick(Sender: TObject);
procedure btnPasteLanguageClick(Sender: TObject);
procedure btnDelLanguageClick(Sender: TObject);
procedure btnTestTransClick(Sender: TObject);
procedure btnOpenOldEditorClick(Sender: TObject);
private
procedure ClearLang1;
procedure ClearGrid;
procedure DoClearAll;
procedure DoAddLanguage(const aLang: String);
procedure DoDeleteColumn;
procedure LoadNativeLanguage;
public
procedure populateGridWithLanguages;
procedure copyGridToLang1;
end;
var
FormTLangTool: TFormTLangTool;
implementation
uses
omNativeLanguage; //
{$R *.fmx}
function TryGetClipboardService(out _clp: IFMXClipboardService): boolean;
begin
Result := TPlatformServices.Current.SupportsPlatformService(IFMXClipboardService);
if Result then
_clp := IFMXClipboardService(TPlatformServices.Current.GetPlatformService(IFMXClipboardService));
end;
procedure StringToClipboard(const _s: string);
var
clp: IFMXClipboardService;
begin
if TryGetClipboardService(clp) then
clp.SetClipboard(_s);
end;
procedure StringFromClipboard(out _s: string);
var
clp: IFMXClipboardService;
Value: TValue;
s: string;
begin
if TryGetClipboardService(clp) then begin
Value := clp.GetClipboard;
if not Value.TryAsType(_s) then
_s := '';
end;
end;
{ TFormTLangTool }
procedure TFormTLangTool.FormCreate(Sender: TObject);
var aLang:String;
begin
{ This defines the default active tab at runtime }
TabControl1.First(TTabTransition.None);
LoadNativeLanguage;
end;
procedure TFormTLangTool.LoadNativeLanguage;
var aLang:String;
begin
// This app is not localized at this time, but this is how you get the
// navive language and localize your form
aLang := Copy(NativeLanguage,1,2); // like 'en' or 'pt' get native language ( drop coutry part )
if (CompareText( aLang,'en')<>0) then // en is default
begin
LoadLangFromStrings( Lang1.LangStr[aLang] ); // load language from resources, if available
// LocalizeDialogButtons;
end;
end;
procedure TFormTLangTool.TitleActionUpdate(Sender: TObject);
begin
if Sender is TCustomAction then
begin
if TabControl1.ActiveTab <> nil then
TCustomAction(Sender).Text := TabControl1.ActiveTab.Text
else
TCustomAction(Sender).Text := '';
end;
end;
procedure TFormTLangTool.populateGridWithLanguages;
var
len: Cardinal;
i,j: Integer;
r,c:integer;
S,aTrans,sLang,aOrig:String;
begin
// Sep := #9; //csv w/ tab
ClearGrid;
len := Lang1.Resources.Count; // number of languages
S := 'en'; // Original language = 'en'
c:=0; r:=0; // col row
TransGrid.Columns[0].Header := S; // 1st col= Original texts
for j := 0 to Lang1.Original.Count-1 do
TransGrid.Cells[0,j] := Lang1.Original.Strings[j];
for i:=0 to len-1 do //for each lang, build column
begin
c := i+1;
sLang := Lang1.Resources[i]; // sLang tipo 'pt'
// TransGrid.Cells[c,0] := sLang; // header da lingua
TransGrid.Columns[c].Header := sLang; //
//
LoadLangFromStrings( Lang1.LangStr[sLang]); // load language strings if available
for j:=0 to Lang1.Original.Count-1 do
begin
aOrig := Lang1.Original.Strings[j];
aTrans := Translate(aOrig);
if (aTrans=aOrig) then aTrans:='';
TransGrid.Cells[c,j] := aTrans;
end;
end;
end;
procedure TFormTLangTool.btnAddLanguageClick(Sender: TObject);
begin
InputBox('New Lang','Enter language code (2 chars):','',
procedure(const AResult: TModalResult; const AValue: string)
begin
if (AResult=mrOK) then
DoAddLanguage(AValue);
end
);
end;
procedure TFormTLangTool.DoAddLanguage(const aLang:String);
var c:integer; aCol:TColumn;
begin
for c:=1 to TransGrid.ColumnCount-1 do
begin
aCol := TransGrid.Columns[c];
if aCol.Header='' then
begin
aCol.Header := aLang;
exit;
end;
end;
end;
procedure TFormTLangTool.btnClearAllClick(Sender: TObject);
begin
MessageDlg( 'Clear all?',
System.UITypes.TMsgDlgType.mtConfirmation,
[TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], 0,
procedure(const AResult: TModalResult) //non modal msg box for Android
begin
case AResult of
mrYes: DoClearAll; // pressed yes, delete it
mrNo: ;
end;
end
);
end;
procedure TFormTLangTool.btnCopyTextsToClipboardClick(Sender: TObject);
var S,aText:String; r:integer;
const crlf=#13#10;
begin
S:='';
for r :=0 to TransGrid.RowCount-1 do
begin
aText := TransGrid.Cells[0,r]; // col 0 has the originals
if (aText<>'') then
S := S+aText+crlf;
end;
StringToClipboard(S);
end;
procedure TFormTLangTool.btnDelLanguageClick(Sender: TObject);
var c:integer; aCol:TColumn;
begin
c := TransGrid.ColumnIndex;
if (c>0) then
begin
aCol := TransGrid.Columns[c];
MessageDlg( 'Delete column '+aCol.Header+'?',
System.UITypes.TMsgDlgType.mtConfirmation,
[TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], 0,
procedure(const AResult: TModalResult) //non modal msg box for Android
begin
case AResult of
mrYes: DoDeleteColumn; // pressed yes, delete it
mrNo: ;
end;
end
);
end;
end;
procedure TFormTLangTool.DoDeleteColumn;
var c:integer; aCol:TColumn;
begin
c := TransGrid.ColumnIndex;
aCol := TransGrid.Columns[c];
aCol.DisposeOf;
end;
procedure TFormTLangTool.DoClearAll; // clear grid and Lang1
var c:integer; aCol:TColumn;
begin
for c:=1 to TransGrid.ColumnCount-1 do //clear grid headers
begin
aCol := TransGrid.Columns[c];
aCol.Header := '';
end;
ClearGrid; // clear grid cells
ClearLang1; //
end;
procedure TFormTLangTool.btnLoadLngFileClick(Sender: TObject);
var fn,s:String;
begin
if OpenDialog1.Execute then
begin
fn := OpenDialog1.FileName;
Lang1.LoadFromFile(fn);
populateGridWithLanguages; // Lang1 --> Grid
s := 'Loaded '+IntToStr(Lang1.Original.Count)+' texts '+IntToStr(Lang1.Resources.Count)+' languages';
labHeaderLangTool.Text := s;
end;
end;
procedure TFormTLangTool.btnPasteLanguageClick(Sender: TObject);
var S,aText:String; r,c,arow,i:integer; SL:TStringList;
const crlf=#13#10;
begin
StringFromClipboard(S);
SL := TStringList.Create;
SL.Text := S;
c := TransGrid.ColumnIndex;
arow := TransGrid.Selected;
if (SL.Count>0) then
begin
for i := 0 to SL.Count-1 do
begin
S := SL.Strings[i];
TransGrid.Cells[c,arow+i] := S;
end;
end;
SL.Free;
end;
procedure TFormTLangTool.ClearLang1;
begin
Lang1.Lang := 'en'; //original
Lang1.Original.Clear;
// TODO: dispose Resources?
Lang1.Resources.Clear;
end;
procedure TFormTLangTool.ClearGrid;
var c, r: Integer;
begin
for c := 0 to Pred(TransGrid.ColumnCount) do
for r := 0 to Pred(TransGrid.RowCount) do
TransGrid.Cells[c, r] := '';
end;
procedure TFormTLangTool.copyGridToLang1; //tricky
var c,r:integer; aCol:TColumn; aText,aTrans,aLang,S:String; aStrings:TStrings;
begin
ClearLang1;
c:=0; //Originals
aCol := TransGrid.Columns[0];
for r :=0 to TransGrid.RowCount-1 do
begin
aText := TransGrid.Cells[c,r];
if (aText<>'') then
Lang1.Original.Add(aText); // populate Originals
end;
for c:=1 to TransGrid.ColumnCount-1 do
begin
aCol := TransGrid.Columns[c];
aLang := aCol.Header;
if (aLang<>'') then
begin
Lang1.AddLang(aLang); // this create a stringlist for each lang
aStrings := Lang1.LangStr[aLang];
for r:=0 to TransGrid.RowCount-1 do //for each text..
begin
aText := TransGrid.Cells[0,r];
if Trim(aText)='' then continue; //ignore empty original texts
aTrans := TransGrid.Cells[c,r]; //get translation
if (aTrans<>'') then //has tranlation for this text?
begin
S := aText+'='+aTrans; // add name=value line
aStrings.Add(S);
end;
end;
end;
end;
end;
procedure TFormTLangTool.btnSaveFileAsClick(Sender: TObject);
var fn:String;
begin
//
if (OpenDialog1.Filename<>'') then
SaveDialog1.Filename := OpenDialog1.Filename;
if SaveDialog1.Execute then
begin
fn := SaveDialog1.Filename;
copyGridToLang1;
Lang1.SaveToFile(fn);
end;
end;
procedure TFormTLangTool.btnOpenOldEditorClick(Sender: TObject);
// var s:string; aOldEditor:TComponentEditor;
begin
// if Assigned(PrevEditorClass) then
// begin
// s := 'PrevEditorClass ok:';
// aOldEditor := TComponentEditor(PrevEditorClass.Create(Lang1,{ADesigner:}nil ));
// if Assigned(aOldEditor) then aOldEditor.Edit;
// end
// else s := 'PrevEditorClass nil';
// labHeaderLangTool.Text := s;
end;
procedure TFormTLangTool.btnTestTransClick(Sender: TObject);
var s:String;
begin
LoadNativeLanguage;
s := Translate(edTranslationTest.Text ); //test 'Yes'
labHeaderLangTool.Text := s;
end;
procedure TFormTLangTool.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
if (Key = vkHardwareBack) and (TabControl1.TabIndex <> 0) then
begin
TabControl1.First;
Key := 0;
end;
end;
end.