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:
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