Changeset 416


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.
Location:
trunk
Files:
1 deleted
6 edited
10 moved

Legend:

Unmodified
Added
Removed
  • trunk/Integrated.lpi

    r396 r416  
    9999      </Item3>
    100100    </RequiredPackages>
    101     <Units Count="48">
     101    <Units Count="47">
    102102      <Unit0>
    103103        <Filename Value="Integrated.lpr"/>
     
    360360        <IsPartOfProject Value="True"/>
    361361      </Unit46>
    362       <Unit47>
    363         <Filename Value="ULanguages.pas"/>
    364         <IsPartOfProject Value="True"/>
    365       </Unit47>
    366362    </Units>
    367363  </ProjectOptions>
  • trunk/LocalPlayer/Help.pas

    r410 r416  
    290290
    291291  HelpText := TStringTable.Create;
    292   HelpText.LoadFromFile(LocalizedFilePath('Help' + DirectorySeparator + 'help.txt'));
     292  HelpText.LoadFromFile(LocalizedFilePath('Help' + DirectorySeparator + 'Help.txt'));
    293293  hADVHELP := HelpText.Gethandle('ADVHELP');
    294294  hIMPHELP := HelpText.Gethandle('IMPHELP');
  • trunk/Packages/Common/ULanguages.pas

    r396 r416  
    11unit ULanguages;
    22
    3 {$mode objfpc}{$H+}
     3{$mode delphi}{$H+}
    44
    55interface
    66
    77uses
    8   Classes, SysUtils, Contnrs;
     8  Classes, SysUtils, fgl;
    99
    1010type
     
    1515  end;
    1616
    17   { TLanguageList }
    18 
    19   TLanguageList = class(TObjectList)
     17  { TLanguages }
     18
     19  TLanguages = class(TFPGObjectList<TLanguage>)
    2020    function SearchByCode(ACode: string): TLanguage;
    2121    procedure AddNew(Code: string; Name: string);
    22     constructor Create;
     22    constructor Create(FreeObjects: Boolean = True);
    2323  end;
    2424
     
    223223
    224224
    225 { TLanguageList }
    226 
    227 function TLanguageList.SearchByCode(ACode: string): TLanguage;
     225{ TLanguages }
     226
     227function TLanguages.SearchByCode(ACode: string): TLanguage;
    228228var
    229229  I: Integer;
     
    235235end;
    236236
    237 procedure TLanguageList.AddNew(Code: string; Name: string);
     237procedure TLanguages.AddNew(Code: string; Name: string);
    238238var
    239239  NewItem: TLanguage;
     
    245245end;
    246246
    247 constructor TLanguageList.Create;
     247constructor TLanguages.Create(FreeObjects: Boolean);
    248248begin
    249   inherited Create;
     249  inherited;
    250250  AddNew('', SLangAuto);
    251251  AddNew('aa', SLang_aa);
  • 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
  • trunk/Settings.pas

    r408 r416  
    5858{$R *.lfm}
    5959
     60uses
     61  Start;
     62
    6063var
    6164  SFullScreen, SGamma, SRestartMsg, SShortCutPrimary, SShortCutSecondary,
     
    193196begin
    194197  ReloadLanguages;
    195   Languages.LoadToStrings(ListLanguages.Items);
     198  StartDlg.Translator.LanguageListToStrings(ListLanguages.Items);
    196199  ListLanguages.Font.Color := MainTexture.ColorMark;
    197200  ListKeyBindings.Font.Color := MainTexture.ColorMark;
     
    260263procedure TSettingsDlg.LoadData;
    261264begin
    262   ListLanguages.ItemIndex := Languages.Search(LocaleCode);
    263   if (ListLanguages.ItemIndex = -1) and (Languages.Count > 0) then
    264     ListLanguages.ItemIndex := 0;
     265  StartDlg.Translator.Language := StartDlg.Translator.Languages.SearchByCode(LocaleCode);
     266  StartDlg.Translator.LanguageListToStrings(ListLanguages.Items, False);
     267  ListLanguages.ItemIndex := ListLanguages.Items.IndexOfObject(StartDlg.Translator.Language);
     268  if ListLanguages.ItemIndex = -1 then ListLanguages.ItemIndex := 0;
    265269  if FullScreen then ButtonFullscreen.ButtonIndex := 3
    266270    else ButtonFullscreen.ButtonIndex := 2;
     
    274278begin
    275279  NeedRestart := Gamma <> LocalGamma;
    276   LocaleCode := Languages[ListLanguages.ItemIndex].ShortName;
     280  if ListLanguages.ItemIndex <> -1 then begin
     281    StartDlg.Translator.Language := TLanguage(ListLanguages.Items.Objects[ListLanguages.ItemIndex]);
     282    LocaleCode := StartDlg.Translator.Language.Code;
     283  end else begin
     284    StartDlg.Translator.Language := nil;
     285    LocaleCode := '';
     286  end;
    277287  FullScreen := (ButtonFullscreen.ButtonIndex and 1) = 1;
    278288  Gamma := LocalGamma;
  • trunk/Start.pas

    r385 r416  
    77  GameServer, Messg, ButtonBase, ButtonA, ButtonC, ButtonB, Area, Types,
    88  LCLIntf, LCLType, SysUtils, Classes, Graphics, Controls, Forms, StdCtrls,
    9   Menus, Registry,  DrawDlg, fgl, Protocol, UMiniMap, UBrain;
     9  Menus, Registry,  DrawDlg, fgl, Protocol, UMiniMap, UBrain, UTranslator;
    1010
    1111type
     
    133133    procedure LoadConfig;
    134134    procedure SaveConfig;
     135    procedure LoadLanguages;
    135136    procedure LoadAiBrainsPictures;
    136137    procedure UpdateInterface;
     
    138139  public
    139140    EmptyPicture: TBitmap;
     141    Translator: TTranslator;
    140142    procedure UpdateFormerGames;
    141143    procedure UpdateMaps;
     
    149151
    150152uses
    151   Global, Directories, Direct, ScreenTools, Inp, Back, Settings, UKeyBindings;
     153  Global, Directories, Direct, ScreenTools, Inp, Back, Settings, UKeyBindings,
     154  ULanguages;
    152155
    153156{$R *.lfm}
     
    226229  LoadConfig;
    227230  LoadAssets;
     231  LoadLanguages;
    228232
    229233  ActionsOffered := [maConfig, maManual, maCredits, maWeb];
     
    325329begin
    326330  SaveConfig;
     331  FreeAndNil(Translator);
    327332  FreeAndNil(FormerGames);
    328333  FreeAndNil(Maps);
     
    454459
    455460  KeyBindings.SaveToRegistry(HKEY_CURRENT_USER, AppRegistryKey + '\KeyBindings');
     461end;
     462
     463procedure TStartDlg.LoadLanguages;
     464var
     465  I: Integer;
     466begin
     467  Translator := TTranslator.Create(nil);
     468  with Translator, Languages do begin
     469    AddNew('zh-Hant', 'Traditional Chinese');
     470    AddNew('zh-Hans', 'Simplified Chinese');
     471    SearchByCode('').Available := True;
     472
     473    for I := 1 to Languages.Count - 1 do
     474    with Languages[I] do begin
     475      Available := DirectoryExists(HomeDir + 'Localization' + DirectorySeparator + Code) or (Code = 'en');
     476    end;
     477  end;
    456478end;
    457479
Note: See TracChangeset for help on using the changeset viewer.