* * *

Author Topic: dialogs YES NO translation  (Read 3273 times)

mesiarm

  • New member
  • *
  • Posts: 45
dialogs YES NO translation
« on: June 28, 2013, 03:18:02 pm »
Hi i want to translate Yes No buttons in dialogs. I tried this http://wiki.lazarus.freepascal.org/Translations_/_i18n_/_localizations_for_programs#Translating_at_start_of_program But I don´t want to still have to keep lclstrconsts file with my program. So I tried this http://wiki.lazarus.freepascal.org/Translations_/_i18n_/_localizations_for_programs#Compiling_po_files_into_the_executable i created lrs file of  lclstrconsts but i dont´t know how to create unit, which is described. I also want to use it for all units and that tutorial is bit perplexing I think

Blaazen

  • Hero Member
  • *****
  • Posts: 2370
  • POKE 54296,15
    • Eye-Candy Controls
Re: dialogs YES NO translation
« Reply #1 on: June 28, 2013, 04:08:17 pm »
This is my half-year old post with attached demo: http://forum.lazarus.freepascal.org/index.php/topic,18939.msg107377.html#msg107377
AFAIR you have to add *.po files to resources via lazres and then you have to use the resource file using directive:
Code: [Select]
{$I name_of_resource_file.lrs} - good place is initialization section of the main form.
Lazarus 1.7 r52036M FPC 3.0.0 x86_64-linux-qt Chakra, Qt 4.8.7, Plasma 5.5
Lazarus 1.4RC3 r48629 FPC 2.6.4 i386-win32-win32/win64 Wine 1.7.43
Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

mesiarm

  • New member
  • *
  • Posts: 45
Re: dialogs YES NO translation
« Reply #2 on: June 28, 2013, 07:14:59 pm »
Thank you. I have seen the demo took something from your code, edited  - here it is

Code: [Select]
re:=LazarusResources.Find('lclstrconsts.sk.lrs', 'PO');
if assigned(re) then
try
ass:=TStringStream.Create(re.Value);
FPOFile:=TPOFile.Create(aSS, False);
Translations.TranslateResourceStrings(FPOFile);
finally
 aSS.Free;
FPOFile.Free;
end; 

it is in main Application file and {$I} is in main form in initialization part, but it doesn´t work. should I move it ({$I} or the code above)somewhere else?

Blaazen

  • Hero Member
  • *****
  • Posts: 2370
  • POKE 54296,15
    • Eye-Candy Controls
Re: dialogs YES NO translation
« Reply #3 on: June 28, 2013, 08:07:06 pm »
Are all your paths correct? Do you have enabled i18n in Project Options? Demo didn't work at all (without modifications)?
Lazarus 1.7 r52036M FPC 3.0.0 x86_64-linux-qt Chakra, Qt 4.8.7, Plasma 5.5
Lazarus 1.4RC3 r48629 FPC 2.6.4 i386-win32-win32/win64 Wine 1.7.43
Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

mesiarm

  • New member
  • *
  • Posts: 45
Re: dialogs YES NO translation
« Reply #4 on: June 28, 2013, 09:22:28 pm »
yes, but it doesn´t show any error. I put there only that edited part of code which i pasted there and I use i18n only with other resourcestrings in program and it work. And paradox is that i need it only for two buttons Captions Yes and No  :) 

Bart

  • Hero Member
  • *****
  • Posts: 2598
    • Bart en Mariska's Webstek
Re: dialogs YES NO translation
« Reply #5 on: June 28, 2013, 10:19:44 pm »
From some of my programs.
This is in a separate unit.
I call TranslateLCL in it's initialization code, so just using the unit means LCL get's translated.

First: use LazRes (or glazres, if you have truk: it's a GUI frontend for lazes) and create a .lrs containg the appropriate LCLStrConsts.xx.po file (in my it's filename is po_nl.lrs).

Then use this code:

Code: [Select]
function TryTranslateFromLazarusResource: TTransalateFromLazarusResourceResult;
var
  LRes: TLResource;
  PO: TPOFile;
  SS: TStringStream;
  B: Boolean;
begin
  Result := trResourceNotFound;
  LRes := LazarusResources.Find('lclstrconsts.nl','PO');
  if LRes <> nil then
    begin
    SS := nil;
    PO := nil;
    try
      SS := TStringStream.Create(LRes.Value);
      //don't use TPoFile.Create(SS,True): it will crash on TranslateUnitResourceStrings if you do
      PO := TPoFile.Create(SS, False);
      try
        B := TranslateUnitResourceStrings('lclstrconsts',PO);
        if B then Result := trSuccess else Result := trTranslationError;
      except
        Result := trTranslationError;
        DebugLn('Exception while translating LclStrConsts unit form internal resources.');
      end;
    finally
      if Assigned(SS) then SS.Free;
      if Assigned(PO) then PO.Free;
    end;
  end;
end;

procedure TranslateLCL;
var
  PODirectory, POFile, AppPath: String;
  i: Integer;
  Found: Boolean;
  ResResult: TTransalateFromLazarusResourceResult;
begin
  ResResult := TryTranslatefromLazarusResource;
  case ResResult of
    trSuccess:
    begin
      DebugLn('Translated LCL from internal resource.');
      Exit;
    end;
    trResourceNotFound: DebugLn('No internal resource for LCL translation foud. Trying to find po-file instead.');
    trTranslationError: DebugLn('Error while translating internal resource. Trying to find po-file instead');
  end;
  ....
  .....  //skipping some code that tries to find a po file on disk somewhere
end;

It is a modification of the code that was in the wiki (at that point in time), because the code in the wiki segfaulted.
I still use it today.

All you have to do if a new version of Lazarus gets out is updating the .lrs file and rebuild the unit.

(You can find the entire unit at http://svn.code.sf.net/p/flyingsheep/code/trunk/MijnLib/nlautotranslation.pp.)

Bart

mesiarm

  • New member
  • *
  • Posts: 45
Re: dialogs YES NO translation
« Reply #6 on: June 29, 2013, 10:55:09 am »
I found it....I only looked to your code and I have found the mistake....I had in my code LRes := LazarusResources.Find('lclstrconsts.nl.lrs','PO');
and there must be it without lrs ...now it works ....thank you :)

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus