Lazarus

Miscellaneous => Translations => Topic started by: Blaazen on June 30, 2012, 11:34:50 am

Title: Setting prefered language from code
Post by: Blaazen on June 30, 2012, 11:34:50 am
Hello,
I desing my app. to be translatable. I have *.po files (project.po, project.cs.po and project.en.po). I have DefaultTranslator in uses and set correctly i18n in Project Options . It works.
I can change language from command line like:
Code: [Select]
./project --LANG cs_CS
./project -l en
./project --lang en_EN
It also works. But how can I set this from code ? I would like to store prefered language in *.ini file.
Thanks
Title: Re: Setting prefered language from code
Post by: Leledumbo on June 30, 2012, 01:51:25 pm
http://wiki.freepascal.org/Translations_/_i18n_/_localizations_for_programs
Title: Re: Setting prefered language from code
Post by: Blaazen on June 30, 2012, 02:49:45 pm
Thanks. Yes, I know this wiki. Now I'm trying to follow the part Compiling po files into the executable but I cannot make it work of some reason. I created *.lrs file form *.po files but this line (from wiki) seems not working:
Code: [Select]
r:=LazarusResources.Find('unit1.de','PO');
Title: Re: Setting prefered language from code
Post by: Leledumbo on June 30, 2012, 04:15:20 pm
Quote
I created *.lrs file form *.po files but this line (from wiki) seems not working
Title: Re: Setting prefered language from code
Post by: Bart on June 30, 2012, 06:28:54 pm
The example on the wiki (at least in the past) segfaulted for me.

I ended up with this (I only include the .nl.po into the resource):

Code: [Select]
type
  TTransalateFromLazarusResourceResult = (trSuccess, trResourceNotFound, trTranslationError);

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; 

Bart
Title: Re: Setting prefered language from code
Post by: Blaazen on July 01, 2012, 12:30:56 pm
@Leledumbo
Yes. I have {$I project.lrs}  in initialization section of my main form. I also changed Project Options ... / Miscelaneous "Resource type of project" to *.lrs include files.
There are two big block in *.lrs file: the first begins with "LazarusResources.Add('project.cs','PO',[   " and the second "LazarusResources.Add('project.en','PO',[   ", it also contains correct captions (czech and english) so I think it is correct.

@Bart
Thanks for code, it is compilable but it does not translate.

This snippet from wiki translates well the LCLStrConsts:
Code: [Select]
PODirectory :='/home/v1/Lazarus_Qt/lazarus/lcl/languages/';
  GetLanguageIDs(Lang, FallbackLang);
  Translations.TranslateUnitResourceStrings('LCLStrConsts', PODirectory + 'lclstrconsts.%s.po', Lang, FallbackLang);

but I would like to make it work for my own MenuItems etc. I also checked the executable and both czech and english strings are there (that's good!).

OK. I can use the second method with DefaultTranslator and distribute *.po files with executable but - as I mentioned in the very first post - I need to know how to change the language from code (I don't want to use command-line parameter).

Thanks.

EDIT:
If I write this code:
Code: [Select]
  LRes := LazarusResources.Find('project.cs','PO');
  writeln(LRes.Name+' '+LRes.Value+' '+LRes.ValueType);     
it displays all translated strings correctly (to console) but the form is not translated.
Title: Re: Setting prefered language from code
Post by: Leledumbo on July 01, 2012, 02:10:02 pm
Suddenly I remembered I've ever posted this in the past: http://forum.lazarus.freepascal.org/index.php/topic,13388.msg69978.html#msg69978
Title: Re: Setting prefered language from code
Post by: Bart on July 01, 2012, 04:22:53 pm
@Bart
Thanks for code, it is compilable but it does not translate.
Did you inspect the function result of TryTranslateFromLazarusResource?

For me always worked fine (and still does in Lazarus 1.1 r37837 FPC 2.6.0 i386-win32-win32/win64).

See attached unit.
Include (use) it in a project, and it should translate the lcl to dutch.
Try a MessageDlg('Test',mtInformation,[mbClose,mbYes,mbIgnore],0) for instance to see the result.
Button captions should read "Ja", "Negeer", "Sluiten" instead of "Yes", "Ignore", "Close".
Uncheck Win32 GUI aplication in project options, so you can see debug output.

Bart
Title: Re: Setting prefered language from code
Post by: BigChimp on July 01, 2012, 04:41:38 pm
@Bart: did you perhaps forgot the nl_Translations unit? Or how are we supposed to get that?
Title: Re: Setting prefered language from code
Post by: Blaazen on July 01, 2012, 07:12:01 pm
@BigChimp: simply change nl_Translations to Translations.

@Bart: Yes, it works as you said. LCL is translated to dutch, and it (somehow?!) translated my own menu items to czech, so I'm nearer. Due to testing my code became a little ugly, I'll cleanup now.

Thanks.
Title: Re: Setting prefered language from code
Post by: Bart on July 01, 2012, 07:28:17 pm
@Bart: did you perhaps forgot the nl_Translations unit? Or how are we supposed to get that?

Sorry, it will do nicely with (regular) translations unit.
(nl_translations unit is a remnant of the past and not needed anymore. Thanks for pointing it out).

Bart
TinyPortal © 2005-2018