Lazarus
Miscellaneous => Translations => Topic started 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:
./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
-
http://wiki.freepascal.org/Translations_/_i18n_/_localizations_for_programs
-
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:
r:=LazarusResources.Find('unit1.de','PO');
-
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?
-
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):
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
-
@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:
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:
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.
-
Suddenly I remembered I've ever posted this in the past: http://forum.lazarus.freepascal.org/index.php/topic,13388.msg69978.html#msg69978
-
@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
-
@Bart: did you perhaps forgot the nl_Translations unit? Or how are we supposed to get that?
-
@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.
-
@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