* * *

Author Topic: Setting prefered language from code  (Read 9116 times)

Blaazen

  • Hero Member
  • *****
  • Posts: 2377
  • POKE 54296,15
    • Eye-Candy Controls
Setting prefered language from code
« 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
Lazarus 1.9.0 r55618 FPC 3.0.2 x86_64-linux-qt Chakra, Qt 4.8.7, Plasma 5.9.4
Lazarus 1.6.4 r54278 FPC 3.0.2 i386-win32-win32/win64 Wine 2.4










Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/


Blaazen

  • Hero Member
  • *****
  • Posts: 2377
  • POKE 54296,15
    • Eye-Candy Controls
Re: Setting prefered language from code
« Reply #2 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');
Lazarus 1.9.0 r55618 FPC 3.0.2 x86_64-linux-qt Chakra, Qt 4.8.7, Plasma 5.9.4
Lazarus 1.6.4 r54278 FPC 3.0.2 i386-win32-win32/win64 Wine 2.4










Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Leledumbo

  • Hero Member
  • *****
  • Posts: 7627
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Setting prefered language from code
« Reply #3 on: June 30, 2012, 04:15:20 pm »
Quote
I created *.lrs file form *.po files but this line (from wiki) seems not working
  • Have you included it to your program? Either at the start of program or any unit initalization block?
  • Open up the .lrs file, what's the first argument of LazarusResources.Add?

Bart

  • Hero Member
  • *****
  • Posts: 2713
    • Bart en Mariska's Webstek
Re: Setting prefered language from code
« Reply #4 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

Blaazen

  • Hero Member
  • *****
  • Posts: 2377
  • POKE 54296,15
    • Eye-Candy Controls
Re: Setting prefered language from code
« Reply #5 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.
« Last Edit: July 01, 2012, 01:35:07 pm by Blaazen »
Lazarus 1.9.0 r55618 FPC 3.0.2 x86_64-linux-qt Chakra, Qt 4.8.7, Plasma 5.9.4
Lazarus 1.6.4 r54278 FPC 3.0.2 i386-win32-win32/win64 Wine 2.4










Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Leledumbo

  • Hero Member
  • *****
  • Posts: 7627
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Setting prefered language from code
« Reply #6 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

Bart

  • Hero Member
  • *****
  • Posts: 2713
    • Bart en Mariska's Webstek
Re: Setting prefered language from code
« Reply #7 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

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Setting prefered language from code
« Reply #8 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?
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Blaazen

  • Hero Member
  • *****
  • Posts: 2377
  • POKE 54296,15
    • Eye-Candy Controls
Re: Setting prefered language from code
« Reply #9 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.
Lazarus 1.9.0 r55618 FPC 3.0.2 x86_64-linux-qt Chakra, Qt 4.8.7, Plasma 5.9.4
Lazarus 1.6.4 r54278 FPC 3.0.2 i386-win32-win32/win64 Wine 2.4










Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Bart

  • Hero Member
  • *****
  • Posts: 2713
    • Bart en Mariska's Webstek
Re: Setting prefered language from code
« Reply #10 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

peterosa

  • Newbie
  • Posts: 1
Re: Setting prefered language from code
« Reply #11 on: June 22, 2016, 05:26:37 pm »
I am interested in growing my product internationally so i need to build for for language. Does anyone here offer services?
MLS Housing marketing is on the rise in Mississaua. Toronto Lofts are selling at a all time high on MLS Mississauga.

 

Recent

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