Forum > Translations

Setting prefered language from code

(1/3) > >>

Blaazen:
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: ---./project --LANG cs_CS
./project -l en
./project --lang en_EN

--- End code ---
It also works. But how can I set this from code ? I would like to store prefered language in *.ini file.
Thanks

Leledumbo:
http://wiki.freepascal.org/Translations_/_i18n_/_localizations_for_programs

Blaazen:
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: ---r:=LazarusResources.Find('unit1.de','PO');
--- End code ---

Leledumbo:

--- Quote ---I created *.lrs file form *.po files but this line (from wiki) seems not working
--- End quote ---

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

--- End code ---

Bart

Navigation

[0] Message Index

[#] Next page

Go to full version