Recent

Author Topic: Translation and ShowMessage  (Read 620 times)

Philippe1

  • Newbie
  • Posts: 4
Translation and ShowMessage
« on: October 14, 2024, 02:19:06 pm »
Hello,
I have an application which is very well localized for each caption / text of labels, TEdit, TMaskEdit, Buttons etc. The .po files are correct (path = /languages) eg for French :
 
msgid ""
msgstr "Content-Type: text/plain; charset=UTF-8"

#: tform1.btcalculate.caption
msgid "Calculate"
msgstr "Calculer"

.../...

I Have :
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Windows, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, IniFiles, LCLTranslator,
  Graph, RegExpr, StdCtrls, ExtCtrls, MaskEdit, LCLType, LCLIntf, Variants, Math;


I can't figure out how to translate a string which is to be shown by ShowMessage : ShowMessage('my text to be translated').
Is there someone to help me ?
Thanks
Philippe

mig-31

  • Sr. Member
  • ****
  • Posts: 306
Re: Translation and ShowMessage
« Reply #1 on: October 14, 2024, 02:35:52 pm »
Hi,

to translate button caption you should copy lclstrconsts.fr.po from <lazarus installation dir>/lcl/languages to /languages and load it during app start up <your project>.lpr file
Code: Pascal  [Select][+][-]
  1. program xxx;
  2. {$mode objfpc}{$H+}
  3.  
  4. uses
  5.   {$IFDEF UNIX}
  6.   cthreads,clocale,
  7.   {$ENDIF}
  8.   Interfaces, // this includes the LCL widgetset
  9.   Forms, main, tachartlazaruspkg, about, lazutf8, Translations, SysUtils;
  10.  
  11. procedure TranslateLCL;
  12. var
  13.   PODirectory, Lang, FallbackLang: String;
  14. begin
  15.   PODirectory:=  ExtractFilePath(Application.ExeName) +'languages/';
  16.   Lang:='';
  17.   FallbackLang:='';
  18.   LazGetLanguageIDs(Lang,FallbackLang);
  19.   Translations.TranslateUnitResourceStrings('LCLStrConsts',
  20.                       PODirectory+'lclstrconsts.%s.po',Lang,FallbackLang);
  21.   Translations.TranslateUnitResourceStrings('lr_const',
  22.                       PODirectory+'lr_const.%s.po',Lang,FallbackLang);
  23. end;
  24.  
  25. {$R *.res}
  26.  
  27. begin
  28.   Application.Title:='xxx';
  29.   TranslateLCL;
  30.   RequireDerivedFormResource := True;
  31.   Application.Initialize;
  32.   Application.CreateForm(TMainForm, MainForm);
  33.   Application.Run;
  34. end.      
  35.  

To translate string in the MessageBox Add resourcestring after var with string which you want to show in the MessageBox:

Code: Pascal  [Select][+][-]
  1. resourcestring
  2. MyMessageBoxStr = 'My message';
  3.  
  4. ShowMessage(MyMessageBoxStr);
  5.  
Find it in the .po file and translate to French.
« Last Edit: October 14, 2024, 02:43:46 pm by mig-31 »
Lazarus 2.2.6 - OpenSuse Leap 15.4, Mageia 8, CentOS 7

 

TinyPortal © 2005-2018