Changeset 85 for trunk/Packages/Common/Translator.pas
- Timestamp:
- Jun 7, 2024, 11:59:43 AM (5 months ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/Translator.pas
r84 r85 1 unit UTranslator; 2 3 {$mode Delphi}{$H+} 1 unit Translator; 4 2 5 3 interface 6 4 7 5 uses 8 Classes, SysUtils, Forms, ExtCtrls, Controls, Contnrs,LazFileUtils, LazUTF8,9 Translations, TypInfo, Dialogs, FileUtil, LCLProc, ULanguages, LCLType,10 LCLVersion ;6 Classes, SysUtils, Forms, ExtCtrls, Controls, LazFileUtils, LazUTF8, 7 Translations, TypInfo, Dialogs, FileUtil, LCLProc, Languages, LCLType, 8 LCLVersion, Generics.Collections; 11 9 12 10 type 13 11 THandleStringEvent = function (AValue: string): string of object; 12 13 TPoFiles = class(TObjectList<TPOFile>) 14 end; 14 15 15 16 { TComponentExcludes } … … 24 25 { TComponentExcludesList } 25 26 26 TComponentExcludesList = class(TObjectList )27 TComponentExcludesList = class(TObjectList<TComponentExcludes>) 27 28 function FindByClassType(AClassType: TClass): TComponentExcludes; 28 29 procedure DumpToStrings(Strings: TStrings); … … 36 37 FOnAutomaticLanguage: THandleStringEvent; 37 38 FOnTranslate: TNotifyEvent; 38 FP OFilesFolder: string;39 FP OFiles: TObjectList; // TObjectList<TPOFile>;39 FPoFilesFolder: string; 40 FPoFiles: TPoFiles; 40 41 function GetLocale: string; 41 42 function GetLocaleShort: string; … … 47 48 procedure TranslateProperty(Component: TPersistent; PropInfo: PPropInfo); 48 49 function IsExcluded(Component: TPersistent; PropertyName: string): Boolean; 49 function GetLangFileDir : string;50 function GetLangFileDirs: TStrings; 50 51 public 51 52 ComponentExcludes: TComponentExcludesList; 52 Languages: TLanguage List;53 Languages: TLanguages; 53 54 procedure Translate; 54 procedure LanguageListToStrings(Strings: TStrings );55 procedure LanguageListToStrings(Strings: TStrings; WithCode: Boolean = True); 55 56 procedure TranslateResourceStrings(PoFileName: string); 56 57 procedure TranslateUnitResourceStrings(UnitName: string; PoFileName: string); … … 63 64 destructor Destroy; override; 64 65 published 65 property POFilesFolder: string read FP OFilesFolder write SetPOFilesFolder;66 property POFilesFolder: string read FPoFilesFolder write SetPOFilesFolder; 66 67 property Language: TLanguage read FLanguage write SetLanguage; 67 68 property OnTranslate: TNotifyEvent read FOnTranslate write FOnTranslate; … … 70 71 end; 71 72 73 const 74 PoExt = '.po'; 75 72 76 procedure Register; 73 77 78 74 79 implementation 80 81 uses 82 Common; 75 83 76 84 procedure Register; … … 117 125 destructor TComponentExcludes.Destroy; 118 126 begin 119 PropertyExcludes.Free;120 inherited Destroy;127 FreeAndNil(PropertyExcludes); 128 inherited; 121 129 end; 122 130 … … 128 136 I, J: Integer; 129 137 Po: TPoFile; 130 Item: TP OFileItem;138 Item: TPoFileItem; 131 139 begin 132 140 TranslateComponentRecursive(Application); … … 134 142 // Merge files to single translation file 135 143 try 136 Po := TP OFile.Create;137 for I := 0 to FP OFiles.Count - 1 do144 Po := TPoFile.Create; 145 for I := 0 to FPoFiles.Count - 1 do 138 146 with TPoFile(FPoFiles[I]) do 139 147 for J := 0 to Items.Count - 1 do … … 159 167 FileList: TStringList; 160 168 I: Integer; 169 J: Integer; 161 170 LocaleShort: string; 162 171 SearchMask: string; 163 begin 164 FPOFiles.Clear; 165 if Assigned(FLanguage) then166 try172 LangDirs: TStrings; 173 begin 174 FPoFiles.Clear; 175 if Assigned(FLanguage) then begin 167 176 LocaleShort := GetLocaleShort; 168 //ShowMessage(ExtractFileDir(Application.ExeName) +169 // DirectorySeparator + 'Languages' + ' ' + '*.' + LocaleShort + '.po');170 177 SearchMask := '*'; 171 178 if LocaleShort <> '' then SearchMask := SearchMask + '.' + LocaleShort; 172 SearchMask := SearchMask + '.po'; 173 FileList := FindAllFiles(GetLangFileDir, SearchMask); 174 for I := 0 to FileList.Count - 1 do begin 175 FileName := FileList[I]; 176 //FileName := FindLocaleFileName('.po'); 177 if FileExists(FileName) and ( 178 ((LocaleShort = '') and (Pos('.', FileName) = Pos('.po', FileName))) or 179 (LocaleShort <> '')) then FPOFiles.Add(TPOFile.Create(FileName)); 180 end; 181 finally 182 FileList.Free; 179 SearchMask := SearchMask + PoExt; 180 LangDirs := GetLangFileDirs; 181 for J := 0 to LangDirs.Count - 1 do begin 182 FileList := FindAllFiles(LangDirs[J], SearchMask); 183 try 184 for I := 0 to FileList.Count - 1 do begin 185 FileName := FileList[I]; 186 //FileName := FindLocaleFileName('.po'); 187 if FileExists(FileName) and ( 188 ((LocaleShort = '') and (Pos('.', FileName) = Pos(PoExt, FileName))) or 189 (LocaleShort <> '')) then FPoFiles.Add(TPOFile.Create(FileName)); 190 end; 191 finally 192 FileList.Free; 193 end; 194 end; 195 LangDirs.Free; 183 196 end; 184 197 end; … … 281 294 var 282 295 Item: TClass; 283 284 296 Excludes: TComponentExcludes; 285 297 begin … … 287 299 Item := Component.ClassType; 288 300 while Assigned(Item) do begin 289 //ShowMessage(Component.Name + ', ' + Component.ClassName + ', ' + Item.ClassName + ', ' + PropertyName);290 301 Excludes := ComponentExcludes.FindByClassType(Item.ClassType); 291 302 if Assigned(Excludes) then begin … … 299 310 end; 300 311 301 function TTranslator.GetLangFileDir: string; 302 begin 303 Result := FPOFilesFolder; 304 if Copy(Result, 1, 1) <> DirectorySeparator then 305 Result := ExtractFileDir(Application.ExeName) + 306 DirectorySeparator + Result; 307 end; 308 309 procedure TTranslator.LanguageListToStrings(Strings: TStrings); 312 function TTranslator.GetLangFileDirs: TStrings; 313 var 314 I: Integer; 315 begin 316 Result := TStringList.Create; 317 Result.Delimiter := ';'; 318 Result.StrictDelimiter := True; 319 Result.DelimitedText := FPoFilesFolder; 320 321 for I := 0 to Result.Count - 1 do begin 322 Result[I] := StringReplace(Result[I], '/', DirectorySeparator, [rfReplaceAll]); 323 Result[I] := StringReplace(Result[I], '\', DirectorySeparator, [rfReplaceAll]); 324 if Copy(Result[I], 1, 1) <> DirectorySeparator then 325 Result[I] := ExtractFileDir(Application.ExeName) + 326 DirectorySeparator + Result[I]; 327 end; 328 end; 329 330 procedure TTranslator.LanguageListToStrings(Strings: TStrings; WithCode: Boolean = True); 310 331 var 311 332 I: Integer; … … 313 334 begin 314 335 with Strings do begin 315 Clear; 316 for I := 0 to Languages.Count - 1 do 317 with TLanguage(Languages[I]) do 318 if Available then begin 319 ItemName := Name; 320 if Code <> '' then ItemName := ItemName + ' (' + Code + ')'; 321 AddObject(ItemName, Languages[I]); 322 end; 336 BeginUpdate; 337 try 338 Clear; 339 for I := 0 to Languages.Count - 1 do 340 with Languages[I] do 341 if Available then begin 342 ItemName := Name; 343 if WithCode and (Code <> '') then ItemName := ItemName + ' (' + Code + ')'; 344 AddObject(ItemName, Languages[I]); 345 end; 346 finally 347 EndUpdate; 348 end; 323 349 end; 324 350 end; … … 342 368 if Text <> '' then begin 343 369 for I := 0 to FPoFiles.Count - 1 do begin 344 Result := TPoFile(FP OFiles[I]).Translate(Identifier, Text);370 Result := TPoFile(FPoFiles[I]).Translate(Identifier, Text); 345 371 if Result <> Text then Break; 346 372 end; … … 366 392 var 367 393 I: Integer; 368 LangDir: string; 369 begin 370 LangDir := GetLangFileDir; 371 TLanguage(Languages[0]).Available := True; // Automatic 394 J: Integer; 395 LangDirs: TStrings; 396 begin 397 LangDirs := GetLangFileDirs; 398 Languages.SearchByCode('').Available := True; // Automatic 372 399 373 400 for I := 1 to Languages.Count - 1 do 374 with TLanguage(Languages[I]) do begin 375 Available := FileExists(LangDir + DirectorySeparator + ExtractFileNameOnly(Application.ExeName) + 376 '.' + Code + ExtensionSeparator + 'po') or (Code = 'en'); 377 end; 401 with Languages[I] do begin 402 for J := 0 to LangDirs.Count - 1 do begin 403 if FileExists(LangDirs[J] + DirectorySeparator + ExtractFileNameOnly(Application.ExeName) + 404 '.' + Code + PoExt) or (Code = 'en') then begin 405 Available := True; 406 Continue; 407 end; 408 end; 409 end; 410 LangDirs.Free; 378 411 end; 379 412 … … 381 414 begin 382 415 inherited; 383 FP OFiles := TObjectList.Create;416 FPoFiles := TPoFiles.Create; 384 417 ComponentExcludes := TComponentExcludesList.Create; 385 Languages := TLanguage List.Create;418 Languages := TLanguages.Create; 386 419 POFilesFolder := 'Languages'; 387 420 CheckLanguageFiles; … … 395 428 destructor TTranslator.Destroy; 396 429 begin 397 F POFiles.Free;398 Languages.Free;399 ComponentExcludes.Free;400 inherited Destroy;430 FreeAndNil(FPoFiles); 431 FreeAndNil(Languages); 432 FreeAndNil(ComponentExcludes); 433 inherited; 401 434 end; 402 435 … … 564 597 end; 565 598 566 567 599 end. 568
Note:
See TracChangeset
for help on using the changeset viewer.