From 64656d67ad7d78e8b03a3881611fa32d37721d30 Mon Sep 17 00:00:00 2001 From: maxim Date: Fri, 2 Mar 2018 14:06:19 +0000 Subject: [PATCH] LazUtils, translations.pas unit: reverted 57426 #a377818801, it caused removal of strings which should not be removed git-svn-id: trunk@57431 - --- components/lazutils/translations.pas | 81 +++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/components/lazutils/translations.pas b/components/lazutils/translations.pas index 3b633ffe8f..a0ed4c8d6d 100644 --- a/components/lazutils/translations.pas +++ b/components/lazutils/translations.pas @@ -126,6 +126,7 @@ type protected FItems: TFPList;// list of TPOFileItem FIdentifierLowToItem: TStringToPointerTree; // lowercase identifier to TPOFileItem + FIdentLowVarToItem: TStringHashList; // of TPOFileItem FOriginalToItem: TStringHashList; // of TPOFileItem FCharSet: String; FHeader: TPOFileItem; @@ -133,6 +134,7 @@ type FTag: Integer; FModified: boolean; FHelperList: TStringList; + FModuleList: TStringList; // New fields FPoName: string; FNrTranslated: Integer; @@ -140,6 +142,7 @@ type FNrFuzzy: Integer; FNrErrors: Integer; procedure RemoveTaggedItems(aTag: Integer); + procedure RemoveUntaggedModules; function Remove(Index: Integer): TPOFileItem; procedure UpdateCounters(Item: TPOFileItem; Removed: Boolean); // used by pochecker @@ -166,6 +169,8 @@ type procedure FillItem(var CurrentItem: TPOFileItem; Identifier, Original, Translation, Comments, Context, Flags, PreviousID: string; LineNr: Integer = -1); procedure UpdateTranslation(BasePOFile: TPOFile); + procedure ClearModuleList; + procedure AddToModuleList(Identifier: string); procedure UntagAll; procedure RemoveIdentifier(const AIdentifier: string); @@ -674,6 +679,44 @@ end; { TPOFile } +procedure TPOFile.RemoveUntaggedModules; +var + Module: string; + Item,VItem: TPOFileItem; + i, p: Integer; + VarName: String; +begin + if FModuleList=nil then + exit; + + // remove all module references that were not tagged + for i:=FItems.Count-1 downto 0 do begin + Item := TPOFileItem(FItems[i]); + p := pos('.',Item.IdentifierLow); + if P=0 then + continue; // module not found (?) + + Module :=LeftStr(Item.IdentifierLow, p-1); + if (FModuleList.IndexOf(Module)<0) then + continue; // module was not modified this time + + if Item.Tag=FTag then + continue; // PO item was updated + + // this item is not more in updated modules, delete it + FIdentifierLowToItem.Remove(Item.IdentifierLow); + // delete it also from VarToItem + VarName := RightStr(Item.IdentifierLow, Length(Item.IdentifierLow)-P); + VItem := TPoFileItem(FIdentLowVarToItem.Data[VarName]); + if (VItem=Item) then + FIdentLowVarToItem.Remove(VarName); + + FOriginalToItem.Remove(Item.Original, Item); + FItems.Delete(i); + Item.Free; + end; +end; + function TPOFile.GetCount: Integer; begin Result := FItems.Count; @@ -711,6 +754,7 @@ begin FAllowChangeFuzzyFlag:=true; FItems:=TFPList.Create; FIdentifierLowToItem:=TStringToPointerTree.Create(true); + FIdentLowVarToItem:=TStringHashList.Create(true); FOriginalToItem:=TStringHashList.Create(true); end; @@ -750,6 +794,8 @@ destructor TPOFile.Destroy; var i: Integer; begin + if FModuleList<>nil then + FModuleList.Free; if FHelperList<>nil then FHelperList.Free; if FHeader<>nil then @@ -757,6 +803,7 @@ begin for i:=0 to FItems.Count-1 do TObject(FItems[i]).Free; FItems.Free; + FIdentLowVarToItem.Free; FIdentifierLowToItem.Free; FOriginalToItem.Free; inherited Destroy; @@ -1012,10 +1059,15 @@ begin end; function TPOFile.Remove(Index: Integer): TPOFileItem; +var + P: Integer; begin Result := TPOFileItem(FItems[Index]); FOriginalToItem.Remove(Result.Original, Result); FIdentifierLowToItem.Remove(Result.IdentifierLow); + P := Pos('.', Result.IdentifierLow); + if P>0 then + FIdentLowVarToItem.Remove(Copy(Result.IdentifierLow, P+1, Length(Result.IdentifierLow))); FItems.Delete(Index); UpdateCounters(Result, True); end; @@ -1312,6 +1364,7 @@ var end; begin + ClearModuleList; UntagAll; if (SType = stLrj) or (SType = stRsj) then // .lrj/.rsj file @@ -1397,7 +1450,7 @@ begin end; end; - RemoveTaggedItems(0); + RemoveUntaggedModules; end; procedure TPOFile.SaveToStrings(OutLst: TStrings); @@ -1583,9 +1636,12 @@ procedure TPOFile.FillItem(var CurrentItem: TPOFileItem; Identifier, Original, var FoundItem: TPOFileItem; NewItem: boolean; + P: SizeInt; begin NewItem := false; + AddToModuleList(Identifier); + FoundItem := TPOFileItem(FOriginalToItem.Data[Original]); if CurrentItem = nil then @@ -1639,6 +1695,9 @@ begin //debugln(['TPOFile.FillItem Identifier=',Identifier,' Orig="',dbgstr(OriginalValue),'" Transl="',dbgstr(TranslatedValue),'"']); FIdentifierLowToItem[CurrentItem.IdentifierLow]:=CurrentItem; + P := Pos('.', Identifier); + if P>0 then + FIdentLowVarToItem.Add(copy(CurrentItem.IdentifierLow, P+1, Length(CurrentItem.IdentifierLow)), CurrentItem); end; if Original <> '' then @@ -1661,6 +1720,7 @@ var i: Integer; begin UntagAll; + ClearModuleList; for i:=0 to BasePOFile.Items.Count-1 do begin Item := TPOFileItem(BasePOFile.Items[i]); UpdateItem(Item.IdentifierLow, Item.Original); @@ -1668,6 +1728,25 @@ begin RemoveTaggedItems(0); // get rid of any item not existing in BasePOFile end; +procedure TPOFile.ClearModuleList; +begin + if FModuleList<>nil then + FModuleList.Clear; +end; + +procedure TPOFile.AddToModuleList(Identifier: string); +var + p: Integer; +begin + if FModuleList=nil then begin + FModuleList := TStringList.Create; + FModuleList.Duplicates:=dupIgnore; + end; + p := pos('.', Identifier); + if p>0 then + FModuleList.Add(LeftStr(Identifier, P-1)); +end; + procedure TPOFile.UntagAll; var Item: TPOFileItem;