Ignore:
Timestamp:
Jan 17, 2025, 9:05:54 PM (4 days ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
  • Modified: Remove U prefix from unit names.
  • Modified: Use Gneeric.Collections instead of fgl.
  • Modified: Do not use global form variables.
File:
1 moved

Legend:

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

    r218 r219  
    1 unit UTranslator;
    2 
    3 {$mode delphi}{$H+}
     1unit Translator;
    42
    53interface
    64
    75uses
    8   Classes, SysUtils, Forms, ExtCtrls, Controls, fgl, 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;
    1412
    15   TPoFiles = class(TFPGObjectList<TPOFile>)
     13  TPoFiles = class(TObjectList<TPOFile>)
    1614  end;
    1715
     
    2725  { TComponentExcludesList }
    2826
    29   TComponentExcludesList = class(TFPGObjectList<TComponentExcludes>)
     27  TComponentExcludesList = class(TObjectList<TComponentExcludes>)
    3028    function FindByClassType(AClassType: TClass): TComponentExcludes;
    3129    procedure DumpToStrings(Strings: TStrings);
     
    5048    procedure TranslateProperty(Component: TPersistent; PropInfo: PPropInfo);
    5149    function IsExcluded(Component: TPersistent; PropertyName: string): Boolean;
    52     function GetLangFileDir: string;
     50    function GetLangFileDirs: TStrings;
    5351  public
    5452    ComponentExcludes: TComponentExcludesList;
     
    7371  end;
    7472
     73const
     74  PoExt = '.po';
     75
    7576procedure Register;
    7677
    77 const
    78   PoFileExt = '.po';
    79 
    8078
    8179implementation
     80
     81uses
     82  Common;
    8283
    8384procedure Register;
     
    166167  FileList: TStringList;
    167168  I: Integer;
     169  J: Integer;
    168170  LocaleShort: string;
    169171  SearchMask: string;
     172  LangDirs: TStrings;
    170173begin
    171174  FPoFiles.Clear;
    172   if Assigned(FLanguage) then
    173   try
     175  if Assigned(FLanguage) then begin
    174176    LocaleShort := GetLocaleShort;
    175177    SearchMask := '*';
    176178    if LocaleShort <> '' then SearchMask := SearchMask + '.' + LocaleShort;
    177     SearchMask := SearchMask + PoFileExt;
    178     FileList := FindAllFiles(GetLangFileDir, SearchMask);
    179     for I := 0 to FileList.Count - 1 do begin
    180       FileName := FileList[I];
    181       if FileExists(FileName) and (
    182       ((LocaleShort = '') and (Pos('.', FileName) = Pos(PoFileExt, FileName))) or
    183       (LocaleShort <> '')) then FPoFiles.Add(TPOFile.Create(FileName));
    184     end;
    185   finally
    186     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;
    187196  end;
    188197end;
     
    245254  I: Integer;
    246255begin
     256
    247257//  PropInfo^.Name;
    248258  // Using IsDefaultPropertyValue will tell us if we should write out
     
    300310end;
    301311
    302 function TTranslator.GetLangFileDir: string;
    303 begin
    304   Result := FPoFilesFolder;
    305   if not FilenameIsAbsolute(Result) then
    306     Result := ExtractFileDir(Application.ExeName) + DirectorySeparator + Result;
     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) and (Copy(Result[I], 2, 2) <> ':\') then
     325      Result[I] := ExtractFileDir(Application.ExeName) +
     326        DirectorySeparator + Result[I];
     327  end;
    307328end;
    308329
     
    371392var
    372393  I: Integer;
    373   LangDir: string;
    374 begin
    375   LangDir := GetLangFileDir;
     394  J: Integer;
     395  LangDirs: TStrings;
     396begin
     397  LangDirs := GetLangFileDirs;
    376398  Languages.SearchByCode('').Available := True; // Automatic
    377399
    378400  for I := 1 to Languages.Count - 1 do
    379401  with Languages[I] do begin
    380     Available := FileExists(LangDir + DirectorySeparator + ExtractFileNameOnly(Application.ExeName) +
    381       '.' + Code + PoFileExt) or (Code = 'en');
    382   end;
     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;
    383411end;
    384412
     
    468496
    469497  begin
     498
    470499    // ParamStrUTF8(0) is said not to work properly in linux, but I've tested it
    471500    Result := ExtractFilePath(ParamStrUTF8(0)) + LangID +
     
    568597end;
    569598
    570 
    571599end.
    572 
Note: See TracChangeset for help on using the changeset viewer.