Ignore:
Timestamp:
Nov 17, 2021, 10:47:48 AM (3 years ago)
Author:
chronos
Message:
  • Fixed: Use Common package translator to solve duplicate ULanguages unit name.
  • Modified: Also automatically detect new localizations in Localization directory.
  • Fixed: Use localization files with first capital letter to work correctly on Linux.
File:
1 edited

Legend:

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

    r396 r416  
    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;
     
    318321      if Available then begin
    319322        ItemName := Name;
    320         if Code <> '' then ItemName := ItemName + ' (' + Code + ')';
     323        if WithCode and (Code <> '') then ItemName := ItemName + ' (' + Code + ')';
    321324        AddObject(ItemName, Languages[I]);
    322325      end;
     
    342345  if Text <> '' then begin
    343346    for I := 0 to FPoFiles.Count - 1 do begin
    344       Result := TPoFile(FPOFiles[I]).Translate(Identifier, Text);
     347      Result := TPoFile(FPoFiles[I]).Translate(Identifier, Text);
    345348      if Result <> Text then Break;
    346349    end;
     
    369372begin
    370373  LangDir := GetLangFileDir;
    371   TLanguage(Languages[0]).Available := True; // Automatic
     374  Languages.SearchByCode('').Available := True; // Automatic
    372375
    373376  for I := 1 to Languages.Count - 1 do
     
    381384begin
    382385  inherited;
    383   FPOFiles := TObjectList.Create;
     386  FPoFiles := TPoFiles.Create;
    384387  ComponentExcludes := TComponentExcludesList.Create;
    385   Languages := TLanguageList.Create;
     388  Languages := TLanguages.Create;
    386389  POFilesFolder := 'Languages';
    387390  CheckLanguageFiles;
     
    395398destructor TTranslator.Destroy;
    396399begin
    397   FPOFiles.Free;
    398   Languages.Free;
    399   ComponentExcludes.Free;
    400   inherited Destroy;
     400  FreeAndNil(FPoFiles);
     401  FreeAndNil(Languages);
     402  FreeAndNil(ComponentExcludes);
     403  inherited;
    401404end;
    402405
Note: See TracChangeset for help on using the changeset viewer.