Ignore:
Timestamp:
Nov 29, 2023, 2:35:44 PM (12 months ago)
Author:
chronos
Message:
  • Modified: HighDpi branch updated to trunk version.
File:
1 moved

Legend:

Unmodified
Added
Removed
  • branches/highdpi/Packages/Common/Translator.pas

    r462 r463  
    1 unit UTranslator;
    2 
    3 {$mode Delphi}{$H+}
     1unit Translator;
    42
    53interface
    64
    75uses
    8   UDpiControls, Classes, SysUtils, Forms, ExtCtrls, Controls, Contnrs, LazFileUtils, LazUTF8,
    9   Translations, TypInfo, Dialogs, FileUtil, LCLProc, ULanguages, LCLType,
    10   LCLVersion;
     6  UDpiControls, Classes, SysUtils, Forms, ExtCtrls, Controls, LazFileUtils, LazUTF8,
     7  Translations, TypInfo, Dialogs, FileUtil, LCLProc, Languages, LCLType,
     8  LCLVersion, Generics.Collections;
    119
    1210type
    1311  THandleStringEvent = function (AValue: string): string of object;
     12
     13  TPoFiles = class(TObjectList<TPOFile>)
     14  end;
    1415
    1516  { TComponentExcludes }
     
    2425  { TComponentExcludesList }
    2526
    26   TComponentExcludesList = class(TObjectList)
     27  TComponentExcludesList = class(TObjectList<TComponentExcludes>)
    2728    function FindByClassType(AClassType: TClass): TComponentExcludes;
    2829    procedure DumpToStrings(Strings: TStrings);
     
    3637    FOnAutomaticLanguage: THandleStringEvent;
    3738    FOnTranslate: TNotifyEvent;
    38     FPOFilesFolder: string;
    39     FPOFiles: TObjectList; // TObjectList<TPOFile>;
     39    FPoFilesFolder: string;
     40    FPoFiles: TPoFiles;
    4041    function GetLocale: string;
    4142    function GetLocaleShort: string;
     
    5051  public
    5152    ComponentExcludes: TComponentExcludesList;
    52     Languages: TLanguageList;
     53    Languages: TLanguages;
    5354    procedure Translate;
    54     procedure LanguageListToStrings(Strings: TStrings);
     55    procedure LanguageListToStrings(Strings: TStrings; WithCode: Boolean = True);
    5556    procedure TranslateResourceStrings(PoFileName: string);
    5657    procedure TranslateUnitResourceStrings(UnitName: string; PoFileName: string);
     
    6364    destructor Destroy; override;
    6465  published
    65     property POFilesFolder: string read FPOFilesFolder write SetPOFilesFolder;
     66    property POFilesFolder: string read FPoFilesFolder write SetPOFilesFolder;
    6667    property Language: TLanguage read FLanguage write SetLanguage;
    6768    property OnTranslate: TNotifyEvent read FOnTranslate write FOnTranslate;
     
    7172
    7273procedure Register;
     74
    7375
    7476implementation
     
    117119destructor TComponentExcludes.Destroy;
    118120begin
    119   PropertyExcludes.Free;
    120   inherited Destroy;
     121  FreeAndNil(PropertyExcludes);
     122  inherited;
    121123end;
    122124
     
    128130  I, J: Integer;
    129131  Po: TPoFile;
    130   Item: TPOFileItem;
     132  Item: TPoFileItem;
    131133begin
    132134  TranslateComponentRecursive(DpiApplication);
     
    134136  // Merge files to single translation file
    135137  try
    136     Po := TPOFile.Create;
    137     for I := 0 to FPOFiles.Count - 1 do
     138    Po := TPoFile.Create;
     139    for I := 0 to FPoFiles.Count - 1 do
    138140    with TPoFile(FPoFiles[I]) do
    139141      for J := 0 to Items.Count - 1 do
     
    162164  SearchMask: string;
    163165begin
    164   FPOFiles.Clear;
     166  FPoFiles.Clear;
    165167  if Assigned(FLanguage) then
    166168  try
    167169    LocaleShort := GetLocaleShort;
    168     //ShowMessage(ExtractFileDir(DpiApplication.ExeName) +
     170    //ShowMessage(ExtractFileDir(Application.ExeName) +
    169171    //  DirectorySeparator + 'Languages' + ' ' + '*.' + LocaleShort + '.po');
    170172    SearchMask := '*';
     
    177179      if FileExists(FileName) and (
    178180      ((LocaleShort = '') and (Pos('.', FileName) = Pos('.po', FileName))) or
    179       (LocaleShort <> '')) then FPOFiles.Add(TPOFile.Create(FileName));
     181      (LocaleShort <> '')) then FPoFiles.Add(TPOFile.Create(FileName));
    180182    end;
    181183  finally
     
    281283var
    282284  Item: TClass;
    283 
    284285  Excludes: TComponentExcludes;
    285286begin
     
    287288  Item := Component.ClassType;
    288289  while Assigned(Item) do begin
    289     //ShowMessage(Component.Name + ', ' + Component.ClassName + ', ' + Item.ClassName + ', ' + PropertyName);
    290290    Excludes := ComponentExcludes.FindByClassType(Item.ClassType);
    291291    if Assigned(Excludes) then begin
     
    301301function TTranslator.GetLangFileDir: string;
    302302begin
    303   Result := FPOFilesFolder;
     303  Result := FPoFilesFolder;
    304304  if Copy(Result, 1, 1) <> DirectorySeparator then
    305305    Result := ExtractFileDir(DpiApplication.ExeName) +
     
    307307end;
    308308
    309 procedure TTranslator.LanguageListToStrings(Strings: TStrings);
     309procedure TTranslator.LanguageListToStrings(Strings: TStrings; WithCode: Boolean = True);
    310310var
    311311  I: Integer;
     
    313313begin
    314314  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;
     315    BeginUpdate;
     316    try
     317      Clear;
     318      for I := 0 to Languages.Count - 1 do
     319      with Languages[I] do
     320        if Available then begin
     321          ItemName := Name;
     322          if WithCode and (Code <> '') then ItemName := ItemName + ' (' + Code + ')';
     323          AddObject(ItemName, Languages[I]);
     324        end;
     325    finally
     326      EndUpdate;
     327    end;
    323328  end;
    324329end;
     
    342347  if Text <> '' then begin
    343348    for I := 0 to FPoFiles.Count - 1 do begin
    344       Result := TPoFile(FPOFiles[I]).Translate(Identifier, Text);
     349      Result := TPoFile(FPoFiles[I]).Translate(Identifier, Text);
    345350      if Result <> Text then Break;
    346351    end;
     
    369374begin
    370375  LangDir := GetLangFileDir;
    371   TLanguage(Languages[0]).Available := True; // Automatic
     376  Languages.SearchByCode('').Available := True; // Automatic
    372377
    373378  for I := 1 to Languages.Count - 1 do
    374   with TLanguage(Languages[I]) do begin
     379  with Languages[I] do begin
    375380    Available := FileExists(LangDir + DirectorySeparator + ExtractFileNameOnly(DpiApplication.ExeName) +
    376381      '.' + Code + ExtensionSeparator + 'po') or (Code = 'en');
     
    381386begin
    382387  inherited;
    383   FPOFiles := TObjectList.Create;
     388  FPoFiles := TPoFiles.Create;
    384389  ComponentExcludes := TComponentExcludesList.Create;
    385   Languages := TLanguageList.Create;
     390  Languages := TLanguages.Create;
    386391  POFilesFolder := 'Languages';
    387392  CheckLanguageFiles;
     
    395400destructor TTranslator.Destroy;
    396401begin
    397   FPOFiles.Free;
    398   Languages.Free;
    399   ComponentExcludes.Free;
    400   inherited Destroy;
     402  FreeAndNil(FPoFiles);
     403  FreeAndNil(Languages);
     404  FreeAndNil(ComponentExcludes);
     405  inherited;
    401406end;
    402407
     
    564569end;
    565570
    566 
    567571end.
    568 
Note: See TracChangeset for help on using the changeset viewer.