Forum > Translations
[SOLVED] SetDefaultLang with a filename
(1/1)
CM630:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure SetAppLang(LocaleFileName: string = '');var LocalTranslator: TUpdateTranslator; i: integer;begin LocalTranslator := nil; Translations.TranslateResourceStrings(LocaleFileName); LocalTranslator := TPOTranslator.Create(LocaleFileName);// TranslateLCLResourceStrings(Lang, LocaleFileName); if (LocalTranslator<>nil) then begin if Assigned(LRSTranslator) then LRSTranslator.Free; LRSTranslator := LocalTranslator; for i := 0 to Screen.CustomFormCount-1 do LocalTranslator.UpdateTranslation(Screen.CustomForms[i]); for i := 0 to Screen.DataModuleCount-1 do LocalTranslator.UpdateTranslation(Screen.DataModules[i]); end;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.
wp:
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:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, LazFileUtils, LCLTranslator, Translations, LResources; resourcestring TestString = 'TestString'; type { TForm1 } TForm1 = class(TForm) Button1: TButton; Label1: TLabel; procedure Button1Click(Sender: TObject); private public end; var Form1: TForm1; LanguageName : string; implementation {$R *.lfm} { TForm1 } procedure SetAppLang(LocaleFileName: string = '');var LocalTranslator: TUpdateTranslator; i: integer;begin if (LocaleFileName = '') then exit; LocalTranslator := nil; Translations.TranslateResourceStrings(LocaleFileName); LocalTranslator := TPOTranslator.Create(LocaleFileName);// TranslateLCLResourceStrings(Lang, LocaleFileName); if (LocalTranslator<>nil) then begin if Assigned(LRSTranslator) then LRSTranslator.Free; LRSTranslator := LocalTranslator; for i := 0 to Screen.CustomFormCount-1 do LocalTranslator.UpdateTranslation(Screen.CustomForms[i]); for i := 0 to Screen.DataModuleCount-1 do LocalTranslator.UpdateTranslation(Screen.DataModules[i]); end;end; procedure TForm1.Button1Click(Sender: TObject);var langfilename : string ='';begin LanguageName := 'bg_BG'; langfilename := AppendPathDelim(ExtractFilePath(Application.ExeName)) + AppendPathDelim('lang') + LanguageName + '.po'; SetAppLang(langfilename); //SetDefaultLang('','',langfilename); ShowMessage(TestString);end; end.
wp:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var langDir: String;begin LanguageName := 'bg_BG'; langDir := AppendPathDelim(ExtractFilePath(Application.ExeName)) + AppendPathDelim('lang'); SetDefaultLang(LanguageName, langDir); ShowMessage(TestString);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.
CM630:
--- Quote from: wp on April 17, 2025, 03:38:49 pm ---...
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.
--- End quote ---
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”.
Navigation
[0] Message Index