-
Notifications
You must be signed in to change notification settings - Fork 282
/
restranslator.pas
823 lines (731 loc) · 24.4 KB
/
restranslator.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
{*************************************************************************************
This file is part of Transmission Remote GUI.
Copyright (c) 2011-2013 by Yury Sidorov
Copyright (c) 2010 Alex Cherednichenko, aka Alex7Che.
Transmission Remote GUI is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Transmission Remote GUI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Transmission Remote GUI; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give permission to
link the code of portions of this program with the
OpenSSL library under certain conditions as described in each individual
source file, and distribute linked combinations including the two.
You must obey the GNU General Public License in all respects for all of the
code used other than OpenSSL. If you modify file(s) with this exception, you
may extend this exception to your version of the file(s), but you are not
obligated to do so. If you do not wish to do so, delete this exception
statement from your version. If you delete this exception statement from all
source files in the program, then also delete it here.
*************************************************************************************}
unit ResTranslator;
{$MODE objfpc}{$H+}
interface
uses
Classes, StrUtils, SysUtils, FileUtil, LazFileUtils, LResources, TypInfo, LCLProc, LazUTF8;
type
TWordDelimitersOptions = set of (wdIgnoreLeading, wdIgnoreTrailing);
TResTranslator = class;
TTranslateStringEvent = procedure(Sender: TResTranslator; const ResourceName: AnsiString; var Accept: boolean);
TTranslateStringOption = (tsoNew, tsoUsed, tsoExternal);
TTranslateStringOptions = set of TTranslateStringOption;
{ TTranslateStringList }
TTranslateStringList = class(TStringList)
private
function CorrectGetName(Index: integer): string;
function CorrectGetValue(const Name: string): string;
function GetOptions(Index: integer): TTranslateStringOptions;
function NormaliseQuotedStr(const S: string): string;
function ScanQuotSep(P: PChar):integer;
procedure SetOptions(Index: integer; const AValue: TTranslateStringOptions);
protected
function DoCompareText(const s1,s2 : string) : PtrInt; override;
public
constructor Create(const FileName: string); overload;
function IndexOfName(const Name: string; var Offset: integer): integer;
function IndexOfName(const Name: string): integer; override;
procedure LoadFromFile(const FileName: string); override;
procedure SaveToFile(const FileName: string); override;
procedure Merge(Source: TTranslateStringList; const NamesOnly: boolean = false);
property CValues[const Name: string]: string read CorrectGetValue;
property CNames[Index: integer]: string read CorrectGetName;
property Options[Index: integer]: TTranslateStringOptions read GetOptions write SetOptions;
end;
{ TResTranslator }
TResTranslator = class(TAbstractTranslator)
private
FIgnoreDelimiters: TWordDelimitersOptions;
FOnTranslateString: TTranslateStringEvent;
FStrResLst: TTranslateStringList;
FModified: boolean;
FTranslationLanguage: AnsiString;
FWordDelims: TSysCharset;
function GetStrings: TStringList;
procedure SetIgnoreDelimiters(const AValue: TWordDelimitersOptions);
procedure SetOnTranslateString(const AValue: TTranslateStringEvent);
procedure SetWordDelims(const AValue: TSysCharset);
function InternalTranslateString(const Value: AnsiString; IsExternal: boolean = False): AnsiString;
public
FTranslationFile: string;
constructor Create(TranslationFile: AnsiString);
destructor Destroy; override;
procedure TranslateStringProperty(Sender: TObject; const Instance: TPersistent; PropInfo: PPropInfo; var Content: string); override;
procedure SaveFile; overload;
procedure SaveFile(const aFileName: string); overload;
property Modified: boolean Read FModified;
property Strings: TStringList Read GetStrings;
property IgnoreDelimiters: TWordDelimitersOptions Read FIgnoreDelimiters Write SetIgnoreDelimiters;
property WordDelims: TSysCharset Read FWordDelims Write SetWordDelims;
property TranslationLanguage: AnsiString read FTranslationLanguage;
property OnTranslateString: TTranslateStringEvent Read FOnTranslateString Write SetOnTranslateString;
end;
function LoadTranslationFile(const TranslationFile: AnsiString; const OnTranslate: TTranslateStringEvent = nil): AnsiString;
procedure SaveTranslationFile; overload;
procedure SaveTranslationFile(const FileName: AnsiString); overload;
procedure MakeTranslationFile; overload;
procedure MakeTranslationFile(Language: AnsiString); overload;
procedure MakeTranslationFile(const FileName, Language: AnsiString); overload;
procedure SupplementTranslationFile(const FileName: AnsiString);
procedure SupplementTranslationFiles; overload;
procedure SupplementTranslationFiles(const TranslationFilesPath: AnsiString); overload;
function LoadDefaultTranslationFile(const OnTranslate: TTranslateStringEvent = nil): TFileName;
function LoadDefaultTranslationFile(const TranslationFilesPath: AnsiString; const OnTranslate: TTranslateStringEvent = nil): TFileName;
function LoadLanguageTranslation(const Language: AnsiString; const OnTranslate: TTranslateStringEvent = nil): TFileName;
function LoadLanguageTranslation(const Language, TranslationFilesPath: AnsiString; const OnTranslate: TTranslateStringEvent = nil): TFileName;
function TranslateString(const Value: AnsiString; IsExternal: boolean = False): AnsiString;
function ExtractLangName(const FileName: TFilename): AnsiString;
function GetAvailableTranslations: TStringList;
function GetAvailableTranslations(const SearchPath: AnsiString): TStringList;
function GetTranslationFileName(const Language: AnsiString; AvailableTranslations: TStringList): AnsiString;
function DefaultLangDir: AnsiString;
function IsTranslationFileValid(const TranslationFile: AnsiString): boolean;
const
sLanguageIDName = 'TranslationLanguage';
var
IniFileName: string;
implementation
uses
Forms, utils;
const
LineSeparator = '###################';
{ procedures and functions }
function IsQuoted(const S: AnsiString; QuoteChar: char): boolean; inline;
var
L: integer;
begin
L:= Length(S);
if L > 1 then
Result := (S[1] = QuoteChar) and (S[L] = QuoteChar)
else
Result := false;
end;
function HasSeparator(const S: AnsiString; Separator: char): boolean; inline;
begin
Result := Pos(Separator, S) > 0;
end;
function ExtractLangName(const FileName: TFilename): AnsiString;
begin
with TTranslateStringList.Create(FileName) do
try
Result := AnsiDequotedStr(CValues[sLanguageIDName], QuoteChar);
finally
Free;
end;
end;
function GetAvailableTranslations: TStringList;
begin
Result:= GetAvailableTranslations(DefaultLangDir);
end;
function GetAvailableTranslations(const SearchPath: AnsiString): TStringList;
var
Sr: TSearchRec;
LangName, s: AnsiString;
begin
Result:= TStringList.Create;
if FindFirstUTF8(IncludeTrailingPathDelimiter(SearchPath) + '*', faArchive or faReadOnly, Sr) = 0 then
with Result do begin
NameValueSeparator:= '=';
QuoteChar:= '"';
repeat
if ExtractFileExt(Sr.Name) = '.template' then
continue;
s:=IncludeTrailingPathDelimiter(ExtractFilePath(SearchPath)) + Sr.Name;
if IsTranslationFileValid(s) then begin
LangName:= ExtractLangName(s);
if LangName <> '' then
Add(LangName + NameValueSeparator + Sr.Name);
end;
until FindNextUTF8(Sr) <> 0;
FindClose(Sr);
end;
end;
var
FDefaultLangDir: AnsiString;
function DefaultLangDir: AnsiString;
{$ifdef unix}
function _IsLangDir(const dir: string): boolean;
var
sr: TSearchRec;
begin
Result:=FindFirstUtf8(dir + ExtractFileNameOnly(ParamStrUtf8(0)) + '.*', faAnyFile, sr) = 0;
FindClose(sr);
end;
var
s: string;
{$endif unix}
begin
if FDefaultLangDir = '' then begin
FDefaultLangDir:=ExtractFilePath(ParamStrUtf8(0)) + 'lang' + DirectorySeparator;
{$ifdef unix}
if not _IsLangDir(FDefaultLangDir) then begin
s:='/usr/share/' + ExtractFileNameOnly(ParamStrUtf8(0)) + '/lang/';
if _IsLangDir(s) then
FDefaultLangDir:=s
else begin
s:='/usr/local/share/' + ExtractFileNameOnly(ParamStrUtf8(0)) + '/lang/';
if _IsLangDir(s) then
FDefaultLangDir:=s;
end;
end;
{$endif unix}
end;
Result:=FDefaultLangDir;
end;
function GetResStrings(Name, Value: AnsiString; Hash: longint; P: pointer): AnsiString;
var
Accept: boolean;
begin
with TResTranslator(P) do begin
Accept := True;
if Assigned(OnTranslateString) then
OnTranslateString(TResTranslator(P), Name, Accept);
if Accept then
Result := InternalTranslateString(Value)
else
Result := Value;
end;
end;
function LoadTranslationFile(const TranslationFile: AnsiString; const OnTranslate: TTranslateStringEvent = nil): AnsiString;
begin
LRSTranslator := TResTranslator.Create(TranslationFile);
TResTranslator(LRSTranslator).OnTranslateString := OnTranslate;
SetResourceStrings(@GetResStrings, LRSTranslator);
Result := TResTranslator(LRSTranslator).TranslationLanguage;
end;
procedure SupplementTranslationFiles; overload;
begin
SupplementTranslationFiles(DefaultLangDir);
end;
procedure MakeTranslationFile; overload;
begin
MakeTranslationFile('???');
end;
procedure MakeTranslationFile(Language: AnsiString); overload;
var
lLang, sLang, s: string;
begin
LazGetLanguageIDs(lLang, sLang);
sLang:=AnsiLowerCase(sLang);
s:=ExtractFileNameOnly(ParamStrUtf8(0));
if (sLang <> '') and not FileExistsUTF8(DefaultLangDir + s + '.' + sLang) then
s:=s + '.' + sLang
else
s:=s + '.lng';
MakeTranslationFile(DefaultLangDir + s, Language);
end;
procedure MakeTranslationFile(const FileName, Language: AnsiString);
var
Dst: TTranslateStringList;
begin
if Assigned(LRSTranslator) and (LRSTranslator is TResTranslator) then begin
Dst := TTranslateStringList.Create;
try
Dst.Values[sLanguageIDName]:= Language;
with LRSTranslator as TResTranslator do
Dst.Merge(Strings as TTranslateStringList, true);
ForceDirectories(ExtractFilePath(FileName));
Dst.SaveToFile(FileName);
finally
Dst.Free;
end;
end;
end;
procedure SupplementTranslationFile(const FileName: AnsiString);
var
Dst: TTranslateStringList;
begin
if Assigned(LRSTranslator) and (LRSTranslator is TResTranslator) then begin
Dst := TTranslateStringList.Create(FileName);
try
with LRSTranslator as TResTranslator do
Dst.Merge(Strings as TTranslateStringList, true);
Dst.SaveToFile(FileName);
finally
Dst.Free;
end;
end;
end;
procedure SupplementTranslationFiles(const TranslationFilesPath: AnsiString);
var
Sl: TStringList;
i: integer;
s: string;
begin
if Assigned(LRSTranslator) and (LRSTranslator is TResTranslator) then begin
Sl := GetAvailableTranslations(TranslationFilesPath);
with Sl do
for i := 0 to Count - 1 do
SupplementTranslationFile(IncludeTrailingPathDelimiter(TranslationFilesPath) + ValueFromIndex[i]);
// Supplement template file
s:=IncludeTrailingPathDelimiter(TranslationFilesPath) + ExtractFileNameOnly(ParamStrUtf8(0)) + '.template';
if FileExistsUTF8(s) then
SupplementTranslationFile(s);
end;
end;
const
InvalidLangExt: array[1..6] of string = ('ua', 'by', 'cn', 'cz', 'se', 'tw');
function IsTranslationFileValid(const TranslationFile: AnsiString): boolean;
var
s: string;
i: integer;
begin
Result:=FileExistsUTF8(TranslationFile);
if not Result then
exit;
s:=LowerCase(ExtractFileExt(TranslationFile));
Delete(s, 1, 1);
for i:=Low(InvalidLangExt) to High(InvalidLangExt) do
if s = InvalidLangExt[i] then begin
Result:=False;
exit;
end;
end;
function LoadDefaultTranslationFile(const OnTranslate: TTranslateStringEvent): TFileName;
begin
Result := LoadDefaultTranslationFile(DefaultLangDir, OnTranslate);
end;
function LoadDefaultTranslationFile(const TranslationFilesPath: AnsiString; const OnTranslate: TTranslateStringEvent): TFileName;
var
lLang, sLang, s: string;
i: integer;
begin
LazGetLanguageIDs(lLang, sLang);
lLang:=LowerCase(lLang);
sLang:=LowerCase(sLang);
{$ifdef windows}
if sLang = 'ch' then begin
sLang:='zh';
lLang:=StringReplace(lLang, 'ch_', 'zh_', []);
end;
{$endif windows}
i:=Pos('.', lLang);
if i > 0 then
SetLength(lLang, i - 1);
s:=IncludeTrailingPathDelimiter(TranslationFilesPath) + ExtractFileNameOnly(ParamStrUtf8(0))+ '.';
Result := s + lLang;
// First check full language name (uk_ua)
if not IsTranslationFileValid(Result) then begin
Result := s + sLang;
// Check fallback language name (uk)
if not IsTranslationFileValid(Result) then begin
// Finally use country name (ua)
i:=Pos('_', lLang);
if i > 0 then
lLang:=Copy(lLang, i + 1, MaxInt);
Result := s + lLang;
if not IsTranslationFileValid(Result) then begin
Result:='';
exit;
end;
end;
end;
IniFileName:=Result;
Result := LoadTranslationFile(Result, OnTranslate);
end;
function LoadLanguageTranslation(const Language: AnsiString; const OnTranslate: TTranslateStringEvent): TFileName;
begin
Result := LoadLanguageTranslation(Language, DefaultLangDir, OnTranslate);
end;
function LoadLanguageTranslation(const Language, TranslationFilesPath: AnsiString; const OnTranslate: TTranslateStringEvent): TFileName;
var
Sl: TStringList;
begin
Sl:= GetAvailableTranslations(TranslationFilesPath);
Result:= GetTranslationFileName(Language, Sl);
if Result <> '' then
Result := IncludeTrailingPathDelimiter(TranslationFilesPath) + Result;
if FileExistsUTF8(Result) then
LoadTranslationFile(Result, OnTranslate);
end;
function GetTranslationFileName(const Language: AnsiString; AvailableTranslations: TStringList): AnsiString;
var
i: integer;
aName, aValue: string;
begin
Result := '';
if Assigned(AvailableTranslations) then
with AvailableTranslations do
for i := 0 to Count - 1 do begin
GetNameValue(i, aName, aValue);
if AnsiSameText(AnsiDequotedStr(Language, QuoteChar), AnsiDequotedStr(aName, QuoteChar)) then begin
Result:= AnsiDequotedStr(aValue, QuoteChar);
Break;
end;
end;
end;
procedure SaveTranslationFile; overload;
begin
if Assigned(LRSTranslator) and (LRSTranslator is TResTranslator) then
with LRSTranslator as TResTranslator do
if Modified then
SaveFile;
end;
procedure SaveTranslationFile(const FileName: AnsiString); overload;
begin
if Assigned(LRSTranslator) and (LRSTranslator is TResTranslator) then
with LRSTranslator as TResTranslator do
if Modified then
SaveFile(FileName);
end;
function TranslateString(const Value: AnsiString; IsExternal: boolean): AnsiString;
begin
if Assigned(LRSTranslator) and (LRSTranslator is TResTranslator) then
with LRSTranslator as TResTranslator do
result := InternalTranslateString(Value, IsExternal)
else
result := Value;
end;
{ TTranslateStringList }
function TTranslateStringList.CorrectGetValue(const Name: string): string;
var
Index: integer;
offset: integer;
begin
Index := IndexOfName(Name, offset);
if Index >= 0 then begin
Result := Copy(Strings[Index], offset, MaxInt);
Options[Index]:=Options[Index] + [tsoUsed];
end
else
result := '';
end;
function TTranslateStringList.GetOptions(Index: integer): TTranslateStringOptions;
begin
Result:=TTranslateStringOptions(cardinal(ptruint(Objects[Index])));
end;
function TTranslateStringList.CorrectGetName(Index: integer): string;
var
Offset: integer;
s: string;
begin
CheckSpecialChars;
Result := '';
s := Strings[Index];
Offset := ScanQuotSep(PChar(s));
if (Offset > 0) then
Result := NormaliseQuotedStr(LeftStr(s, offset));
end;
function TTranslateStringList.ScanQuotSep(P: PChar): integer;
var
i, len: integer;
QuoteCount: integer;
begin
result := 0;
QuoteCount := 0;
i := 0;
len:=strlen(P);
while (i < len) and (result = 0) do begin
if P[i] = QuoteChar then
inc(QuoteCount)
else if (P[i] = NameValueSeparator) and not odd(QuoteCount) then
result := i;
inc(i);
end;
end;
procedure TTranslateStringList.SetOptions(Index: integer; const AValue: TTranslateStringOptions);
begin
Objects[Index]:=TObject(ptruint(cardinal(AValue)));
end;
function TTranslateStringList.DoCompareText(const s1, s2: string): PtrInt;
begin
if CaseSensitive then
result:=AnsiCompareText(s1,s2)
else
result:=AnsiCompareText(UTF8UpperCase(s1),UTF8UpperCase(s2));
end;
constructor TTranslateStringList.Create(const FileName: string);
begin
inherited Create;
CheckSpecialChars;
LoadFromFile(FileName);
end;
function TTranslateStringList.NormaliseQuotedStr(const S: string): string;
begin
if not HasSeparator(S, NameValueSeparator) then
Result := AnsiDequotedStr(S, QuoteChar)
else if not IsQuoted(S, QuoteChar) then
Result := AnsiQuotedStr(S, QuoteChar)
else
Result := S;
end;
function TTranslateStringList.IndexOfName(const Name: string; var Offset: integer): integer;
var
s, n: string;
begin
CheckSpecialChars;
result := 0;
n:=NormaliseQuotedStr(Name);
while (result < Count) do begin
s:=Strings[result];
Offset := ScanQuotSep(PChar(s));
if (Offset > 0) and (n = Copy(s, 1, Offset)) then begin
inc(Offset, 2);
exit;
end;
inc(result);
end;
result := -1;
end;
function TTranslateStringList.IndexOfName(const Name: string): integer;
var
i: integer;
begin
Result:=IndexOfName(Name, i);
end;
procedure TTranslateStringList.LoadFromFile(const FileName: string);
var
FS: TFileStreamUTF8;
buff: array[1..3] of char;
i, j, k: integer;
s, esep: string;
begin
FS:= TFileStreamUTF8.Create(FileName, fmOpenRead);
try
// Skip UTF8 header
buff := '';
FS.Read(buff, SizeOf(UTF8FileHeader));
if buff <> UTF8FileHeader then
FS.Position:=0;
LoadFromStream(FS);
finally
FS.Free;
end;
i:=IndexOf(LineSeparator);
if i >= 0 then
Delete(i);
// Normalize quotations
esep:=NameValueSeparator + NameValueSeparator;
for i:=0 to Count - 1 do begin
s:=Strings[i];
j:=ScanQuotSep(PChar(s));
if j > 0 then begin
k:=j + 2;
if Copy(s, j + 1, 2) = esep then begin
Options[i]:=[tsoExternal];
Inc(k);
end;
Strings[i]:=NormaliseQuotedStr(Copy(s, 1, j)) + NameValueSeparator + NormaliseQuotedStr(Copy(s, k, MaxInt));
end;
end;
end;
procedure TTranslateStringList.SaveToFile(const FileName: string);
var
FS: TFileStreamUTF8;
i, j: integer;
s, esep: string;
begin
ForceDirectories(ExtractFilePath(FileName));
FS := TFileStreamUTF8.Create(FileName, fmCreate);
try
FS.WriteBuffer(UTF8FileHeader, SizeOf(UTF8FileHeader));
esep:=NameValueSeparator + NameValueSeparator;
for i:=0 to Count - 1 do begin
s:=Strings[i];
if tsoExternal in Options[i] then begin
j:=ScanQuotSep(PChar(s));
if j > 0 then
s:=NormaliseQuotedStr(Copy(s, 1, j)) + esep + NormaliseQuotedStr(Copy(s, j + 2, MaxInt));
end;
if s <> '' then
FS.WriteBuffer(s[1], Length(s));
s:=LineEnding;
FS.WriteBuffer(s[1], Length(s));
end;
finally
FS.Free;
end;
end;
procedure TTranslateStringList.Merge(Source: TTranslateStringList; const NamesOnly: boolean = false);
var
i, j: integer;
n: string;
begin
CheckSpecialChars;
Source.Sort;
for i:=0 to Count - 1 do
Options[i]:=[];
for i:=0 to Source.Count - 1 do begin
if Source.Options[i]*[tsoUsed, tsoExternal] = [] then
continue;
n:=Source.CNames[i];
if n <> '' then begin
j:=IndexOfName(n);
if j < 0 then begin
// New string
if NamesOnly then
j:=Add(n + NameValueSeparator + n)
else
j:=Add(Source.Strings[i]);
end;
Options[j]:=Source.Options[i] + [tsoUsed];
end;
end;
// Delete unused strings
i:=0;
while i < Count do begin
n:=CNames[i];
if (Options[i] = []) and (n <> '') and (CompareText(n, sLanguageIDName) <> 0) then
Delete(i)
else
Inc(i);
end;
end;
{ TResTranslator }
constructor TResTranslator.Create(TranslationFile: AnsiString);
begin
inherited Create;
FTranslationFile := TranslationFile;
FIgnoreDelimiters := [wdIgnoreTrailing];
FWordDelims := ['.', ',', ':'];
FStrResLst := TTranslateStringList.Create;
with FStrResLst do begin
Duplicates := dupIgnore;
CaseSensitive := False;
CheckSpecialChars;
if FileExistsUTF8(FTranslationFile) then begin
LoadFromFile(FTranslationFile);
FTranslationLanguage := AnsiDequotedStr(CValues[AnsiQuotedStr(sLanguageIDName, QuoteChar)], QuoteChar);
end;
end;
end;
destructor TResTranslator.Destroy;
begin
FStrResLst.Free;
inherited Destroy;
end;
function TResTranslator.InternalTranslateString(const Value: AnsiString; IsExternal: boolean): AnsiString;
function IsAlpha(Ch: char): boolean; inline;
begin
Result := Ch in ['A'..'Z', 'a'..'z'];
end;
function HasAlpha: boolean;
var
i: integer;
begin
Result := False;
i := 1;
while not Result and (i <= Length(Value)) do begin
Result := IsAlpha(Value[i]);
Inc(i);
end;
end;
var
ClearValue: AnsiString;
Original, s, n: AnsiString;
i: integer;
begin
Original := Value;
ClearValue := StringReplace(AdjustLineBreaks(Value), LineEnding, '~', [rfReplaceAll]);
Result := ClearValue;
if wdIgnoreLeading in IgnoreDelimiters then
RemoveLeadingChars(ClearValue, FWordDelims);
if wdIgnoreTrailing in IgnoreDelimiters then
RemoveTrailingChars(ClearValue, FWordDelims);
if HasAlpha then
begin
with FStrResLst do begin
if HasSeparator(ClearValue, NameValueSeparator) then
n := AnsiQuotedStr(ClearValue, QuoteChar)
else
n := ClearValue;
s:=CValues[n];
if (s = '') then begin
i:=Add(n + NameValueSeparator + n);
Options[i]:=[tsoNew, tsoUsed];
FModified := True;
Result := Original;
end
else begin
Result := StringReplace(Result, ClearValue, AnsiDequotedStr(s, QuoteChar), [rfReplaceAll]);
Result := StringReplace(Result, '~', LineEnding, [rfReplaceAll]);
end;
if IsExternal then begin
i:=IndexOfName(n);
if i >= 0 then
Options[i]:=Options[i] + [tsoExternal];
end;
end;
end;
end;
procedure TResTranslator.SetIgnoreDelimiters(const AValue: TWordDelimitersOptions);
begin
if FIgnoreDelimiters = AValue then
exit;
FIgnoreDelimiters := AValue;
end;
function TResTranslator.GetStrings: TStringList;
begin
Result := FStrResLst;
end;
procedure TResTranslator.SetOnTranslateString(const AValue: TTranslateStringEvent);
begin
if FOnTranslateString = AValue then
exit;
FOnTranslateString := AValue;
end;
procedure TResTranslator.SetWordDelims(const AValue: TSysCharset);
begin
if FWordDelims = AValue then
exit;
FWordDelims := AValue;
end;
procedure TResTranslator.TranslateStringProperty(Sender: TObject; const Instance: TPersistent; PropInfo: PPropInfo; var Content: string);
var
Accept: boolean;
ResourceName: AnsiString;
OwnerName: AnsiString;
begin
if Sender is TReader and Assigned(TReader(Sender).Owner) then
OwnerName := TReader(Sender).Owner.GetNamePath;
if Instance.InheritsFrom(TForm) then
ResourceName := OwnerName + '.' + PropInfo^.Name
else
ResourceName := OwnerName + '.' + Instance.GetNamePath + '.' + PropInfo^.Name;
Accept := True;
if Assigned(OnTranslateString) then
OnTranslateString(Self, ResourceName, Accept);
if (PropInfo^.Name = 'Caption') and (Instance.GetNamePath = Content) then
Accept:=False
else
if PropInfo^.Name = 'Name' then
Accept:=False;
if Accept then
Content := InternalTranslateString(Content);
end;
procedure TResTranslator.SaveFile;
begin
SaveTranslationFile(FTranslationFile);
end;
procedure TResTranslator.SaveFile(const aFileName: string);
begin
FStrResLst.SaveToFile(aFileName);
end;
finalization
FreeAndNil(LRSTranslator);
end.