-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJPL.LangMgr.pas
1152 lines (927 loc) · 32.5 KB
/
JPL.LangMgr.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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit JPL.LangMgr;
interface
{$I .\..\jp.inc}
{$IFDEF FPC}
{$MODE DELPHI}
{$IFNDEF HAS_RTTI}
This unit is only for FPC 3.2 or above
{$ENDIF}
{$WARN 5079 off : Unit "$1" is experimental}
{$ENDIF}
{$IFDEF DCC}
{$IFDEF DELPHI2009_OR_BELOW}
Unit for Delphi 2010 or newer!
{$ENDIF}
{$ENDIF}
uses
SysUtils, Classes, Generics.Collections, IniFiles, Rtti, TypInfo,
StdCtrls, ExtCtrls, ActnList, Menus,
Dialogs,
JPL.Strings, JPL.Conversion, JPL.IniFile, JPL.FileSearch, JPL.RTTI;
const
INVALID_COMPONENT_PROP_STR = '!45758679@@##INVALID##@@8989568!';
type
TLangIniListItem = record
FileName: string;
LangName_InEnglish: string;
LangName_Native: string;
function AsInfoStr(Indent: string = ' '): string;
end;
TLangIniListItems = TList<TLangIniListItem>;
{$region ' --- TLangIniList --- '}
TLangIniList = class
private
FItems: TLangIniListItems;
FIniInfoSectionName: string;
FIniKeyLangNameEn: string;
FIniKeyLangNameNative: string;
function GetCount: integer;
function GetItems(Index: integer): TLangIniListItem;
public
constructor Create;
destructor Destroy; override;
function AsInfoStr: string;
procedure AddFilesFromDir(const Dir: string; Ext: string = 'ini');
function TryGetLangIniListItem(LangName: string; out Item: TLangIniListItem): Boolean;
procedure Clear;
procedure GetLanguageNames_EN(const Strings: TStrings);
procedure GetLanguageNames_Native(const Strings: TStrings);
procedure GetLanguageNames_EnglishAndNative(const Strings: TStrings; const Separator: string = ' - ');
procedure GetLanguageFileNames(const Strings: TStrings);
property IniInfoSectionName: string read FIniInfoSectionName write FIniInfoSectionName;
property IniKeyLangNameEn: string read FIniKeyLangNameEn write FIniKeyLangNameEn;
property IniKeyLangNameNative: string read FIniKeyLangNameNative write FIniKeyLangNameNative;
property Count: integer read GetCount;
property Items[Index: integer]: TLangIniListItem read GetItems; default; // write SetItems; default;
end;
{$endregion TLangIniList}
TLangItemProperties = TDictionary<string,string>;
TLangComponentItem = record
private
procedure SetCaption(const Value: string);
function GetCaption: string;
public
Component: TComponent;
Properties: TLangItemProperties;
function AsInfoStr(Indent: string = ' '): string;
function AddProperty(const PropertyName, PropertyString: string): TLangComponentItem; overload;
function ap(const PropertyName, PropertyString: string): TLangComponentItem; overload; // As above, but shorter name
function AddProperty(const PropertyName: string): TLangComponentItem; overload;
function ap(const PropertyName: string): TLangComponentItem; overload; // As above, but shorter name
function AddCaption(const CaptionStr: string): TLangComponentItem;
function AddHint(const HintStr: string): TLangComponentItem;
function GetProperty(const PropertyName: string; Default: string = ''): string;
property Caption: string read GetCaption write SetCaption;
end;
// KEY: INI key name, VALUE: TLangComponentItem
TLangComponentItems = TDictionary<string,TLangComponentItem>;
TLangStringItems = TDictionary<string,string>;
{$region ' --- TLangSection --- '}
TLangSection = class
private
FLangStringItems: TLangStringItems;
FLangComponentItems: TLangComponentItems;
FSectionName: string;
FComponentNamePropertyNameSeparator: string;
procedure SetSectionName(const Value: string);
function GetStringItemsCount: integer;
function GetComponentItemsCount: integer;
function GetAllItemsCount: integer;
procedure SetComponentNamePropertyNameSeparator(const Value: string);
public
constructor Create(const SectionName: string);
destructor Destroy; override;
procedure Clear;
procedure LoadFromIniSection(const Ini: TJPIniFile);
procedure UpdateComponents;
procedure AddString(const KeyName, StrValue: string); overload; // AddString('Key', 'Value);
procedure AddString(const KeyNameAndValue: string); overload; // AddString('Key=Value');
function GetString(const KeyName: string; Default: string = ''): string;
function AddComponent(const ComponentName: string): TLangComponentItem; overload;
function AddComponent(Component: TComponent): TLangComponentItem; overload;
function AddComponent(Component: TComponent; const ACaption: string): TLangComponentItem; overload;
function AddComponent(Component: TComponent; const ACaption, AHint: string): TLangComponentItem; overload;
// ac = AddComponent (short name)
function ac(Component: TComponent): TLangComponentItem; overload;
function ac(Component: TComponent; const ACaption: string): TLangComponentItem; overload;
function ac(Component: TComponent; const ACaption, AHint: string): TLangComponentItem; overload;
function AddComponentWithCaption(Component: TComponent): TLangComponentItem;
function acc(Component: TComponent): TLangComponentItem; // calls AddComponentWithCaption
function AddComponentWithCaptionAndHint(Component: TComponent): TLangComponentItem;
function acch(Component: TComponent): TLangComponentItem; // calls AddComponentWithCaptionAndHint
function AddComponentWithBoundLabel(Component: TComponent): TLangComponentItem;
function AddLabel(const ALabel: TCustomLabel; bCaption: Boolean = True; bHint: Boolean = False): TLangComponentItem;
function AddStaticText(const StaticText: TStaticText; bCaption: Boolean = True; bHint: Boolean = False): TLangComponentItem;
function AddAction(const Action: TCustomAction; bCaption: Boolean = True; bHint: Boolean = True): TLangComponentItem;
function AddCheckBox(const CheckBox: TCheckBox; bCaption: Boolean = True; bHint: Boolean = True): TLangComponentItem;
procedure AddCheckBoxArray(Arr: array of TCheckBox; bCaption: Boolean = True; bHint: Boolean = True);
function AddRadioButton(const RadioButton: TRadioButton; bCaption: Boolean = True; bHint: Boolean = True): TLangComponentItem;
function AddLabeledEdit(const LabeledEdit: TCustomLabeledEdit; bLabelCaption: Boolean = True; bEditText: Boolean = False): TLangComponentItem;
function AddMenuItem(MenuItem: TMenuItem): TLangComponentItem;
function TryGetComponentItem(const KeyName: string; out Item: TLangComponentItem): Boolean;
function GetComponentProperty(const ComponentName, PropertyName: string; Default: string = ''): string;
function AsInfoStr: string;
property SectionName: string read FSectionName write SetSectionName;
property LangStringItems: TLangStringItems read FLangStringItems;
property LangComponentItems: TLangComponentItems read FLangComponentItems;
property StringItemsCount: integer read GetStringItemsCount;
property ComponentItemsCount: integer read GetComponentItemsCount;
property AllItemsCount: integer read GetAllItemsCount;
property ComponentNamePropertyNameSeparator: string read FComponentNamePropertyNameSeparator write SetComponentNamePropertyNameSeparator;
property StringItems: TLangStringItems read FLangStringItems;
end;
{$endregion TLangSection}
TLangSections = TList<TLangSection>;
{$region ' --- TLangMgr --- '}
TLangMgr = class
private
FLangIniList: TLangIniList;
FLangSections: TLangSections;
FComboBox: TComboBox;
FActiveLanguageIndex: integer;
function GetLanguageCount: integer;
function GetSection(Index: integer): TLangSection;
function GetSectionCount: integer;
procedure SetComboBox(const Value: TComboBox);
procedure LoadStringsFromFile(const IniFileName: string);
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function AsInfoStr: string;
procedure FillComboBox;
function ReloadSectionFromFile(const SectionName, IniFileName: string): Boolean;
procedure SetActiveLanguageByIndex(const Index: integer);
procedure SetActiveLanguageByIniFileName(const IniFileName: string);
procedure AddFilesFromDir(const Dir: string; Ext: string = 'ini');
procedure GetLanguageNames_EN(const Strings: TStrings);
procedure GetLanguageNames_Native(const Strings: TStrings);
procedure GetLanguageNames_EnglishAndNative(const Strings: TStrings; const Separator: string = ' - ');
procedure GetLanguageFileNames(const Strings: TStrings);
function GetLanguageFileNameByIndex(const Index: integer): string;
function GetLangIndexByFileName(const IniFileName: string): integer;
function AddSection(const SectionName: string): TLangSection;
function GetSectionByName(const SectionName: string): TLangSection;
procedure RemoveSection(LangSection: TLangSection);
function GetString(const SectionName, KeyName: string; Default: string = ''): string;
property LanguageCount: integer read GetLanguageCount;
property SectionCount: integer read GetSectionCount;
property Section[Index: integer]: TLangSection read GetSection; default;
property Sections: TLangSections read FLangSections;
property ComboBox: TComboBox read FComboBox write SetComboBox;
property ActiveLanguageIndex: integer read FActiveLanguageIndex;
end;
{$endregion TLangMgr}
function fixs(SrcStr: string; SubStr1: string = ''; SubStr2: string = ''): string;
implementation
function fixs(SrcStr: string; SubStr1: string = ''; SubStr2: string = ''): string;
begin
SrcStr := ReplaceAll(SrcStr, '\n', ENDL, True);
SrcStr := ReplaceAll(SrcStr, '\t', TAB, True);
//SrcStr := StringReplace(SrcStr, '\n', ENDL, [rfReplaceAll]);
//SrcStr := StringReplace(SrcStr, '\t', TAB, [rfReplaceAll]);
if SubStr1 <> '' then
begin
SrcStr := ReplaceFirst(SrcStr, '%s', SubStr1, True);
if SubStr2 <> '' then SrcStr := ReplaceFirst(SrcStr, '%s', SubStr2, True);
//SrcStr := StringReplace(SrcStr, '%s', SubStr1, []);
//if SubStr2 <> '' then SrcStr := StringReplace(SrcStr, '%s', SubStr2, []);
end;
Result := SrcStr;
end;
{$region ' ------------------------------- TLangSection ----------------------------- '}
constructor TLangSection.Create(const SectionName: string);
begin
inherited Create;
FSectionName := SectionName;
FComponentNamePropertyNameSeparator := '.';
FLangStringItems := TLangStringItems.Create;
FLangComponentItems := TLangComponentItems.Create;
end;
destructor TLangSection.Destroy;
var
Key: string;
lci: TLangComponentItem;
begin
FSectionName := '';
for Key in FLangComponentItems.Keys do
begin
lci := FLangComponentItems[Key];
if Assigned(lci.Properties) then lci.Properties.Free;
end;
FLangComponentItems.Free;
FLangStringItems.Free;
inherited;
end;
procedure TLangSection.Clear;
begin
FLangStringItems.Clear;
FLangComponentItems.Clear;
end;
function TLangSection.GetAllItemsCount: integer;
begin
Result := StringItemsCount + ComponentItemsCount;
end;
function TLangSection.GetComponentItemsCount: integer;
begin
Result := FLangComponentItems.Count;
end;
function TLangSection.GetComponentProperty(const ComponentName, PropertyName: string; Default: string): string;
var
Item: TLangComponentItem;
begin
Result := Default;
if not TryGetComponentItem(ComponentName, Item) then Exit;
Result := Item.GetProperty(PropertyName, Default);
end;
function TLangSection.GetString(const KeyName: string; Default: string = ''): string;
var
sKey, sDef: string;
begin
sDef := Default;
if SplitStr(KeyName, sKey, sDef, '=') then
begin
if not FLangStringItems.TryGetValue(sKey, Result) then Result := sDef;
end
else
if not FLangStringItems.TryGetValue(KeyName, Result) then Result := sDef;
Result := fixs(Result);
end;
function TLangSection.GetStringItemsCount: integer;
begin
Result := FLangStringItems.Count;
end;
procedure TLangSection.SetComponentNamePropertyNameSeparator(const Value: string);
begin
FComponentNamePropertyNameSeparator := Value;
end;
procedure TLangSection.SetSectionName(const Value: string);
begin
if FSectionName = Value then Exit;
FSectionName := Value;
end;
function TLangSection.TryGetComponentItem(const KeyName: string; out Item: TLangComponentItem): Boolean;
begin
Result := FLangComponentItems.TryGetValue(KeyName, Item);
end;
procedure TLangSection.LoadFromIniSection(const Ini: TJPIniFile); // const SectionName: string);
var
Section: TJPIniSection;
Key, CompName, PropName, PropValue, IniKey: string;
lci: TLangComponentItem;
begin
Section := Ini.GetSection(SectionName, False);
if not Assigned(Section) then Exit;
for Key in FLangStringItems.Keys do
begin
IniKey := Key;
if not Section.KeyExists(IniKey) then Continue;
PropValue := Section.ReadString(IniKey, '');
FLangStringItems[Key] := PropValue
end;
for Key in FLangComponentItems.Keys do
begin
lci := FLangComponentItems[Key];
CompName := Key;
for PropName in lci.Properties.Keys do
begin
IniKey := CompName + FComponentNamePropertyNameSeparator + PropName;
if not Section.KeyExists(IniKey) then Continue;
PropValue := Section.ReadString(IniKey, '');
lci.Properties.AddOrSetValue(PropName, PropValue);
end;
end;
end;
procedure TLangSection.UpdateComponents;
var
Key, PropName, PropValue, ObjName, PropName2: string;
lci: TLangComponentItem;
x: integer;
Obj: TObject;
begin
if FLangComponentItems.Count <= 0 then Exit;
for Key in FLangComponentItems.Keys do
begin
lci := FLangComponentItems[Key];
if not Assigned(lci.Component) then Continue;
if not Assigned(lci.Properties) then Continue;
for PropName in lci.Properties.Keys do
begin
PropValue := lci.Properties[PropName];
PropValue := fixs(PropValue);
x := Pos('.', PropName);
if x > 0 then
begin
ObjName := Copy(PropName, 1, x - 1);
PropName2 := Copy(PropName, x + 1, Length(PropName));
if TryGetPropertyAsObject(lci.Component, ObjName, Obj) then SetPropertyText(Obj, PropName2, PropValue);
end
else
SetPropertyText(lci.Component, PropName, PropValue);
end;
end;
end;
{$region ' Add component '}
function TLangSection.AddComponent(Component: TComponent): TLangComponentItem; // overload
var
Item: TLangComponentItem;
begin
if FLangComponentItems.ContainsKey(Component.Name) then Item := FLangComponentItems.Items[Component.Name]
else
begin
Item.Component := Component;
Item.Properties := TLangItemProperties.Create;
FLangComponentItems.AddOrSetValue(Component.Name, Item);
end;
Result := Item;
end;
function TLangSection.AddComponent(Component: TComponent; const ACaption: string): TLangComponentItem;
begin
Result := AddComponent(Component);
Result.AddCaption(ACaption);
end;
function TLangSection.AddComponent(Component: TComponent; const ACaption, AHint: string): TLangComponentItem;
begin
Result := AddComponent(Component);
Result.AddCaption(ACaption);
Result.AddHint(AHint);
end;
function TLangSection.AddComponent(const ComponentName: string): TLangComponentItem; // overload
var
Item: TLangComponentItem;
begin
if FLangComponentItems.ContainsKey(ComponentName) then Item := FLangComponentItems.Items[ComponentName]
else
begin
Item.Component := nil;
Item.Properties := TLangItemProperties.Create;
FLangComponentItems.AddOrSetValue(ComponentName, Item);
end;
Result := Item;
end;
function TLangSection.ac(Component: TComponent): TLangComponentItem;
begin
Result := AddComponent(Component);
end;
function TLangSection.ac(Component: TComponent; const ACaption: string): TLangComponentItem;
begin
Result := AddComponent(Component, ACaption);
end;
function TLangSection.ac(Component: TComponent; const ACaption, AHint: string): TLangComponentItem;
begin
Result := AddComponent(Component, ACaption, AHint);
end;
function TLangSection.AddComponentWithCaption(Component: TComponent): TLangComponentItem;
var
s: string;
begin
Result := AddComponent(Component);
s := GetPropertyText(Component, 'Caption', INVALID_COMPONENT_PROP_STR);
if s <> INVALID_COMPONENT_PROP_STR then Result.AddCaption(s);
end;
function TLangSection.acc(Component: TComponent): TLangComponentItem;
begin
Result := AddComponentWithCaption(Component);
end;
function TLangSection.AddComponentWithCaptionAndHint(Component: TComponent): TLangComponentItem;
var
s: string;
begin
Result := AddComponent(Component);
s := GetPropertyText(Component, 'Caption', INVALID_COMPONENT_PROP_STR);
if s <> INVALID_COMPONENT_PROP_STR then Result.AddCaption(s);
s := GetPropertyText(Component, 'Hint', INVALID_COMPONENT_PROP_STR);
if s <> INVALID_COMPONENT_PROP_STR then Result.AddHint(s);
end;
function TLangSection.acch(Component: TComponent): TLangComponentItem;
begin
Result := AddComponentWithCaptionAndHint(Component);
end;
function TLangSection.AddComponentWithBoundLabel(Component: TComponent): TLangComponentItem;
var
s: string;
BoundLabelObj: TObject;
begin
if TryGetPropertyAsObject(Component, 'BoundLabel', BoundLabelObj) then s := GetPropertyText(BoundLabelObj, 'Caption', '')
else s := '';
Result := AddComponent(Component).AddProperty('BoundLabel.Caption', s);
end;
{$endregion Add component}
function TLangSection.AddAction(const Action: TCustomAction; bCaption: Boolean = True; bHint: Boolean = True): TLangComponentItem;
begin
Result := AddComponent(Action);
if bCaption then Result.AddProperty('Caption', Action.Caption);
if bHint then Result.AddProperty('Hint', Action.Hint);
end;
function TLangSection.AddLabel(const ALabel: TCustomLabel; bCaption: Boolean = True; bHint: Boolean = False): TLangComponentItem;
begin
Result := AddComponent(ALabel);
if bCaption then Result.AddProperty('Caption', ALabel.Caption);
if bHint then Result.AddProperty('Hint', ALabel.Hint);
end;
function TLangSection.AddStaticText(const StaticText: TStaticText; bCaption: Boolean = True; bHint: Boolean = False): TLangComponentItem;
begin
Result := AddComponent(StaticText);
if bCaption then Result.AddProperty('Caption', StaticText.Caption);
if bHint then Result.AddProperty('Hint', StaticText.Hint);
end;
function TLangSection.AddLabeledEdit(const LabeledEdit: TCustomLabeledEdit; bLabelCaption: Boolean; bEditText: Boolean): TLangComponentItem;
begin
Result := AddComponent(LabeledEdit);
if bLabelCaption then Result.AddProperty('EditLabel.Caption', LabeledEdit.EditLabel.Caption);
if bEditText then Result.AddProperty('Text', LabeledEdit.Text);
end;
function TLangSection.AddMenuItem(MenuItem: TMenuItem): TLangComponentItem;
begin
Result := AddComponent(MenuItem);
Result.AddProperty('Caption', MenuItem.Caption);
end;
function TLangSection.AddRadioButton(const RadioButton: TRadioButton; bCaption: Boolean; bHint: Boolean): TLangComponentItem;
begin
Result := AddComponent(RadioButton);
if bCaption then Result.AddProperty('Caption', RadioButton.Caption);
if bHint then Result.AddProperty('Hint', RadioButton.Hint);
end;
function TLangSection.AddCheckBox(const CheckBox: TCheckBox; bCaption: Boolean = True; bHint: Boolean = True): TLangComponentItem;
begin
Result := AddComponent(CheckBox);
if bCaption then Result.AddProperty('Caption', CheckBox.Caption);
if bHint then Result.AddProperty('Hint', CheckBox.Hint);
end;
procedure TLangSection.AddCheckBoxArray(Arr: array of TCheckBox; bCaption: Boolean = True; bHint: Boolean = True);
var
cb: TCheckBox;
i: integer;
begin
for i := 0 to Length(Arr) - 1 do
begin
cb := Arr[i];
AddCheckBox(cb, bCaption, bHint);
end;
end;
procedure TLangSection.AddString(const KeyName, StrValue: string);
begin
FLangStringItems.AddOrSetValue(KeyName, StrValue);
end;
procedure TLangSection.AddString(const KeyNameAndValue: string);
var
sKey, sValue: string;
begin
if not SplitStr(KeyNameAndValue, sKey, sValue, '=') then Exit;
AddString(sKey, sValue);
end;
function TLangSection.AsInfoStr: string;
var
s, Key, Value: string;
x: integer;
lci: TLangComponentItem;
begin
s := 'Section: ' + FSectionName + ENDL;
s := s + ENDL + 'String items: ' + itos(StringItemsCount) + ENDL;
x := 0;
for Key in FLangStringItems.Keys do
begin
Inc(x);
Value := FLangStringItems.Items[Key];
s := s + itos(x) + ': ' + Key + '=' + Value + ENDL;
end;
s := s + ENDL + 'Component items: ' + itos(ComponentItemsCount) + ENDL;
x := 0;
for Key in FLangComponentItems.Keys do
begin
Inc(x);
lci := FLangComponentItems.Items[Key];
s := s + 'Component ' + itos(x) + ENDL + lci.AsInfoStr(' ') + ENDL;
end;
Result := Trim(s);
end;
{$endregion TLangSection}
{$region ' ------------------- TLangComponentItem --------------------- '}
function TLangComponentItem.AddProperty(const PropertyName, PropertyString: string): TLangComponentItem;
begin
Result := Self;
if not Assigned(Properties) then Properties := TLangItemProperties.Create;
Properties.AddOrSetValue(PropertyName, PropertyString);
end;
function TLangComponentItem.ap(const PropertyName, PropertyString: string): TLangComponentItem;
begin
Result := AddProperty(PropertyName, PropertyString);
end;
function TLangComponentItem.AddProperty(const PropertyName: string): TLangComponentItem;
var
s, SubComponentName, SubcomponentPropertyName: string;
SubComponentObj: TObject;
x: integer;
begin
Result := Self;
if not Assigned(Self.Component) then Exit;
x := Pos('.', PropertyName);
if x > 0 then
begin
SubComponentName := Copy(PropertyName, 1, x - 1);
SubcomponentPropertyName := Copy(PropertyName, x + 1, Length(PropertyName));
if not TryGetPropertyAsObject(Self.Component, SubComponentName, SubComponentObj) then Exit;
s := GetPropertyText(SubComponentObj, SubcomponentPropertyName, INVALID_COMPONENT_PROP_STR);
end
else
s := GetPropertyText(Self.Component, PropertyName, INVALID_COMPONENT_PROP_STR);
if s <> INVALID_COMPONENT_PROP_STR then Result := AddProperty(PropertyName, s);
end;
function TLangComponentItem.ap(const PropertyName: string): TLangComponentItem;
begin
Result := AddProperty(PropertyName);
end;
function TLangComponentItem.AddCaption(const CaptionStr: string): TLangComponentItem;
begin
Result := AddProperty('Caption', CaptionStr);
end;
function TLangComponentItem.AddHint(const HintStr: string): TLangComponentItem;
begin
Result := AddProperty('Hint', HintStr);
end;
function TLangComponentItem.AsInfoStr(Indent: string): string;
var
s, Key, Value: string;
begin
if not Assigned(Component) then Exit('<component not assigned!>');
s := Indent + Component.Name + ': ' + Component.ClassName + ENDL;
if Assigned(Properties) then
begin
s := s + Indent + 'Properties: ' + itos(Properties.Count) + ENDL;
for Key in Properties.Keys do
begin
Value := Properties[Key];
s := s + Indent + Key + ' = ' + Value + ENDL;
end;
end;
Result := TrimRight(s);
end;
function TLangComponentItem.GetProperty(const PropertyName: string; Default: string): string;
begin
Result := Default;
if not Assigned(Properties) then Exit;
if not Properties.TryGetValue(PropertyName, Result) then Exit;
end;
function TLangComponentItem.GetCaption: string;
begin
Result := GetProperty('Caption');
end;
procedure TLangComponentItem.SetCaption(const Value: string);
begin
AddProperty('Caption', Value);
end;
{$endregion TLangComponentItem}
{$region ' ---------------------------------- TLangMgr ------------------------------------ '}
constructor TLangMgr.Create;
begin
FLangIniList := TLangIniList.Create;
FLangSections := TLangSections.Create; // TList<TLangSection>
FActiveLanguageIndex := -1;
FComboBox := nil;
end;
destructor TLangMgr.Destroy;
begin
Clear;
if Assigned(FLangIniList) then FLangIniList.Free;
if Assigned(FLangSections) then FLangSections.Free;
inherited;
end;
procedure TLangMgr.FillComboBox;
var
ChangeProc: TNotifyEvent;
begin
if not Assigned(FComboBox) then Exit;
ChangeProc := nil;
if Assigned(FComboBox.OnChange) then
begin
ChangeProc := FComboBox.OnChange;
FComboBox.OnChange := nil;
end;
GetLanguageNames_EnglishAndNative(FComboBox.Items, ' - ');
FComboBox.OnChange := ChangeProc;
end;
procedure TLangMgr.Clear;
var
i: integer;
begin
for i := 0 to FLangSections.Count - 1 do
if Assigned(Section[i]) then Section[i].Free;
end;
function TLangMgr.AddSection(const SectionName: string): TLangSection;
var
Section: TLangSection;
begin
Section := TLangSection.Create(SectionName);
FLangSections.Add(Section);
Result := Section;
end;
function TLangMgr.AsInfoStr: string;
const
Line: string = '===============================================';
var
s: string;
i: integer;
Section: TLangSection;
begin
s := 'Language count: ' + itos(LanguageCount);
s := s + ENDL + 'Sections: ' + itos(SectionCount) + ENDL + Line;
s := s + ENDL + FLangIniList.AsInfoStr;
s := s + ENDL + Line + ENDL;
s := s + 'SECTIONS' + ENDL;
for i := 0 to FLangSections.Count - 1 do
begin
Section := FLangSections[i];
s := s + 'Section No ' + itos(i + 1) + ENDL;
s := s + Section.AsInfoStr + ENDL;
end;
Result := s;
end;
function TLangMgr.GetLangIndexByFileName(const IniFileName: string): integer;
var
i: integer;
UFileName: string;
begin
Result := -1;
UFileName := AnsiUpperCase(IniFileName);
for i := 0 to FLangIniList.Count - 1 do
if AnsiUpperCase(FLangIniList[i].FileName) = UFileName then
begin
Result := i;
Break;
end;
end;
function TLangMgr.GetLanguageCount: integer;
begin
Result := FLangIniList.Count;
end;
procedure TLangMgr.GetLanguageFileNames(const Strings: TStrings);
begin
FLangIniList.GetLanguageFileNames(Strings);
end;
procedure TLangMgr.GetLanguageNames_EN(const Strings: TStrings);
begin
FLangIniList.GetLanguageNames_EN(Strings);
end;
procedure TLangMgr.GetLanguageNames_Native(const Strings: TStrings);
begin
FLangIniList.GetLanguageNames_Native(Strings);
end;
procedure TLangMgr.GetLanguageNames_EnglishAndNative(const Strings: TStrings; const Separator: string = ' - ');
begin
FLangIniList.GetLanguageNames_EnglishAndNative(Strings, Separator);
end;
function TLangMgr.GetSection(Index: integer): TLangSection;
begin
Result := FLangSections.Items[Index];
end;
function TLangMgr.GetSectionByName(const SectionName: string): TLangSection;
var
UName: string;
Section: TLangSection;
begin
Result := nil;
UName := AnsiUpperCase(SectionName);
for Section in FLangSections do
if AnsiUpperCase(Section.SectionName) = UName then
begin
Result := Section;
Break;
end;
end;
procedure TLangMgr.RemoveSection(LangSection: TLangSection);
begin
FLangSections.Remove(LangSection);
end;
function TLangMgr.GetSectionCount: integer;
begin
Result := FLangSections.Count;
end;
function TLangMgr.GetString(const SectionName, KeyName: string; Default: string): string;
var
Section: TLangSection;
begin
Result := Default;
Section := GetSectionByName(SectionName);
if not Assigned(Section) then Exit;
Result := Section.GetString(KeyName, Default);
end;
procedure TLangMgr.LoadStringsFromFile(const IniFileName: string);
var
Ini: TJPIniFile;
Section: TLangSection;
begin
if not FileExists(IniFileName) then Exit;
Ini := TJPIniFile.Create(IniFileName, TEncoding.UTF8);
try
Ini.UpdateFileOnExit := False;
for Section in FLangSections do
begin
Section.LoadFromIniSection(Ini);
Section.UpdateComponents;
end;
Ini.UpdateFileOnExit := False;
finally
Ini.Free;
end;
end;
function TLangMgr.ReloadSectionFromFile(const SectionName, IniFileName: string): Boolean;
var
Ini: TJPIniFile;
Section: TLangSection;
begin
Result := False;
if not FileExists(IniFileName) then Exit;
Ini := TJPIniFile.Create(IniFileName, TEncoding.UTF8);
try
Ini.UpdateFileOnExit := False;
for Section in FLangSections do
if Section.SectionName = SectionName then
begin
Section.LoadFromIniSection(Ini);
Section.UpdateComponents;
Result := True;
Break;
end;
Ini.UpdateFileOnExit := False;
finally
Ini.Free;
end;
end;
function TLangMgr.GetLanguageFileNameByIndex(const Index: integer): string;
begin
if Index in [0..FLangIniList.Count - 1] then Result := FLangIniList[Index].FileName
else Result := '';
end;
procedure TLangMgr.SetActiveLanguageByIndex(const Index: integer);
begin
if Index in [0..FLangIniList.Count - 1] then
begin
FActiveLanguageIndex := Index;
LoadStringsFromFile(FLangIniList[Index].FileName);
end
else
FActiveLanguageIndex := -1;
end;
procedure TLangMgr.SetActiveLanguageByIniFileName(const IniFileName: string);
var
x: integer;
begin
x := GetLangIndexByFileName(IniFileName);
SetActiveLanguageByIndex(x);
end;
procedure TLangMgr.SetComboBox(const Value: TComboBox);
begin
if FComboBox = Value then Exit;
FComboBox := Value;
end;
procedure TLangMgr.AddFilesFromDir(const Dir: string; Ext: string);
begin
FLangIniList.AddFilesFromDir(Dir, Ext);
FillComboBox;
end;
{$endregion TLangMgr}
{$region ' ------------------------------ TLangIniList ----------------------------------- '}
constructor TLangIniList.Create;
begin
inherited Create;
FIniInfoSectionName := 'Info';
FIniKeyLangNameEn := 'LangName_InEnglish';
FIniKeyLangNameNative := 'LangName_Native';
FItems := TLangIniListItems.Create;
end;
destructor TLangIniList.Destroy;
begin
FItems.Free;
inherited;
end;
function TLangIniList.AsInfoStr: string;
var
i: integer;
lili: TLangIniListItem;
begin
Result :=