Recent

Author Topic: [SOLVED] How does one trigger the translation on a runtime only package?  (Read 1069 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1331
  • Professional amateur ;-P
Hey Y'All,

I've added the necessary PO files to translate my fp-humanize package.

I'm aware of how to trigger the translations on a GUI app:
  • Enable the i18n setting on the project options
  • Create the necessary files on the folder you specify on the above option
  • Add the DefaultTranslator unit on the form/forms you want translated

Alas, I've never did it for a runtime only package.

I've had a look at the DefaultTranslator and LCLTranslator units, to se what's being done there, so I could maybe repeat it...
It confused me more that it gave me a solution.

I do not want to add a dependency on LCL on a package that is completely just calls to formatting stuff.
So, please, do not suggest using SetDefaultLang, since that would need a dependency on LCL.

Is there a similar function that exists that is not dependant on the LCL?

Cheers,
Gus
« Last Edit: June 28, 2025, 05:17:50 pm by Gustavo 'Gus' Carreno »

wp

  • Hero Member
  • *****
  • Posts: 13264
Re: How does one trigger the translation on a runtime only package?
« Reply #1 on: June 28, 2025, 03:02:26 pm »
Unit "translations" does all the work for the LCLTranslator/DefaultTranslator. It belongs to the LazUtils package of Lazarus - note that this is independent of the LCL; you'll probably need LazUtils anyway for better UTF8 support. Call "TranslateUnitResourceStrings" for all units containing resource strings; to get the current language call "GetLanguageID" from the same unit. See attached mini commandline project.
« Last Edit: June 28, 2025, 03:13:29 pm by wp »

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1331
  • Professional amateur ;-P
Re: How does one trigger the translation on a runtime only package?
« Reply #2 on: June 28, 2025, 03:59:05 pm »
Hey WP,

Unit "translations" does all the work for the LCLTranslator/DefaultTranslator. It belongs to the LazUtils package of Lazarus - note that this is independent of the LCL; you'll probably need LazUtils anyway for better UTF8 support. Call "TranslateUnitResourceStrings" for all units containing resource strings; to get the current language call "GetLanguageID" from the same unit. See attached mini commandline project.

This is awesome to know!! And I'm keeping this is a very secure place ;)

Ok, but we have a major problem: I've implemented what you've just gave me and I still get en when I call GetLanguageID.LanguageCode from the package !!
I'm running the Demo app with -l pt, and the GUI Demo app does pick up on the Portuguese language.

But what is reported from the package is en, not pt.

Sorry for throwing you a curve ball here!!!

Cheers,
Gus

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1331
  • Professional amateur ;-P
Re: How does one trigger the translation on a runtime only package?
« Reply #3 on: June 28, 2025, 05:17:20 pm »
Hey WP,

After having had a bit of a look at what GetLanguageID.LanguageCode does, I realised it was not taking the command line into account.
It was using LC_ALL and shite to get the system, but nothing from the command line!!

So I added this:
Code: Pascal  [Select][+][-]
  1. procedure Translate(ALang: string);
  2. var
  3.   folder: String;
  4. begin
  5.   if ALang = EmptyStr then exit;
  6.  
  7.   folder:= EmptyStr;
  8.   if DirectoryExists('locale') then
  9.     folder:= 'locale';
  10.   if DirectoryExists('languages') then
  11.     folder:= 'languages';
  12.   if folder = EmptyStr then exit;
  13.  
  14.   TranslateUnitResourceStrings('humanize', Format('%s/humanize.%s.po', [
  15.     folder,
  16.     ALang
  17.   ]));
  18. end;
  19.  
  20. function GetLanguageFromCommandLine: String;
  21. var
  22.   index: Integer;
  23. begin
  24.   Result:= EmptyStr;
  25.   for index:= 0 to ParamCount do
  26.   begin
  27.     if ParamStr(index) = '-l' then
  28.     begin
  29.       Result:= ParamStr(index + 1);
  30.       break;
  31.     end;
  32.     if Pos('--lang', ParamStr(index)) > 0 then
  33.     begin
  34.       if Pos('=', ParamStr(index)) > 0 then
  35.       begin
  36.         Result:= Copy(ParamStr(index), Pos('=', ParamStr(index)) + 1, Length(ParamStr(index)));
  37.         break;
  38.       end
  39.       else
  40.       begin
  41.         Result:= ParamStr(index + 1);
  42.         break;
  43.       end;
  44.     end;
  45.   end;
  46.   if Result = EmptyStr then
  47.     Result:= GetLanguageID.LanguageCode;
  48. end;

And now it work to perfection !!!

I'm sure that the Result:= ParamStr(index + 1); will probably come to bite me in the arse, but for now it's working !!

CANNOT thank you enough for pointing me in the right direction !!!  :-*  8-)

Cheers,
Gus
« Last Edit: June 28, 2025, 05:39:14 pm by Gustavo 'Gus' Carreno »

 

TinyPortal © 2005-2018