-
Notifications
You must be signed in to change notification settings - Fork 5
/
LangsU.pas
397 lines (351 loc) · 13.7 KB
/
LangsU.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
unit LangsU;
interface
uses Vcl.Forms, System.Classes, System.Generics.Collections, Vcl.Menus, System.IniFiles;
procedure GenDefaultFileLang;
function GetLangString(const ASection, AString: string): string;
procedure SetLang(const ALangCode: string);
//get Index for AmiLang.Items with gen default LCID
function LangFillListAndGetCurrent(const AMainIniFile: TIniFile; const AMenu: TPopupMenu;
const AmiLang: TMenuItem; const AOnClick: TNotifyEvent): Integer;
// get Index with gen default LCID
function AskForConfirmation(const AForm: TForm;
const AConfirmation: string): Boolean;
function AskForDeletion(const AForm: TForm; const ACaption: string): Boolean;
procedure ErrorDialog(const AForm: TForm; const ACaption: string);
implementation
uses System.SysUtils, System.TypInfo,
System.StrUtils, Vcl.StdCtrls, Vcl.Controls, Vcl.ExtCtrls, Vcl.ActnList,
Vcl.Dialogs, System.Generics.Defaults, Winapi.Windows, System.IOUtils;
const
cLangFolderName = 'Langs';
var
FDefLangFile: TMemIniFile = nil;
FLangFile : TMemIniFile = nil;
FLangPath : string; // Path to Lang folder in app
const
ExcludesForFormConfig: TArray<string> = ['btnClose', 'lblVer'];
ExcludesForFrameCommandConfig: TArray<string> = ['gbRunAtTime',
'lblisRun_FolderChanged', 'lblNextRun', 'cbRunAt', 'cbIsRepeatRun',
'cbisRun_isWhenFolderChange', 'cbIsVisible', 'lblIsRunning'];
procedure LangAddDefaultStrings(const AForcedWrite: Boolean); forward;
procedure GenDefaultFileLang;
procedure WriteToLangFile(const ASectionName: string; AIdentPrefix: string;
AComponent: TComponent);
var
i, FPropCount: Integer;
TypeData: PTypeData;
FPropList: PPropList;
FProp: PPropInfo;
sDataToSave: string;
begin
if (AComponent is TAction) then
Exit;
if AIdentPrefix <> '' then
AIdentPrefix := AIdentPrefix + '.';
TypeData := GetTypeData(AComponent.ClassInfo);
FPropCount := TypeData.PropCount;
GetMem(FPropList, SizeOf(PPropInfo) * FPropCount);
try
GetPropInfos(AComponent.ClassInfo, FPropList);
for i := 0 to FPropCount - 1 do
begin
FProp := FPropList[i];
if((FProp.PropType^.Kind = tkClass) and (FProp.Name <> 'FocusControl')) then
begin
var vComponent := TComponent(GetObjectProp(AComponent, FProp));
if Assigned(vComponent) then
WriteToLangFile(ASectionName, AIdentPrefix + String(FProp.Name), vComponent)
end
else if (FProp.PropType^.Kind = tkUString) and (FProp.Name <> 'Name') and
not((AComponent is TFileOpenDialog) and
(FProp.Name = 'DefaultExtension')) then
begin
sDataToSave := GetStrProp(AComponent, FProp);
if (sDataToSave <> '') and
not((AComponent is TMenuItem) and (FProp.Name = 'Caption') and
(sDataToSave = '-')) then
FLangFile.WriteString(ASectionName,
AIdentPrefix + String(FProp.Name), sDataToSave);
end;
end; // for i .. FPropCount-1
finally
FreeMem(FPropList, SizeOf(PPropInfo) * FPropCount);
end;
end;
procedure WriteComponents(const ASectionName: string;
AFormOrFrame: TScrollingWinControl);
var
viCompoment: Integer;
vComponent: TComponent;
begin
for viCompoment := 0 to AFormOrFrame.ComponentCount - 1 do
begin
vComponent := AFormOrFrame.Components[viCompoment];
if not(vComponent is TFrame) then
begin // now only hardcode
if not(((AFormOrFrame.Name = 'frmCommandConfig') and
MatchStr(vComponent.Name, ExcludesForFrameCommandConfig)) or
((AFormOrFrame.Name = 'frmConfig') and MatchStr(vComponent.Name, ExcludesForFormConfig)))
then
WriteToLangFile(ASectionName, vComponent.Name, vComponent);
end
else
WriteComponents(ASectionName + '\' + vComponent.Name,
vComponent as TFrame)
end;
end;
var
vFileName: string;
viForm: Integer;
vForm: TForm;
begin
vFileName := FLangPath + 'Default.ini';
System.SysUtils.DeleteFile(vFileName);
FLangFile := TMemIniFile.Create(vFileName, System.SysUtils.TEncoding.UTF8);
with FLangFile do
begin
WriteString('LangProperties', '@LCID', '1033');
WriteString('LangProperties', '@Name', 'English - United States');
LangAddDefaultStrings(True);
for viForm := 0 to Screen.FormCount - 1 do
begin
vForm := Screen.Forms[viForm];
WriteToLangFile(vForm.Name, '', vForm);
WriteComponents(vForm.Name, vForm);
end;
UpdateFile;
end; // with
FDefLangFile := TMemIniFile.Create(TMemoryStream.Create);
var vStringList := TStringList.Create;
FLangFile.GetStrings(vStringList);
FDefLangFile.SetStrings(vStringList);
end;
function GetLangString(const ASection, AString: string): string;
begin
Result := FLangFile.ReadString(ASection, '@' + AString, '');
end;
procedure SetLang(const ALangCode: string);
procedure ReadFromLangFile(const ASectionName, AIdentPrefix: string;
const AFormOrFrame: TScrollingWinControl);
begin
var vSection := TStringList.Create;
FLangFile.ReadSection(ASectionName, vSection);
for var SectionIndex := 0 to vSection.Count - 1 do
begin
var vPropertyName: string := vSection[SectionIndex];
if vPropertyName[1] = '@' then
Continue; // it's not a property
var vPropertyValue: string := FLangFile.ReadString(ASectionName, vPropertyName, '');
if vPropertyValue = '' then
Continue; //default string
var vComponent: TComponent := AFormOrFrame;
var DelimetedPropertyNames: TArray<string> := vPropertyName.Split(['.']);
for var TextIndex := Low(DelimetedPropertyNames) to High(DelimetedPropertyNames) do
begin
vPropertyName := DelimetedPropertyNames[TextIndex];
if TextIndex <> High(DelimetedPropertyNames) then
begin
if not ((vComponent is TForm) or (vComponent is TFrame)) then
try
vComponent := TComponent(GetObjectProp(vComponent, vPropertyName))
except
on EPropertyError do
break;
end
else
vComponent := vComponent.FindComponent(vPropertyName);
end
else
try
SetStrProp(vComponent, vPropertyName, vPropertyValue);
except // nothing
end;
end;
end;
end;
var
vLangFileName: string;
viSection: Integer;
vSections: TStringList;
vFormName: string;
DelimetedSectionName: TArray<string>;
vForm, vFrame: TComponent;
vFrameFound: Boolean; // delimiter \ for frame
begin
vLangFileName := FLangPath + ALangCode + '.ini';
if not FileExists(vLangFileName) then
raise Exception.CreateFmt('Language file "%s" is not found',
[vLangFileName]);
FreeAndNil(FLangFile); // prev lang ini
FLangFile := TMemIniFile.Create(vLangFileName, System.SysUtils.TEncoding.UTF8);
vSections := TStringList.Create;
// Default strings
LangAddDefaultStrings(False);
if ALangCode.ToLower <> 'default' then
begin
FDefLangFile.ReadSections(vSections);
for var i := 0 to vSections.Count - 1 do
begin
var vSectionName := vSections[i];
var vSectionKeys := TStringList.Create;
FDefLangFile.ReadSection(vSectionName, vSectionKeys);
for var j := 0 to vSectionKeys.Count - 1 do
begin
var vSectionKey := vSectionKeys[j];
if not FLangFile.ValueExists(vSectionName, vSectionKey) then
FLangFile.WriteString(vSectionName, vSectionKey,
FDefLangFile.ReadString(vSectionName, vSectionKey, ''));
end;
end;
end;
with FLangFile do
begin
ReadSections(vSections);
for viSection := 0 to vSections.Count - 1 do
begin
var vSectionName := vSections[viSection];
if (vSectionName <> 'LangStrings') and (vSectionName <> 'LangProperties') then
begin
DelimetedSectionName := vSectionName.Split(['\'], 2);
vFrameFound := Length(DelimetedSectionName) = 2;
vFormName := DelimetedSectionName[0];
vForm := Application.FindComponent(vFormName);
if not Assigned(vForm) then
Continue;
if not vFrameFound then
ReadFromLangFile(vFormName, '', vForm as TForm)
else // must be frame
begin
vFrame := (vForm as TForm).FindComponent(DelimetedSectionName[1]);
if not Assigned(vFrame) or not(vFrame is TFrame) then
Continue;
ReadFromLangFile(vSectionName, '', vFrame as TFrame)
end;
end;
end;
if Modified then
UpdateFile;
end;
with TIniFile.Create(ChangeFileExt(ParamStr(0), '.ini')) do
try
WriteString('Main', 'LangCode', ALangCode);
finally
Free;
end;
end;
// Add default strings. AForcedWrite - without check (for the first time in the app)
procedure LangAddDefaultStrings(const AForcedWrite: Boolean);
procedure MyWriteString(const ASection, AIdent, AValue: string);
begin
if AForcedWrite or not FLangFile.ValueExists(ASection, AIdent) then
FLangFile.WriteString(ASection, AIdent, AValue);
end;
begin
MyWriteString('LangStrings', '@Cancel', 'Cancel');
MyWriteString('LangStrings', '@Close', 'Close');
MyWriteString('LangStrings', '@DeleteConfirm',
'Are you sure to delete "%s"?');
MyWriteString('LangStrings', '@CancelConfirm',
'Are you sure to cancel changes?');
MyWriteString('LangStrings', '@FileDialogExecutableFile', 'Executable file');
MyWriteString('LangStrings', '@FileDialogAnyFile', 'Any file');
MyWriteString('frmConfig', '@Version', 'Version:');
MyWriteString('frmConfig', '@VersionHint', 'Open the StartFromTray project website');
MyWriteString('frmConfig\frmCommandConfig', '@IsRunning', 'Running');
MyWriteString('frmConfig\frmCommandConfig', '@IsNotRunning', 'Not running');
MyWriteString('frmConfig\frmCommandConfig', '@ErrorEmptyName', 'Empty name. You have to write one');
MyWriteString('frmConfig\frmCommandConfig', '@ErrorCommand', 'Empty command (file to run). You have to write it');
MyWriteString('frmConfig\frmCommandConfig', '@FileDialogTitle', 'Browsing file to run');
MyWriteString('frmConfig\frmCommandConfig', '@FolderDialogTitle', 'Browsing folder to run');
MyWriteString('frmExtensions', '@ActionForEdit', 'Action for <b>Edit</b>');
MyWriteString('frmExtensions', '@ActionForRun', 'Action for <b>Run</b>');
MyWriteString('frmExtensions', '@ChooseFileForRun',
'Choose file for Run action');
MyWriteString('frmExtensions', '@ChooseFileForEdit',
'Choose file for Edit action');
MyWriteString('frmExtensions', '@ErrorEmptyName',
'Empty name. You have to write one');
MyWriteString('frmExtensions', '@ErrorEmptyExtensions',
'Empty extensions. You need at least one.');
end;
function LangFillListAndGetCurrent(const AMainIniFile: TIniFile; const AMenu: TPopupMenu;
const AmiLang: TMenuItem; const AOnClick: TNotifyEvent): Integer;
procedure AddSubMenuItem(const ALangName, ALangCode: string);
begin
var vMenuItem := TMenuItem.Create(AMenu);
with vMenuItem do
begin
Caption := ALangName;
Tag := Integer(StrNew(PChar(ALangCode)));
RadioItem := True;
OnClick := AOnClick;
end;
AmiLang.Add(vMenuItem);
end;
begin
Result := 0; // LCID for user not found
var vUserDefaultLCID := GetUserDefaultUILanguage(); // GetUserDefaultLCID();
var vCurrentIniLangCode: string := AMainIniFile.ReadString('Main', 'LangCode', '');
AddSubMenuItem('Default - English', 'Default');
var vCurrentItemIndexForIniLang: Integer;
if vCurrentIniLangCode = 'Default' then
vCurrentItemIndexForIniLang := 0
else
vCurrentItemIndexForIniLang := -1;
var vCurrentItemIndexForLCID: Integer;
if StrToUIntDef(GetLangString('LangProperties', 'LCID'), 0) = vUserDefaultLCID
then
vCurrentItemIndexForLCID := 0
else
vCurrentItemIndexForLCID := -1;
var vSR: TSearchRec; var vCurrentItemIndex: Integer := 1;
if FindFirst(FLangPath + '???.ini', faNormal, vSR) = 0 then
begin
repeat
var sLangCode := TPath.GetFileNameWithoutExtension(vSR.Name);
with TMemIniFile.Create(FLangPath + vSR.Name, System.SysUtils.TEncoding.UTF8) do
try
var sLangCaption := ReadString('LangProperties', '@Name', '');
if sLangCaption <> '' then
begin
if (vCurrentItemIndexForLCID = -1) and
(LCID(ReadInteger('LangProperties', '@LCID', 0)) = vUserDefaultLCID) then
vCurrentItemIndexForLCID := vCurrentItemIndex;
if (vCurrentItemIndexForIniLang = -1) and
(sLangCode = vCurrentIniLangCode) then
vCurrentItemIndexForIniLang := vCurrentItemIndex;
AddSubMenuItem(sLangCaption, sLangCode);
end;
finally
Free;
end;
vCurrentItemIndex := vCurrentItemIndex + 1;
until (FindNext(vSR) <> 0);
System.SysUtils.FindClose(vSR);
end;
// choose the best match
if vCurrentItemIndexForIniLang >= 0 then
Result := vCurrentItemIndexForIniLang
else if vCurrentItemIndexForLCID >= 0 then
Result := vCurrentItemIndexForLCID;
end;
function AskForConfirmation(const AForm: TForm;
const AConfirmation: string): Boolean;
begin
Result := MessageBoxEx(AForm.Handle, PChar(AConfirmation),
PChar(AForm.Caption), MB_ICONWARNING or MB_YESNO or MB_DEFBUTTON2,
StrToIntDef(GetLangString('LangProperties', 'LCID'), 0)) = mrYes;
end;
function AskForDeletion(const AForm: TForm; const ACaption: string): Boolean;
begin
Result := AskForConfirmation(AForm,
Format(GetLangString('LangStrings', 'DeleteConfirm'), [ACaption]));
end;
procedure ErrorDialog(const AForm: TForm; const ACaption: string);
begin
MessageBoxEx(AForm.Handle, PChar(ACaption),
PChar(AForm.Caption), MB_ICONERROR, StrToIntDef(GetLangString('LangProperties', 'LCID'), 0))
end;
initialization
FLangPath := ExtractFilePath(ParamStr(0)) + cLangFolderName + '\';
end.