Ignore:
Timestamp:
Jan 18, 2022, 10:45:33 PM (3 years ago)
Author:
chronos
Message:
  • Modified: Updated to build under Lazarus 2.2.0.
  • Modified: Updated Common package to version 0.9.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/UTranslator.pas

    r308 r313  
    11unit UTranslator;
    22
    3 {$mode Delphi}{$H+}
     3{$mode delphi}{$H+}
    44
    55interface
    66
    77uses
    8   Classes, SysUtils, Forms, ExtCtrls, Controls, Contnrs, LazFileUtils, LazUTF8,
     8  Classes, SysUtils, Forms, ExtCtrls, Controls, fgl, LazFileUtils, LazUTF8,
    99  Translations, TypInfo, Dialogs, FileUtil, LCLProc, ULanguages, LCLType,
    1010  LCLVersion;
     
    1212type
    1313  THandleStringEvent = function (AValue: string): string of object;
     14
     15  TPoFiles = class(TFPGObjectList<TPOFile>)
     16  end;
    1417
    1518  { TComponentExcludes }
     
    2427  { TComponentExcludesList }
    2528
    26   TComponentExcludesList = class(TObjectList)
     29  TComponentExcludesList = class(TFPGObjectList<TComponentExcludes>)
    2730    function FindByClassType(AClassType: TClass): TComponentExcludes;
    2831    procedure DumpToStrings(Strings: TStrings);
     
    3639    FOnAutomaticLanguage: THandleStringEvent;
    3740    FOnTranslate: TNotifyEvent;
    38     FPOFilesFolder: string;
    39     FPOFiles: TObjectList; // TObjectList<TPOFile>;
     41    FPoFilesFolder: string;
     42    FPoFiles: TPoFiles;
    4043    function GetLocale: string;
    4144    function GetLocaleShort: string;
     
    5053  public
    5154    ComponentExcludes: TComponentExcludesList;
    52     Languages: TLanguageList;
     55    Languages: TLanguages;
    5356    procedure Translate;
    54     procedure LanguageListToStrings(Strings: TStrings);
     57    procedure LanguageListToStrings(Strings: TStrings; WithCode: Boolean = True);
    5558    procedure TranslateResourceStrings(PoFileName: string);
    5659    procedure TranslateUnitResourceStrings(UnitName: string; PoFileName: string);
     
    6366    destructor Destroy; override;
    6467  published
    65     property POFilesFolder: string read FPOFilesFolder write SetPOFilesFolder;
     68    property POFilesFolder: string read FPoFilesFolder write SetPOFilesFolder;
    6669    property Language: TLanguage read FLanguage write SetLanguage;
    6770    property OnTranslate: TNotifyEvent read FOnTranslate write FOnTranslate;
     
    7174
    7275procedure Register;
     76
    7377
    7478implementation
     
    117121destructor TComponentExcludes.Destroy;
    118122begin
    119   PropertyExcludes.Free;
    120   inherited Destroy;
     123  FreeAndNil(PropertyExcludes);
     124  inherited;
    121125end;
    122126
     
    128132  I, J: Integer;
    129133  Po: TPoFile;
    130   Item: TPOFileItem;
     134  Item: TPoFileItem;
    131135begin
    132136  TranslateComponentRecursive(Application);
     
    134138  // Merge files to single translation file
    135139  try
    136     Po := TPOFile.Create;
    137     for I := 0 to FPOFiles.Count - 1 do
     140    Po := TPoFile.Create;
     141    for I := 0 to FPoFiles.Count - 1 do
    138142    with TPoFile(FPoFiles[I]) do
    139143      for J := 0 to Items.Count - 1 do
     
    162166  SearchMask: string;
    163167begin
    164   FPOFiles.Clear;
     168  FPoFiles.Clear;
    165169  if Assigned(FLanguage) then
    166170  try
     
    177181      if FileExists(FileName) and (
    178182      ((LocaleShort = '') and (Pos('.', FileName) = Pos('.po', FileName))) or
    179       (LocaleShort <> '')) then FPOFiles.Add(TPOFile.Create(FileName));
     183      (LocaleShort <> '')) then FPoFiles.Add(TPOFile.Create(FileName));
    180184    end;
    181185  finally
     
    281285var
    282286  Item: TClass;
    283 
    284287  Excludes: TComponentExcludes;
    285288begin
     
    301304function TTranslator.GetLangFileDir: string;
    302305begin
    303   Result := FPOFilesFolder;
     306  Result := FPoFilesFolder;
    304307  if Copy(Result, 1, 1) <> DirectorySeparator then
    305308    Result := ExtractFileDir(Application.ExeName) +
     
    307310end;
    308311
    309 procedure TTranslator.LanguageListToStrings(Strings: TStrings);
     312procedure TTranslator.LanguageListToStrings(Strings: TStrings; WithCode: Boolean = True);
    310313var
    311314  I: Integer;
     
    313316begin
    314317  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;
     318    BeginUpdate;
     319    try
     320      Clear;
     321      for I := 0 to Languages.Count - 1 do
     322      with Languages[I] do
     323        if Available then begin
     324          ItemName := Name;
     325          if WithCode and (Code <> '') then ItemName := ItemName + ' (' + Code + ')';
     326          AddObject(ItemName, Languages[I]);
     327        end;
     328    finally
     329      EndUpdate;
     330    end;
    323331  end;
    324332end;
     
    342350  if Text <> '' then begin
    343351    for I := 0 to FPoFiles.Count - 1 do begin
    344       Result := TPoFile(FPOFiles[I]).Translate(Identifier, Text);
     352      Result := TPoFile(FPoFiles[I]).Translate(Identifier, Text);
    345353      if Result <> Text then Break;
    346354    end;
     
    369377begin
    370378  LangDir := GetLangFileDir;
    371   TLanguage(Languages[0]).Available := True; // Automatic
     379  Languages.SearchByCode('').Available := True; // Automatic
    372380
    373381  for I := 1 to Languages.Count - 1 do
    374   with TLanguage(Languages[I]) do begin
     382  with Languages[I] do begin
    375383    Available := FileExists(LangDir + DirectorySeparator + ExtractFileNameOnly(Application.ExeName) +
    376384      '.' + Code + ExtensionSeparator + 'po') or (Code = 'en');
     
    381389begin
    382390  inherited;
    383   FPOFiles := TObjectList.Create;
     391  FPoFiles := TPoFiles.Create;
    384392  ComponentExcludes := TComponentExcludesList.Create;
    385   Languages := TLanguageList.Create;
     393  Languages := TLanguages.Create;
    386394  POFilesFolder := 'Languages';
    387395  CheckLanguageFiles;
     
    395403destructor TTranslator.Destroy;
    396404begin
    397   FPOFiles.Free;
    398   Languages.Free;
    399   ComponentExcludes.Free;
    400   inherited Destroy;
     405  FreeAndNil(FPoFiles);
     406  FreeAndNil(Languages);
     407  FreeAndNil(ComponentExcludes);
     408  inherited;
    401409end;
    402410
Note: See TracChangeset for help on using the changeset viewer.