Menu

[bb41f4]: / nLangManager.pas  Maximize  Restore  History

Download this file

375 lines (342 with data), 8.4 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
//GNU LESSER GENERAL PUBLIC LICENSE - See lgpl.txt
unit nLangManager;
interface
uses
Classes, Windows, Messages, nCommon, nHash;
const
IM_GATHER_LANG = WM_USER + 100;
IM_APPLY_LANG = WM_USER + 101;
type
TLangState = (
lsBeginning, // at beginning, english language
lsNationLoaded, // in lngEng old strings in lngNation from file
lsAgainEnglish, // choosed English but lngEng,lngNation still created
lsBadFile);
TBaseManager = class
private
FNotifyList: TList;
public
constructor Create;
destructor Destroy; override;
procedure AddNotify(ANotify: TObject);
procedure RemoveNotify(ANotify: TObject);
{$ifdef LINUX}
{$else}
procedure Broadcast(idMsg: Cardinal; Param: Longint);
{$endif}
end;
TLangWing = class
private
FHashTable: THashTableAA;
FFilePath: UnicodeString;
FFileTime: {$ifdef LINUX}integer{$else}TFileTime{$endif};
procedure AddKeyVal(const line:AnsiString);
public
NotFoundKeys: TAnsiStringList;
constructor Create;
destructor Destroy; override;
function LoadFromFile(const filename: UnicodeString):boolean;
procedure Clear;
procedure add(const key,value: AnsiString);
function get(const key: AnsiString): UnicodeString;
function OtherFile(const NewFileName: UnicodeString): Boolean;
function FileChanged:boolean;
end;
TLangManager = class(TBaseManager)
private
FAskUser: boolean;
public
lngEng,lngNation: TLangWing;
lngUsed: TLangWing;
LangState: TLangState;
constructor Create;
destructor Destroy; override;
{$ifdef LINUX}
{$else}
procedure Load(const FileName: UnicodeString);
{$endif}
procedure Update(const FileName: UnicodeString);
property AskUser: boolean read FAskUser;
{$ifdef LINUX}
{$else}
procedure AddNotify(ANotify: TObject);
{$endif}
end;
var
LangManager: TLangManager;
implementation
uses
Sysutils;
{ TBaseManager }
procedure TBaseManager.AddNotify(ANotify: TObject);
begin
if FNotifyList.IndexOf(ANotify)<0 then
FNotifyList.Add(ANotify);
end;
procedure TBaseManager.Broadcast(idMsg: Cardinal; Param: Integer);
var
Message: TMessage;
i: integer;
begin
for i:=0 to FNotifyList.Count-1 do
begin
with Message do
begin
Msg := idMsg;
WParam := Param;
LParam := 0;
result := 0;
end;
TObject(FNotifyList[i]).Dispatch(Message);
end;
end;
constructor TBaseManager.Create;
begin
FNotifyList:=TList.Create;
end;
destructor TBaseManager.Destroy;
begin
FNotifyList.Free;
inherited;
end;
procedure TBaseManager.RemoveNotify(ANotify: TObject);
begin
FNotifyList.Extract(ANotify);
end;
{ TLangWing }
procedure TLangWing.add(const key, value: AnsiString);
begin
FHashTable.Put(key,value);
end;
procedure TLangWing.AddKeyVal(const line: AnsiString);
var
n:integer;
begin
if (line='') or (line[1]=';') then exit;//empty or comment
n:=Pos('=',line);
if n=0 then exit;
FHashTable.Put(Trim(Copy(line, 1, n-1)), PureString(Trim(Copy(line, n+1, Length(line)-n))));
end;
procedure TLangWing.Clear;
begin
FHashTable.Clear;
NotFoundKeys.Clear;
end;
constructor TLangWing.Create;
begin
FHashTable:=THashTableAA.Create(256);
NotFoundKeys:=TAnsiStringList.Create;
end;
destructor TLangWing.Destroy;
begin
FHashTable.Free;
NotFoundKeys.Free;
inherited;
end;
function TLangWing.FileChanged: boolean;
var
{$ifdef LINUX}
F: TSearchRec
{$endif}
{$ifdef MSWINDOWS}
FindDataW: TWIN32FindDataW;
FindHandle: THandle;
{$endif}
begin
{$ifdef LINUX}
F.ExcludeAttr := not Attr and faSpecial;
F.PathOnly := ExtractFileDirW(FFilePath);
F.Pattern := ExtractFileNameW(FFilePath);
if F.PathOnly = '' then
F.PathOnly := IncludeTrailingPathDelimiter(GetCurrentDirW);
F.FindHandle := opendir(PAnsiChar(AnsiString(F.PathOnly)));
if F.FindHandle <> nil then
begin
result := FindMatchingFile(F);
if result <> 0 then
FindClose(F);
result := FFileTime <> sr.Time;
end else result:=FFileTime<>0;
{$endif}
{$ifdef MSWINDOWS}
FindHandle := FindFirstFileW(PWideChar(FFilePath), FindDataW);
if FindHandle<>INVALID_HANDLE_VALUE then
begin
Windows.FindClose(FindHandle);
result := int64(FFileTime) <> int64(FindDataW.ftLastWriteTime);
end else result:=int64(FFileTime) <> 0;
{$endif}
end;
function TLangWing.get(const key: AnsiString): UnicodeString;
var
item: PHashItemAA;
begin
result:='';
item:=FHashTable.Get(key);
if item<>nil then
result:=Utf8Decode(item^.value)
else
if NotFoundKeys.IndexOf(key)<0 then NotFoundKeys.Add(key)
end;
function TLangWing.LoadFromFile(const filename: UnicodeString):boolean;
var
lines:TWideStringList;
isHeader:boolean; //before #
i:integer;
{$ifdef LINUX}
F: TSearchRec
{$endif}
{$ifdef MSWINDOWS}
FindDataW: TWIN32FindDataW;
FindHandle: THandle;
{$endif}
begin
result:=false;
Clear;
FFilePath:=ExpandWithBase(filename,AppDir,false);
{$ifdef LINUX}
F.ExcludeAttr := not Attr and faSpecial;
F.PathOnly := ExtractFileDirW(FFilePath);
F.Pattern := ExtractFileNameW(FFilePath);
if F.PathOnly = '' then
F.PathOnly := IncludeTrailingPathDelimiter(GetCurrentDirW);
-- what about Unicode under Linux?
F.FindHandle := opendir(PAnsiChar(AnsiString(F.PathOnly)));
if F.FindHandle <> nil then
begin
result := FindMatchingFile(F);
if result <> 0 then
FindClose(F);
FFileTime := sr.Time;
end else
begin
FFileTime := 0;
exit;
end;
{$endif}
{$ifdef MSWINDOWS}
FindHandle := FindFirstFileW(PWideChar(FFilePath), FindDataW);
if FindHandle<>INVALID_HANDLE_VALUE then
begin
Windows.FindClose(FindHandle);
FFileTime := FindDataW.ftLastWriteTime;
end else
begin
int64(FFileTime) := 0;
exit;
end;
{$endif}
lines:=TWideStringList.Create;
lines.LoadFromFile(FFilePath);
isHeader:=true;
for i:=0 to lines.Count-1 do
begin
if isHeader then
begin
if lines[i][1]='#' then isHeader:=false;
continue;
end;
AddKeyVal(lines[i]);
end;
lines.Free;
result:=true;
end;
function TLangWing.OtherFile(const NewFileName: UnicodeString): Boolean;
begin
result:=WideCompareFileName(FFilePath, ExpandWithBase(NewFileName,AppDir,false))<>0;
end;
{ TLangManager }
{$ifdef LINUX}
{$else}
procedure TLangManager.AddNotify(ANotify: TObject);
var
Message: TMessage;
begin
inherited;
if LangState=lsNationLoaded then
begin
with Message do
begin
Msg := IM_GATHER_LANG;
WParam := nativeInt(lngEng);
LParam := 0;
result := 0;
end;
ANotify.Dispatch(Message);
with Message do
begin
Msg := IM_APPLY_LANG;
WParam := nativeInt(lngNation);
LParam := 0;
result := 0;
end;
ANotify.Dispatch(Message);
end;
end;
{$endif}
constructor TLangManager.Create;
begin
inherited;
LangState:=lsBeginning;
end;
destructor TLangManager.Destroy;
begin
lngEng.Free;
lngNation.Free;
inherited;
end;
procedure TLangManager.Load(const FileName: UnicodeString);
begin
FAskUser:=false;
if (FileName='') or
(WideCompareText(FileName, 'English') = 0) then
begin
if LangState=lsNationLoaded then LangState:=lsAgainEnglish
else LangState:=lsBeginning;
if lngEng=nil then
begin
lngEng:=TLangWing.Create;
Broadcast(IM_GATHER_LANG, nativeInt(lngEng));
end;
lngUsed:=lngEng;
if FileName='' then FAskUser:=true;
end else
begin
if lngEng=nil then
begin
lngEng:=TLangWing.Create;
Broadcast(IM_GATHER_LANG, nativeInt(lngEng));
end;
if lngNation=nil then lngNation:=TLangWing.Create;
if lngNation.LoadFromFile(FileName) then
begin
LangState:=lsNationLoaded;
lngUsed:=lngNation;
end else
begin
LangState:=lsBadFile;
lngUsed:=lngEng;
FAskUser:=true;
end;
end;
if lngUsed<>nil then
Broadcast(IM_APPLY_LANG, nativeInt(lngUsed));
//I can't clear lngNation because NotFoundKeys is used
end;
procedure TLangManager.Update(const FileName: UnicodeString);
begin
case LangState of
lsBeginning,lsBadFile:Load(FileName);
lsNationLoaded:
with lngNation do
if OtherFile(FileName) or FileChanged then Load(FileName);
lsAgainEnglish:
with lngEng do
if OtherFile(FileName) then Load(FileName);
end;
end;
initialization
LangManager:=TLangManager.Create;
finalization
LangManager.Free;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.