Recent

Author Topic: [SOLVED] SetDefaultLang with a filename  (Read 530 times)

CM630

  • Hero Member
  • *****
  • Posts: 1321
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
[SOLVED] SetDefaultLang with a filename
« on: April 17, 2025, 12:25:51 pm »
I am trying to use SetDefaultLang with an absolute path to the PO files.

SetDefaultLang (full_PO_name);  does not work.
SetDefaultLang ('','',full_PO_name);  does not work.
SetDefaultLang(Language_Name,full_path,only_filename_without_a_path); does not work.
SetDefaultLang('',full_path,only_filename_without_a_path); does not work.

https://dsiders.gitlab.io/lazdocsnext/lcl/lcltranslator/setdefaultlang.html says  Language ID requested for translated strings, or '' to use the system default., so for some reason the name of the language must always be supplied as a parameter.
I tried SetDefaultLang(Language_Name,full_path,full_PO_name); but it still does not work.



I looked in function SetDefaultLang(Lang: string; Dir: string = ''; LocaleFileName: string = ''; ForceUpdate: boolean = true): string;
For some reason, it needs a value for Lang :o


I cropped SetDefaultLang to the following procedure:

Code: Pascal  [Select][+][-]
  1. procedure SetAppLang(LocaleFileName: string = '');
  2. var
  3.   LocalTranslator: TUpdateTranslator;
  4.   i: integer;
  5. begin
  6.   LocalTranslator := nil;
  7.   Translations.TranslateResourceStrings(LocaleFileName);
  8.   LocalTranslator := TPOTranslator.Create(LocaleFileName);
  9. //  TranslateLCLResourceStrings(Lang, LocaleFileName);
  10.  
  11.   if (LocalTranslator<>nil) then
  12.   begin
  13.     if Assigned(LRSTranslator) then
  14.       LRSTranslator.Free;
  15.     LRSTranslator := LocalTranslator;
  16.     for i := 0 to Screen.CustomFormCount-1 do
  17.       LocalTranslator.UpdateTranslation(Screen.CustomForms[i]);
  18.     for i := 0 to Screen.DataModuleCount-1 do
  19.       LocalTranslator.UpdateTranslation(Screen.DataModules[i]);
  20.   end;
  21. end;

Now my app is localizable.
But I still wonder if there is a straightforward way. If there is not, do I need the //  TranslateLCLResourceStrings(Lang, LocaleFileName); ? I see no issues without it now, but maybe they will occur.
« Last Edit: April 23, 2025, 08:51:27 am by CM630 »
Лазар 4,0RC2 32 bit (sometimes 64 bit); FPC3,2,2

wp

  • Hero Member
  • *****
  • Posts: 12784
Re: SetDefaultLang with a filename
« Reply #1 on: April 17, 2025, 12:57:13 pm »
You do not tell what the problem is - "does not work" is too unspecific.

One thing which cames to my mind: Do you "use" DefaultTranslator? It calls "SetDefaultLang" with default parameters in the initialization section, i.e. before you call it with your specific path and thus will not find the po files (if that's your problem). Try to remove DefaultTranslator from "uses".

CM630

  • Hero Member
  • *****
  • Posts: 1321
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: SetDefaultLang with a filename
« Reply #2 on: April 17, 2025, 01:48:04 pm »
I have created and attached a sample project.
I found no working combination of the parameters of SetDefaultLang that will change the language of the app.
SetAppLang, the trimmed version of SetDefaultLang changes the language sucesfully (it requires uses ... Translations, LResources).

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   LazFileUtils,
  10.   LCLTranslator, Translations, LResources;
  11.  
  12. resourcestring
  13.   TestString = 'TestString';
  14.  
  15. type
  16.  
  17.   { TForm1 }
  18.  
  19.   TForm1 = class(TForm)
  20.     Button1: TButton;
  21.     Label1: TLabel;
  22.     procedure Button1Click(Sender: TObject);
  23.   private
  24.  
  25.   public
  26.  
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.   LanguageName : string;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. { TForm1 }
  38.  
  39. procedure SetAppLang(LocaleFileName: string = '');
  40. var
  41.   LocalTranslator: TUpdateTranslator;
  42.   i: integer;
  43. begin
  44.   if (LocaleFileName = '') then exit;
  45.   LocalTranslator := nil;
  46.   Translations.TranslateResourceStrings(LocaleFileName);
  47.   LocalTranslator := TPOTranslator.Create(LocaleFileName);
  48. //  TranslateLCLResourceStrings(Lang, LocaleFileName);
  49.  
  50.   if (LocalTranslator<>nil) then
  51.   begin
  52.     if Assigned(LRSTranslator) then
  53.       LRSTranslator.Free;
  54.     LRSTranslator := LocalTranslator;
  55.     for i := 0 to Screen.CustomFormCount-1 do
  56.       LocalTranslator.UpdateTranslation(Screen.CustomForms[i]);
  57.     for i := 0 to Screen.DataModuleCount-1 do
  58.       LocalTranslator.UpdateTranslation(Screen.DataModules[i]);
  59.   end;
  60. end;
  61.  
  62. procedure TForm1.Button1Click(Sender: TObject);
  63. var
  64.   langfilename : string ='';
  65. begin
  66.   LanguageName := 'bg_BG';
  67.   langfilename := AppendPathDelim(ExtractFilePath(Application.ExeName)) + AppendPathDelim('lang') +  LanguageName + '.po';
  68.   SetAppLang(langfilename);
  69.   //SetDefaultLang('','',langfilename);
  70.   ShowMessage(TestString);
  71. end;
  72.  
  73. end.

Лазар 4,0RC2 32 bit (sometimes 64 bit); FPC3,2,2

wp

  • Hero Member
  • *****
  • Posts: 12784
Re: SetDefaultLang with a filename
« Reply #3 on: April 17, 2025, 03:38:49 pm »
I think the case of a translation filename consisting only of the language code is not consided in FindLocaleFileName of the LCLTranslator unit. When you keep the original name proposed by i18n, "project1.bg_BG.po", it will work. Or, if you insist on your naming, you should not call SetDefaultLang at all, but manage language code in your application and call Translations.TranslateResourceStrings and/or .TranslateUnitResourceStrings yourself, exactly what your SetAppLang is doing.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   langDir: String;
  4. begin
  5.   LanguageName := 'bg_BG';
  6.   langDir := AppendPathDelim(ExtractFilePath(Application.ExeName)) + AppendPathDelim('lang');
  7.   SetDefaultLang(LanguageName, langDir);
  8.   ShowMessage(TestString);
  9. end;



TranslateLCLResourceStrings is needed to translate the strings defined by the LCL in the lclstrconsts unit. Your application maybe uses one or more of them in file and other dialogs (button captions) or error messages. For this to work, you must copy the file lclstrconsts.bg_BG.po (or lclstrconsts.bg.po) from (lazarus)/lcl/languages into your lang directory. But as you'll see none of these two files exists (i.e. there seems to be no bulgarian translation of the LCL...), therefore you can skip it.

« Last Edit: April 17, 2025, 04:05:58 pm by wp »

CM630

  • Hero Member
  • *****
  • Posts: 1321
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: SetDefaultLang with a filename
« Reply #4 on: April 23, 2025, 08:53:39 am »
...
TranslateLCLResourceStrings is needed to translate the strings defined by the LCL in the lclstrconsts unit. Your application maybe uses one or more of them in file and other dialogs (button captions) or error messages. For this to work, you must copy the file lclstrconsts.bg_BG.po (or lclstrconsts.bg.po) from (lazarus)/lcl/languages into your lang directory. But as you'll see none of these two files exists (i.e. there seems to be no bulgarian translation of the LCL...), therefore you can skip it.
Thanks, I will bear that in mind. I have seen some apps using lclstrconsts.*.po. A multilingual app usually does not mean “English, Bulgarian and nothing else”.
Лазар 4,0RC2 32 bit (sometimes 64 bit); FPC3,2,2

 

TinyPortal © 2005-2018