Ignore:
Timestamp:
Jun 7, 2024, 11:59:43 AM (5 months ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
File:
1 moved

Legend:

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

    r84 r85  
    1 unit UTranslator;
    2 
    3 {$mode Delphi}{$H+}
     1unit Translator;
    42
    53interface
    64
    75uses
    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;
    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;
     
    4748    procedure TranslateProperty(Component: TPersistent; PropInfo: PPropInfo);
    4849    function IsExcluded(Component: TPersistent; PropertyName: string): Boolean;
    49     function GetLangFileDir: string;
     50    function GetLangFileDirs: TStrings;
    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;
     
    7071  end;
    7172
     73const
     74  PoExt = '.po';
     75
    7276procedure Register;
    7377
     78
    7479implementation
     80
     81uses
     82  Common;
    7583
    7684procedure Register;
     
    117125destructor TComponentExcludes.Destroy;
    118126begin
    119   PropertyExcludes.Free;
    120   inherited Destroy;
     127  FreeAndNil(PropertyExcludes);
     128  inherited;
    121129end;
    122130
     
    128136  I, J: Integer;
    129137  Po: TPoFile;
    130   Item: TPOFileItem;
     138  Item: TPoFileItem;
    131139begin
    132140  TranslateComponentRecursive(Application);
     
    134142  // Merge files to single translation file
    135143  try
    136     Po := TPOFile.Create;
    137     for I := 0 to FPOFiles.Count - 1 do
     144    Po := TPoFile.Create;
     145    for I := 0 to FPoFiles.Count - 1 do
    138146    with TPoFile(FPoFiles[I]) do
    139147      for J := 0 to Items.Count - 1 do
     
    159167  FileList: TStringList;
    160168  I: Integer;
     169  J: Integer;
    161170  LocaleShort: string;
    162171  SearchMask: string;
    163 begin
    164   FPOFiles.Clear;
    165   if Assigned(FLanguage) then
    166   try
     172  LangDirs: TStrings;
     173begin
     174  FPoFiles.Clear;
     175  if Assigned(FLanguage) then begin
    167176    LocaleShort := GetLocaleShort;
    168     //ShowMessage(ExtractFileDir(Application.ExeName) +
    169     //  DirectorySeparator + 'Languages' + ' ' + '*.' + LocaleShort + '.po');
    170177    SearchMask := '*';
    171178    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;
    183196  end;
    184197end;
     
    281294var
    282295  Item: TClass;
    283 
    284296  Excludes: TComponentExcludes;
    285297begin
     
    287299  Item := Component.ClassType;
    288300  while Assigned(Item) do begin
    289     //ShowMessage(Component.Name + ', ' + Component.ClassName + ', ' + Item.ClassName + ', ' + PropertyName);
    290301    Excludes := ComponentExcludes.FindByClassType(Item.ClassType);
    291302    if Assigned(Excludes) then begin
     
    299310end;
    300311
    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);
     312function TTranslator.GetLangFileDirs: TStrings;
     313var
     314  I: Integer;
     315begin
     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;
     328end;
     329
     330procedure TTranslator.LanguageListToStrings(Strings: TStrings; WithCode: Boolean = True);
    310331var
    311332  I: Integer;
     
    313334begin
    314335  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;
    323349  end;
    324350end;
     
    342368  if Text <> '' then begin
    343369    for I := 0 to FPoFiles.Count - 1 do begin
    344       Result := TPoFile(FPOFiles[I]).Translate(Identifier, Text);
     370      Result := TPoFile(FPoFiles[I]).Translate(Identifier, Text);
    345371      if Result <> Text then Break;
    346372    end;
     
    366392var
    367393  I: Integer;
    368   LangDir: string;
    369 begin
    370   LangDir := GetLangFileDir;
    371   TLanguage(Languages[0]).Available := True; // Automatic
     394  J: Integer;
     395  LangDirs: TStrings;
     396begin
     397  LangDirs := GetLangFileDirs;
     398  Languages.SearchByCode('').Available := True; // Automatic
    372399
    373400  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;
    378411end;
    379412
     
    381414begin
    382415  inherited;
    383   FPOFiles := TObjectList.Create;
     416  FPoFiles := TPoFiles.Create;
    384417  ComponentExcludes := TComponentExcludesList.Create;
    385   Languages := TLanguageList.Create;
     418  Languages := TLanguages.Create;
    386419  POFilesFolder := 'Languages';
    387420  CheckLanguageFiles;
     
    395428destructor TTranslator.Destroy;
    396429begin
    397   FPOFiles.Free;
    398   Languages.Free;
    399   ComponentExcludes.Free;
    400   inherited Destroy;
     430  FreeAndNil(FPoFiles);
     431  FreeAndNil(Languages);
     432  FreeAndNil(ComponentExcludes);
     433  inherited;
    401434end;
    402435
     
    564597end;
    565598
    566 
    567599end.
    568 
Note: See TracChangeset for help on using the changeset viewer.