File: pofamilies.pp

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (878 lines) | stat: -rw-r--r-- 26,330 bytes parent folder | download | duplicates (2)
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
unit PoFamilies;

{ $define DebugSimplePoFiles}

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, ContNrs, Math,
  // LCL
  LCLProc, Masks,
  // LazUtils
  FileUtil, LazFileUtils, Translations, StringHashList,
  // PoChecker
  PoCheckerConsts;

Type

  TPoTestType = (pttCheckNrOfItems, pttCheckFormatArgs, pttCheckMissingIdentifiers,
                 pttCheckMismatchedOriginals);
  TPoTestTypes = Set of TPoTestType;

  TPoTestOption = (ptoFindAllChildren);
  TPoTestOptions = set of TPoTestOption;

const
    optRunAllTests: TPoTestTypes = [];

    PoTestTypeNames: array[TPoTestType] of String = (
      sCheckNumberOfItems,
      sCheckForIncompatibleFormatArguments,
      sCheckMissingIdentifiers,
      sCheckForMismatchesInUntranslatedStrings
    );

Type
  { TPoFamily }

  TTestStartEvent = procedure(const ATestName, APoFileName: String) of object;
  TTestEndEvent = procedure(const ATestName: String; const ErrorCount: Integer) of object;

  TPoFamilyStats = class;

  TPoFamily = class
  private
    FMaster: TPOFile;
    FChild: TPOFile;
    FMasterName: String;
    FChildName: String;
    FOnTestStart: TTestStartEvent;
    FOnTestEnd: TTestEndEvent;
    FPoFamilyStats: TPoFamilyStats;
    FTestOptions: TPoTestOptions;
    FTestTypes: TPoTestTypes;
    procedure SetChildName(AValue: String);
    procedure SetMasterName(AValue: String);
    function GetShortMasterName: String;
    function GetShortChildName: String;
  protected
    procedure DoTestStart(const ATestName, APoFileName: String);
    procedure DoTestEnd(const ATestName: String; const ErrorCount: Integer);
  public
    constructor Create;
    constructor Create(const MasterName: String);
    constructor Create(const AMasterName, AChildName: String);
    destructor Destroy; override;

  protected
    procedure CheckNrOfItems(out ErrorCount: Integer; ErrorLog: TStrings);
    procedure CheckFormatArgs(out ErrorCount, NonFuzzyErrorCount: Integer; ErrorLog: TStrings);
    procedure CheckMissingIdentifiers(out ErrorCount: Integer; ErrorLog: TStrings);
    procedure CheckMismatchedOriginals(out ErrorCount: Integer; ErrorLog: TStrings);
    procedure CheckDuplicateOriginals(out WarningCount: Integer; ErrorLog: TStrings);
    procedure CheckStatistics(ErrorCnt: Integer);

  public
    procedure RunTests(out ErrorCount, NonFuzzyErrorCount, WarningCount,
      TranslatedCount, UntranslatedCount, FuzzyCount: Integer; ErrorLog, StatLog, DupLog: TStringList);

    property Master: TPOFile read FMaster;
    property Child: TPOFile read FChild;
    property MasterName: String read FMasterName write SetMasterName;
    property ChildName: String read FChildName write SetChildName;
    property TestTypes: TPoTestTypes read FTestTypes write FTestTypes;
    property TestOptions: TPoTestOptions read FTestOptions write FTestOptions;
    property PoFamilyStats: TPoFamilyStats read FPoFamilyStats;
    property ShortMasterName: String read  GetShortMasterName;
    property ShortChildName: String read GetShortChildName;
    property OnTestStart: TTestStartEvent read FOnTestStart write FOnTestStart;
    property OnTestEnd: TTestEndEvent read FOnTestEnd write FOnTestEnd;
  end;

  { TPoFamilyStats }

  { TStat }

  TStat = class
  private
    FPoName: String;
    FNrTotal: Integer;
    FNrTranslated: Integer;
    FNrUnTranslated: Integer;
    FNrFuzzy: Integer;
    FNrErrors: Integer;
  public
    constructor Create(APoName: String; ANrTotal, ANrTranslated, ANrUntranslated, ANrFuzzy, ANrErrors: Integer);
    function ShortPoName: String;
    property PoName: string read FPoName;
    property NrTotal: Integer read FNrTotal;
    property NrTranslated: Integer read FNrTranslated;
    property NrUnTranslated: Integer read FNrUnTranslated;
    property NrFuzzy: Integer read FNrFuzzy;
    property NrErrors: Integer read FNrErrors;
    function PercTranslated: Double; inline;
    function PercUnTranslated: Double; inline;
    function PercFuzzy: Double; inline;
    function FracTranslated: Double;
    function FracUnTranslated: Double;
    function FracFuzzy: Double;
  end;

  TPoFamilyStats = class
  private
    FList: TFPObjectList;
    function GetCount: Integer;
    function GetItems(Index: Integer): TStat;
  public
    procedure Clear;
    procedure Add(AName: String; ANrTotal, ANrTranslated, ANrUnTranslated, ANrFuzzy, ANrErrors: Integer);
    procedure AddItemsTo(APoFamilyStats: TPoFamilyStats);
    constructor Create;
    destructor Destroy; override;
    procedure AddStatisticsToLog(ALog: TStrings);

    property Items[Index: Integer]: TStat read GetItems;
    property Count: Integer read GetCount;
  end;

function IsMasterPoName(const Fn: String): Boolean;
function ExtractMasterNameFromChildName(const AChildName: String): String;
function ExtractLanguageFromChildName(const AChildName: string): TLangID;
procedure LocalizePoTestTypeNames;

const
  NoError = 0;

implementation

const
  sCommentIdentifier = '#: ';
  //sCharSetIdentifier = '"Content-Type: text/plain; charset=';
  sMsgID = 'msgid "';
  sMsgStr = 'msgstr "';
  //sMsgCtxt = 'msgctxt "';
  //sFlags = '#, ';
  //sPrevMsgID = '#| msgid "';
  //sPrevStr = '#| "';

  Divider = '-------------------------------------------------------';

  sFormatArgsID = '%s %s';
  sFormatArgsValues = '%s%s"   (= %s)';

  sMismatchOriginalsID = '%s';
  sMismatchOriginalsM = '%s: %s';
  sMismatchOriginalsC = '%s: %s';

  sShortCheckFormatArgs = 'CheckFormatArgs';
  sShortCheckNrOfItems =  'CheckNrOfItems';
  sShortCheckMissingIdentifiers = 'CheckMissingIdentifiers';
  sShortCheckMismatchedOriginals = 'CheckMismatchedOriginals';

//Helper functions


function IsMasterPoName(const Fn: String): Boolean;
//Returns True if Fn is like '[Path/To/]somename.po'
var
  Ext: String;
  FnOnly: String;
  IsInValidFn: Boolean;
begin
  FnOnly := ExtractFileNameOnly(Fn);
  //check if filename is like 'af_ZA.po', which is an invalid name for a master po-file
  //a bit crude, but will do now (at least for Lazarus)
  IsInValidFn := MatchesMaskList(FnOnly, '??;??_??',';',False);
  Ext := ExtractFileExt(Fn);
  Result := not IsInValidFn and
            (Length(FnOnly) > 0) and
            (CompareText(Ext, ExtensionSeparator + 'po') = 0) and
            (Pos(ExtensionSeparator, FnOnly) = 0);
end;

function ExtractMasterNameFromChildName(const AChildName: String): String;
{
  Pre condition: AChildName is like: somename.some_language_specifier.po
  Post condition: Result  = somename.po
}
var
  Ext: String;
  EndSep: Set of Char;
  Len: Integer;
begin
  EndSep := AllowDirectorySeparators + AllowDriveSeparators + [ExtensionSeparator];
  Ext := ExtractFileExt(AChildName);
  Result := Copy(AChildName, 1, Length(AChildName) - Length(Ext));
  Len := Length(Result);
  While (Len > 0) and (not (Result[Len] in EndSep)) do Dec(Len);

  //debugln('Len = ',DbgS(Len));
  //debugln('Length(Result) = ',DbgS(Length(result)));
  //if Len > 0 then debugln('Result[Len] = ',Result[len]);

  if (Len > 1) and (Len < Length(Result)) and (Result[Len] = ExtensionSeparator) then
    Result := Copy(Result, 1, Len - 1) + Ext
  else
    Result := '';
end;

function ExtractLanguageFromChildName(const AChildName: string): TLangID;
Var
  Mn, Abbr: string;
  P1,P2: Integer;
begin
  Mn := ExtractMasterNameFromChildName(AChildName);
  Mn := ExtractFileNameWithoutExt(Mn);
  P1 := Length(Mn);
  P2 := Length(AChildName);
  Abbr := Copy(AChildName,P1+2,P2-(P1+1));
  Abbr := ExtractFileNameWithoutExt(Abbr);
  Result := LangAbbrToLangId(Abbr);
end;

procedure LocalizePoTestTypeNames;
begin
  PoTestTypeNames[pttCheckNrOfItems] := sCheckNumberOfItems;
  PoTestTypeNames[pttCheckFormatArgs] := sCheckForIncompatibleFormatArguments;
  PoTestTypeNames[pttCheckMissingIdentifiers] := sCheckMissingIdentifiers;
  PoTestTypeNames[pttCheckMismatchedOriginals] := sCheckForMismatchesInUntranslatedStrings;
end;

{ TStat }

constructor TStat.Create(APoName: String; ANrTotal, ANrTranslated, ANrUntranslated, ANrFuzzy, ANrErrors: Integer);
begin
  FPoName := APoName;
  FNrTotal := ANrTotal;
  FNrTranslated := ANrTranslated;
  FNrUntranslated := ANrUntranslated;
  FNrFuzzy := ANrFuzzy;
  FNrErrors := ANrErrors;
end;

function TStat.ShortPoName: String;
begin
  Result := ExtractFilename(FPoName);
end;

function TStat.PercTranslated: Double;
begin
  Result := 100 * FracTranslated;
end;

function TStat.PercUnTranslated: Double;
begin
  Result := 100 * FracUnTranslated;
end;

function TStat.PercFuzzy: Double;
begin
  Result := 100 * FracFuzzy;
end;

function TStat.FracTranslated: Double;
begin
  Result := (FNrTranslated / FNrTotal);
end;

function TStat.FracUnTranslated: Double;
begin
  Result := (FNrUnTranslated / FNrTotal);
end;

function TStat.FracFuzzy: Double;
begin
  Result := (FNrFuzzy / FNrTotal);
end;

{ TPoFamilyStats }

function TPoFamilyStats.GetCount: Integer;
begin
  Result := FList.Count;
end;

function TPoFamilyStats.GetItems(Index: Integer): TStat;
begin
  Result := TStat(FList.Items[Index]);
end;

procedure TPoFamilyStats.Clear;
begin
  FList.Clear;
end;

procedure TPoFamilyStats.Add(AName: String; ANrTotal, ANrTranslated, ANrUnTranslated, ANrFuzzy, ANrErrors: Integer);
begin
  FList.Add(TStat.Create(AName, ANrTotal, ANrTranslated, ANrUntranslated, ANrFuzzy, ANrErrors));
end;

procedure TPoFamilyStats.AddItemsTo(APoFamilyStats: TPoFamilyStats);
var
  i: Integer;
  AStat: TStat;
begin
  for i := 0 to FList.Count - 1 do
  begin
    AStat := GetItems(i);
    APoFamilyStats.Add(AStat.PoName, AStat.NrTotal, AStat.NrTranslated,
                       AStat.NrUntranslated, AStat.NrFuzzy, AStat.NrErrors);
  end;
end;

constructor TPoFamilyStats.Create;
begin
  FList := TFPObjectList.Create(True);
end;

destructor TPoFamilyStats.Destroy;
begin
  FList.Free;
  inherited Destroy;
end;

procedure TPoFamilyStats.AddStatisticsToLog(ALog: TStrings);
var
  i: Integer;
  Stat: TStat;
  function Bar(Nr, Total: Integer; RoundDown: Boolean): String;
  const
    Max = 50;
  var
    Count: Integer;
  begin
    if RoundDown then
      Count := Floor(Max * (Nr/Total))
    else
      Count := Ceil(Max * (Nr/Total));
    Result := StringOfChar('x', Count);
    Result := Result + StringOfChar(#32, Max - Count);
  end;
begin
  if (FList.Count = 0) then Exit;
  for i := 0 to FList.Count - 1 do
  begin
    Stat := TStat(FList.Items[i]);
    ALog.Add(Stat.PoName);
    ALog.Add(Format(sPercTranslated,[Bar(Stat.NrTranslated, Stat.NrTotal, True),Stat.PercTranslated]));
    ALog.Add(Format(sPercUntranslated,[Bar(Stat.NrUnTranslated, Stat.NrTotal, False), Stat.PercUnTranslated]));
    ALog.Add(Format(sPercFuzzy,[Bar(Stat.NrFuzzy, Stat.NrTotal, False), Stat.PercFuzzy]));
    ALog.Add('');
    ALog.Add('');
  end;
end;

{ TPoFamily }

procedure TPoFamily.SetMasterName(AValue: String);
begin
  if FMasterName = AValue then Exit;
  FMaster.Free;
  FMaster := nil;
  FMasterName := '';
  if (AValue <> '') then FMaster := TPOFile.Create(AValue, True, False);
  FMasterName := AValue;
end;

function TPoFamily.GetShortMasterName: String;
begin
  Result := ExtractFileName(FMasterName);
end;

function TPoFamily.GetShortChildName: String;
begin
  Result := ExtractFileName(FChildName);
end;

procedure TPoFamily.DoTestStart(const ATestName, APoFileName: String);
begin
  if Assigned(FOnTestStart) then FOnTestStart(ATestName, APoFileName);
end;

procedure TPoFamily.DoTestEnd(const ATestName: String; const ErrorCount: Integer);
begin
  if Assigned(FOnTestEnd) then FOnTestEnd(ATestName, ErrorCount);
end;


procedure TPoFamily.SetChildName(AValue: String);
begin
  if FChildName = AValue then Exit;
  FChild.Free;
  FChild := nil;
  FChildName := '';
  if (AValue <> '') then FChild := TPOFile.Create(AValue, True, False);
  FChildName := AValue;
end;

constructor TPoFamily.Create;
begin
  Create('','');
end;

constructor TPoFamily.Create(const MasterName: String);
begin
  Create(MasterName, '');
end;

constructor TPoFamily.Create(const AMasterName, AChildName: String);
begin
  if (AMasterName <> '') then
  begin
    FMaster := TPOFile.Create(AMasterName, True, False);
    FMasterName := AMasterName;
    //debugln('TPoFamily.Create: created ',FMasterName);
  end;
  if (AChildName <> '') then
  begin
    FChild := TPOFile.Create(AChildName, True, False);
    FChildName := AChildName;
    //debugln('TPoFamily.Create: created ',FChildName);
  end;
  FPoFamilyStats := TPoFamilyStats.Create;
end;

destructor TPoFamily.Destroy;
begin
  if Assigned(FMaster) then FMaster.Free;
  if Assigned(FChild) then FChild.Free;
  FPoFamilyStats.Free;
  inherited Destroy;
end;

procedure TPoFamily.CheckNrOfItems(out ErrorCount: Integer; ErrorLog: TStrings);
begin
  //debugln('TPoFamily.CheckNrOfItems');
  DoTestStart(PoTestTypeNames[pttCheckNrOfItems], ShortChildName);
  if (FMaster.Count <> FChild.Count) then
  begin
    ErrorCount := 1;
    ErrorLog.Add(Divider);
    ErrorLog.Add(Format(sErrorsByTest,[sShortCheckNrOfItems]));
    ErrorLog.Add(ShortChildName);
    ErrorLog.Add(Divider);
    ErrorLog.Add('');
    ErrorLog.Add(sNrOfItemsMismatch);
    ErrorLog.Add(Format(sNrOfItemsMismatchD,[ShortMasterName,FMaster.Count]));
    ErrorLog.Add(Format(sNrOfItemsMismatchD,[ShortChildName,FChild.Count]));
    ErrorLog.Add(Divider);
    ErrorLog.Add('');
    ErrorLog.Add('');
  end
  else ErrorCount := NoError;
  DoTestEnd(PoTestTypeNames[pttCheckNrOfItems], ErrorCount);
  //debugln('TPoFamily.CheckNrOfItemsMismatch: ',Dbgs(ErrorCount),' Errors');
end;

procedure TPoFamily.CheckFormatArgs(out ErrorCount, NonFuzzyErrorCount: Integer
  ; ErrorLog: TStrings);
var
  i: Integer;
  CPoItem: TPOFileItem;
  IsFuzzy: Boolean;
  IsBadFormat: Boolean;
begin
  //debugln('TPoFamily.CheckFormatArgs');
  DoTestStart(PoTestTypeNames[pttCheckFormatArgs], ShortChildName);
  ErrorCount := NoError;
  NonFuzzyErrorCount := NoError;
  for i := 0 to FChild.Count - 1 do
  begin
    //debugln('  i = ',DbgS(i));
    //MPoItem := FMaster.PoItems[i];
    CPoItem := FChild.PoItems[i];
    //CPoItem := FChild.FindPoItem(MPoItem.IdentifierLow);
    if Assigned(CPoItem) then
    begin
      IsFuzzy := (Pos(sFuzzyFlag, CPoItem.Flags) > 0);
      IsBadFormat := (Pos(sBadFormatFlag, CPoItem.Flags) > 0);
      //if (IgnoreFuzzyStrings and IsFuzzy) then debugln('Skipping fuzzy translation: ',CPoItem.Translation);
      if (Length(CPoItem.Translation) > 0) and IsBadFormat then
      begin
        if (ErrorCount = 0) then
        begin
          ErrorLog.Add(Divider);
          ErrorLog.Add(Format(sErrorsByTest,[sShortCheckFormatArgs]));
          ErrorLog.Add(ShortChildName);
          ErrorLog.Add(Divider);
          ErrorLog.Add('');
        end;
        Inc(ErrorCount);
        if not IsFuzzy then
          Inc(NonFuzzyErrorCount);
        ErrorLog.Add(Format(sIncompatibleFormatArgs,[CPoItem.LineNr]));
        ErrorLog.Add(Format(sFormatArgsID,[sCommentIdentifier, CPoItem.IdentifierLow]));
        ErrorLog.Add(Format(sFormatArgsValues,[sMsgID,CPoItem.Original,sOriginal]));
        ErrorLog.Add(Format(sFormatArgsValues,[sMsgStr,CPoItem.Translation,sTranslation]));
        if IsFuzzy then ErrorLog.Add(sNoteTranslationIsFuzzy);
        ErrorLog.Add('');
      end;
    end;
  end;
  if (ErrorCount > 0) then
  begin
    ErrorLog.Add(Format(sNrErrorsFound,[ErrorCount]));
    ErrorLog.Add(Divider);
    ErrorLog.Add('');
    ErrorLog.Add('');
  end;
  DoTestEnd(PoTestTypeNames[pttCheckFormatArgs], ErrorCount);
  //debugln('TPoFamily.CheckIncompatibleFormatArgs: ',Dbgs(ErrorCount),' Errors');
end;

procedure TPoFamily.CheckMissingIdentifiers(out ErrorCount: Integer;
  ErrorLog: TStrings);
var
  i: Integer;
  MPoItem, CPoItem: TPOFileItem;
begin
  //debugln('TPoFamily.CheckMissingIdentifiers');
  DoTestStart(PoTestTypeNames[pttCheckMissingIdentifiers], ShortChildName);
  ErrorCount := NoError;
  for i := 0 to FMaster.Count - 1 do
  begin
    MPoItem := FMaster.PoItems[i];
    if Assigned(MPoItem) and (MPoItem.IdentifierLow <> '') then
    begin
      CPoItem := FChild.FindPoItem(MPoItem.IdentifierLow);
      if not Assigned(CPoItem) then
      begin
        if (ErrorCount = 0) then
        begin
          ErrorLog.Add(Divider);
          ErrorLog.Add(Format(sErrorsByTest,[sShortCheckMissingIdentifiers]));
          ErrorLog.Add(ShortChildName);
          ErrorLog.Add(Divider);
          ErrorLog.Add('');
        end;
        Inc(ErrorCount);
        ErrorLog.Add(Format(sLineInFileName,
                            [MPoItem.LineNr,ShortMasterName]));
        ErrorLog.Add(Format(sIdentifierNotFoundIn,
                            [MPoItem.IdentifierLow,ShortChildName]));
        ErrorLog.Add('');
      end;
    end;
  end;
  //Now reverse the search
  for i := 0 to FChild.Count - 1 do
  begin
    CPoItem := FChild.PoItems[i];
    if Assigned(CPoItem) and (CPoItem.IdentifierLow <> '') then
    begin
      MPoItem := FMaster.FindPoItem(CPoItem.IdentifierLow);
      if not Assigned(MPoItem) then
      begin
        if (ErrorCount = 0) then
        begin
          ErrorLog.Add(Divider);
          ErrorLog.Add(Format(sErrorsByTest,[sShortCheckMissingIdentifiers]));
          ErrorLog.Add(ShortChildName);
          ErrorLog.Add(Divider);
          ErrorLog.Add('');
        end;
        Inc(ErrorCount);
        ErrorLog.Add(Format(sLineNr,
                            [CPoItem.LineNr]));
        ErrorLog.Add(Format(sMissingMasterIdentifier,
                            [CPoItem.IdentifierLow,ShortChildName,ShortMasterName]));
        ErrorLog.Add('');
      end;
    end;
  end;
  if (ErrorCount > 0) then
  begin
    ErrorLog.Add(Format(sNrErrorsFound,[ErrorCount]));
    ErrorLog.Add(Divider);
    ErrorLog.Add('');
    ErrorLog.Add('');
  end;
  DoTestEnd(PoTestTypeNames[pttCheckMissingIdentifiers], ErrorCount);
  //debugln('TPoFamily.CheckMissingIdentifiers: ',Dbgs(ErrorCount),' Errors');
end;

procedure TPoFamily.CheckMismatchedOriginals(out ErrorCount: Integer;
  ErrorLog: TStrings);
var
  i: Integer;
  MPoItem, CPoItem: TPOFileItem;
begin
  //debugln('TPoFamily.CheckMismatchedOriginals');
  DoTestStart(PoTestTypeNames[pttCheckMismatchedOriginals], ShortChildName);
  ErrorCount := NoError;
  for i := 0 to FMaster.Count - 1 do
  begin
    MPoItem := FMaster.PoItems[i];
    CPoItem := FChild.FindPoItem(MpoItem.IdentifierLow);
    if Assigned(CPoItem) then
    begin
      if (MPoItem.Original <> CPoItem.Original) then
      begin
        if (ErrorCount = 0) then
        begin
          ErrorLog.Add(Divider);
          ErrorLog.Add(Format(sErrorsByTest,[sShortCheckMismatchedOriginals]));
          ErrorLog.Add(ShortChildName);
          ErrorLog.Add(Divider);
          ErrorLog.Add('');
        end;
        Inc(ErrorCount);
        ErrorLog.Add(Format(sLineInFileName,[CpoItem.LineNr, ShortChildName]));
        ErrorLog.Add(Format(sMismatchOriginalsID,[CPoItem.IdentifierLow]));
        ErrorLog.Add(Format(sMismatchOriginalsM,[ShortMasterName,MPoItem.Original]));
        ErrorLog.Add(Format(sMismatchOriginalsC,[ShortChildName, CPoItem.Original]));
        ErrorLog.Add('');
      end;
    end;
  end;
  if (ErrorCount > 0) then
  begin
    ErrorLog.Add(Format(sNrErrorsFound,[ErrorCount]));
    ErrorLog.Add(Divider);
    ErrorLog.Add('');
    ErrorLog.Add('');
  end;
  DoTestEnd(PoTestTypeNames[pttCheckMismatchedOriginals], ErrorCount);
  //debugln('TPoFamily.CheckMismatchedOriginals: ',Dbgs(ErrorCount),' Errors');
end;

procedure TPoFamily.CheckDuplicateOriginals(out WarningCount: Integer;
  ErrorLog: TStrings);
var
  i: Integer;
  PoItem: TPOFileItem;
  DupItemsList: TStringHashList;
  LastHash, CurHash: Cardinal;
begin
  //debugln('TPoFamily.CheckMismatchedOriginals');
  WarningCount := 0;

  DupItemsList := TStringHashList.Create(true);
  for i := 0 to FMaster.Items.Count - 1 do begin
    PoItem := TPOFileItem(FMaster.Items[i]);
    if PoItem.Duplicate = true then
      DupItemsList.Add(PoItem.Original, PoItem);
  end;

  //debugln('TPoFamily.CehckDuplicateOriginals');
  //debugln('DupItemsList.Count = ',DbgS(DupItemsList.Count));
  LastHash := 0;
  for i := DupItemsList.Count - 1 downto 0 do
  begin
    PoItem := TPoFileItem(DupItemsList.List[i]^.Data);
    if Assigned(PoItem) then
    begin
      CurHash := DupItemsList.List[i]^.HashValue;
      if (WarningCount = 0) then
      begin
        ErrorLog.Add(Format(sFile, [ShortMasterName]));
        ErrorLog.Add('');
      end;
      if (CurHash <> LastHash) then
      begin//new value for PoItem.Original
        LastHash := CurHash;
        Inc(WarningCount);
        if (WarningCount > 1) then ErrorLog.Add('');
        ErrorLog.Add(Format(sDuplicateOriginals,[PoItem.Original]));
        //debugln(format('The (untranslated) value "%s" is used for more than 1 entry:',[PoItem.Original]));
      end;
      ErrorLog.Add(format(sDuplicateLineNrWithValue,[PoItem.LineNr,PoItem.IdentifierLow]));
      //debugln(format(sDuplicateLineNrWithValue,[PoItem.LineNr,PoItem.IdentifierLow]));
    end;
  end;

  if (WarningCount > 0) then
  begin
    ErrorLog.Add('');
    ErrorLog.Add(Format(sNrWarningsFound,[WarningCount]));
    ErrorLog.Add(Divider);
    ErrorLog.Add('');
  end;

  DupItemsList.Free;

  //debugln('TPoFamily.CheckDuplicateOriginals: ',Dbgs(WarningCount),' Errors');
end;

procedure TPoFamily.CheckStatistics(ErrorCnt: Integer);
var
  NrTranslated, NrUntranslated, NrFuzzy, NrTotal: Integer;
begin
  //debugln('TPoFamily.CheckStatistics');
  NrTranslated := FChild.Statistics.Translated;
  NrUntranslated := FChild.Statistics.Untranslated;
  NrFuzzy := FChild.Statistics.Fuzzy;
  NrTotal := NrTranslated + NrUntranslated + NrFuzzy;
  if (NrTotal > 0) then
  begin
    FPoFamilyStats.Add(ChildName, NrTotal, NrTranslated, NrUntranslated, NrFuzzy, ErrorCnt);
  end;
  //debugln('TPoFamily.CheckIncompatibleFormatArgs: ',Dbgs(ErrorCount),' Errors');
end;

{
procedure TPoFamily.RunTests
Pre conditions:
  * Master and a matching Child must be assigned at start ot testing
  * If a Child is assigned it must be child of Master
}
procedure TPoFamily.RunTests(out ErrorCount, NonFuzzyErrorCount, WarningCount,
  TranslatedCount, UntranslatedCount, FuzzyCount: Integer; ErrorLog, StatLog,
  DupLog: TStringList);
var
  SL: TStringList;
  CurrErrCnt, CurrNonFuzzyErrCnt, CurrWarnCnt, ThisErrCnt: Integer;
  i: Integer;
  CurrChildName: String;
  S: String;
begin
  SL := nil;
  FPoFamilyStats.Clear;
  ErrorCount := NoError;
  NonFuzzyErrorCount := NoError;
  WarningCount := NoError;
  TranslatedCount := 0;
  UntranslatedCount := 0;
  FuzzyCount := 0;
  if (not Assigned(FMaster)) and (not Assigned(FChild)) then
  begin
    {$ifdef DebugSimplePoFiles}
    debugln('TPoFamily.RunTests: Both master and child are unassigned.');
    {$endif}
    Exit;
  end;
  if not Assigned(FMaster) then
  begin
    S := ExtractMasterNameFromChildName(FChildName);
    if (S <> '') and FileExistsUtf8(S) then
    begin
      SetMasterName(S);
    end
    else
    begin
      {$ifdef DebugSimplePoFiles}
      Debugln('TPoFamily.RunTests: Cannot find master for ',ShortChildName);
      {$endif}
      Exit;
    end
  end;

  if (ptoFindAllChildren in FTestOptions) then
  begin
    SL := FindAllTranslatedPoFiles(FMasterName);
    //We want current Child (if currently assigned) at index 0
    if Assigned(FChild) then
    begin
      for i := 0 to SL.Count - 1 do
      begin
        if (CompareFileNames(Sl.Strings[i], FChildName) = 0) then
        begin
          if (i <> 0) then SL.Exchange(i,0);
          Break;
        end;
      end;
    end;
  end
  else
  begin
    SL := TStringList.Create;
    if Assigned(FChild) then Sl.Add(FChildName);
  end;

//  for i := 0 to sl.count - 1 do debugln(extractfilename(sl.strings[i]));

  try

    //First run checks that are Master-only
    CheckDuplicateOriginals(CurrWarnCnt, DupLog);
    WarningCount := CurrWarnCnt + WarningCount;

    {$ifdef DebugSimplePoFiles}
    Debugln('TPoFamily.RunTests: number of childs for testing = ',DbgS(Sl.Count));
    {$endif}

    if (FTestTypes <> []) and (Sl.Count = 0) then
    begin
      {$ifdef DebugSimplePoFiles}
      Debugln('TPoFamily.RunTests: Warning: No child selected or found for selected tests');
      {$endif}
      ErrorLog.Add(Divider);
      ErrorLog.Add('Warning: No child selected (or found) for selected tests.');
      ErrorLog.Add(Divider);
    end;

    //then iterate all Children
    for i := 0 to SL.Count - 1 do
    begin
      ThisErrCnt:= 0;
      CurrChildName := SL.Strings[i];
      //debugln('TPoFamily.RunTests: setting ChildName to ',CurrChildName);
      SetChildName(CurrChildName);

      if (pttCheckNrOfItems in FTestTypes) then
      begin
        CheckNrOfItems(CurrErrCnt, ErrorLog);
        ErrorCount := CurrErrCnt + ErrorCount;
        ThisErrCnt := ThisErrCnt + CurrErrCnt;
      end;

      if (pttCheckFormatArgs in FTestTypes) then
      begin
        CheckFormatArgs(CurrErrCnt, CurrNonFuzzyErrCnt, ErrorLog);
        ErrorCount := CurrErrCnt + ErrorCount;
        ThisErrCnt := ThisErrCnt + CurrErrCnt;
        NonFuzzyErrorCount := CurrNonFuzzyErrCnt + NonFuzzyErrorCount;
      end;


      if (pttCheckMissingIdentifiers in FTestTypes) then
      begin
        CheckMissingIdentifiers(CurrErrCnt, ErrorLog);
        ErrorCount := CurrErrCnt + ErrorCount;
        ThisErrCnt := ThisErrCnt + CurrErrCnt;
      end;

      if (pttCheckMismatchedOriginals in FTestTypes) then
      begin
        CheckMismatchedOriginals(CurrErrCnt, ErrorLog);
        ErrorCount := CurrErrCnt + ErrorCount;
        ThisErrCnt := ThisErrCnt + CurrErrCnt;
      end;

      //Always run this as the last test please
      TranslatedCount := FChild.Statistics.Translated;
      UntranslatedCount := FChild.Statistics.Untranslated;
      FuzzyCount := FChild.Statistics.Fuzzy;
      CheckStatistics(ThisErrCnt);
       {
        if (ptt in FTestTypes) then
        begin
          Check(CurrErrCnt, ErrorLog);
          ErrorCount := CurrErrCnt + ErrorCount;
        end;
        }
    end;
    //Generate statistics
    if FPoFamilyStats.Count > 0 then
      FPoFamilyStats.AddStatisticsToLog(StatLog);
  finally
    SL.Free;
  end;
  //debugln('TPoFamilyRunTests: ErrorCount = ',DbgS(ErrorCount));
end;

procedure InitTestOptions;
var
  Index: TPoTestType;
begin
  for Index := Low(TPoTestType) to High(TPoTestType) do optRunAllTests := optRunAllTests + [Index];
end;

Initialization

InitTestOptions;

end.