Ignore:
Timestamp:
Aug 22, 2011, 8:35:49 AM (13 years ago)
Author:
george
Message:
  • Modified: CoolTranslator now load all language specific .po files for localization.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • CoolTranslator/UCoolTranslator.pas

    r258 r269  
    3333    FOnTranslate: TNotifyEvent;
    3434    FPOFilesFolder: string;
    35     FPOFile: TPOFile;
     35    FPOFiles: TObjectList; // TObjectList<TPOFile>;
     36    function GetLocale: string;
     37    function GetLocaleShort: string;
    3638    function FindLocaleFileName(LCExt: string): string;
    3739    function GetLocaleFileName(const LangID, LCExt: string): string;
     
    116118
    117119procedure TCoolTranslator.Translate;
    118 begin
    119   TranslateResourceStrings('ULanguages.cs.po');
     120var
     121  I, J: Integer;
     122  Po: TPoFile;
     123begin
    120124  TranslateComponentRecursive(Application);
    121   if Assigned(FPOFile) then
    122     Translations.TranslateResourceStrings(FPOFile);
     125
     126  // Merge files to single translation file
     127  try
     128    Po := TPOFile.Create;
     129    for I := 0 to FPOFiles.Count - 1 do
     130    with TPoFile(FPoFiles[I]) do
     131      for J := 0 to Items.Count - 1 do
     132      with TPoFileItem(Items[J]) do
     133        Po.Add(Identifier, Original, Translation, Comments, Context,
     134          Flags, PreviousID);
     135    Translations.TranslateResourceStrings(Po);
     136  finally
     137    Po.Free;
     138  end;
    123139end;
    124140
     
    126142var
    127143  FileName: string;
    128 begin
    129   FreeAndNil(FPOFile);
    130   if Assigned(FLanguage) then begin
    131     FileName := FindLocaleFileName('.po');
    132     if FileExistsUTF8(FileName) then FPOFile := TPOFile.Create(FileName);
     144  FileList: TStringList;
     145  I: Integer;
     146  LocaleShort: string;
     147begin
     148  FPOFiles.Clear;
     149  if Assigned(FLanguage) then
     150  try
     151    LocaleShort := GetLocaleShort;
     152    //ShowMessage(ExtractFileDir(Application.ExeName) +
     153    //  DirectorySeparator + 'Languages' + ' ' + '*.' + LocaleShort + '.po');
     154    FileList := FindAllFiles(ExtractFileDir(Application.ExeName) +
     155      DirectorySeparator + 'Languages', '*.' + LocaleShort + '.po');
     156    for I := 0 to FileList.Count - 1 do begin
     157      FileName := FileList[I];
     158      //FileName := FindLocaleFileName('.po');
     159      if FileExistsUTF8(FileName) then FPOFiles.Add(TPOFile.Create(FileName));
     160    end;
     161  finally
     162    FileList.Free;
    133163  end;
    134164end;
     
    279309
    280310function TCoolTranslator.TranslateText(Identifier, Text: string): string;
    281 begin
    282   if Assigned(FPOFile) then
    283     Result := FPOFile.Translate(Identifier, Text);
    284   //ShowMessage(Text + ' => ' + Result);
     311var
     312  I: Integer;
     313begin
     314  if Text <> '' then begin
     315    for I := 0 to FPoFiles.Count - 1 do begin
     316      Result := TPoFile(FPOFiles[I]).Translate(Identifier, Text);
     317      if Result <> Text then Break;
     318    end;
     319    if Result = '' then Result := Text;
     320  end else Result := '';
    285321end;
    286322
     
    315351begin
    316352  inherited;
     353  FPOFiles := TObjectList.Create;
    317354  ComponentExcludes := TComponentExcludesList.Create;
    318355  Languages := TLanguageList.Create;
     
    328365destructor TCoolTranslator.Destroy;
    329366begin
    330   FPOFile.Free;
     367  FPOFiles.Free;
    331368  Languages.Free;
    332369  ComponentExcludes.Free;
     
    334371end;
    335372
    336 function TCoolTranslator.FindLocaleFileName(LCExt: string): string;
    337 var
     373function TCoolTranslator.GetLocale: string;
     374var
     375  Lang: string;
     376  I: Integer;
    338377  T: string;
    339   I: Integer;
    340   Lang: string;
    341 begin
    342   Result := '';
     378begin
    343379  // Win32 user may decide to override locale with LANG variable.
    344380  Lang := GetEnvironmentVariableUTF8('LANG');
     
    358394
    359395  if Lang = 'en' then Lang := ''; // English files are without en code
     396  Result := Lang;
     397end;
     398
     399function TCoolTranslator.GetLocaleShort: string;
     400begin
     401  Result := Copy(GetLocale, 1, 2);
     402end;
     403
     404function TCoolTranslator.FindLocaleFileName(LCExt: string): string;
     405var
     406  T: string;
     407  I: Integer;
     408  Lang: string;
     409begin
     410  Result := '';
     411  Lang := GetLocale;
    360412
    361413  Result := GetLocaleFileName(Lang, LCExt);
     
    365417  Result := ChangeFileExt(ParamStrUTF8(0), LCExt);
    366418  if FileExistsUTF8(Result) then
    367     exit;
     419    Exit;
    368420
    369421  Result := '';
Note: See TracChangeset for help on using the changeset viewer.