Recent

Author Topic: Translation and ShowMessage  (Read 4124 times)

Philippe1

  • Newbie
  • Posts: 6
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: 307
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

PeterHu

  • Jr. Member
  • **
  • Posts: 60
Re: Translation and ShowMessage
« Reply #2 on: July 25, 2025, 02:16:14 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.  
  3.  
  4. procedure TranslateLCL;
  5. var
  6.   PODirectory, Lang, FallbackLang: String;
  7. begin
  8.  
  9.   Translations.TranslateUnitResourceStrings('LCLStrConsts',
  10.                       PODirectory+'lclstrconsts.%s.po',Lang,FallbackLang);
  11.   Translations.TranslateUnitResourceStrings('lr_const',
  12.                       PODirectory+'lr_const.%s.po',Lang,FallbackLang);
  13. end;
  14.  
  15.  

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.

May I ask where the lr_const.%s.po (e.g. lr_const_zh_CN.po) file is ? I did a search but failed to get one from my computer.

And ,where is the language sub folder exactly located for this project? just under the project root folder?

I have a try following the guideline but failed to implement the translation on showMessage text.Please see the attached screenshort for your reference.

Thanks for anybody who might help.
« Last Edit: July 25, 2025, 02:48:29 pm by PeterHu »

wp

  • Hero Member
  • *****
  • Posts: 13264
Re: Translation and ShowMessage
« Reply #3 on: July 25, 2025, 03:00:37 pm »
I can't figure out how to translate a string which is to be shown by ShowMessage : ShowMessage('my text to be translated').
Create the string as a resource string so that it is included in the pot file of your application:
Quote
resourcestring
  SMyTextToBeTranslated = 'My text to be translated.';

//in your code
  ShowMessage(SMyTextToBeTranslated);
It is only required to have the i18n option active in the project configuration.

Did you study the translation sample project in folder examples/translation of your Lazarus installation? There is also a step-by-step tutorial in the wiki: https://wiki.lazarus.freepascal.org/Step-by-step_instructions_for_creating_multi-language_applications

PeterHu

  • Jr. Member
  • **
  • Posts: 60
Re: Translation and ShowMessage
« Reply #4 on: July 26, 2025, 02:46:05 am »
I can't figure out how to translate a string which is to be shown by ShowMessage : ShowMessage('my text to be translated').
Create the string as a resource string so that it is included in the pot file of your application:
Quote
resourcestring
  SMyTextToBeTranslated = 'My text to be translated.';

//in your code
  ShowMessage(SMyTextToBeTranslated);
It is only required to have the i18n option active in the project configuration.

Did you study the translation sample project in folder examples/translation of your Lazarus installation? There is also a step-by-step tutorial in the wiki: https://wiki.lazarus.freepascal.org/Step-by-step_instructions_for_creating_multi-language_applications

I tried to compile and run the examples/translation project ,but when I select language 'zh_cn' ,an exception was thrown saying that language 'zh' not supported.Please see the attached screenshot.
But I also noticed that there is such settings for zh_cn in function GetLCIDFromLangCode(ALang: String) in unit locallizedForms.
So was I missing something?

wp

  • Hero Member
  • *****
  • Posts: 13264
Re: Translation and ShowMessage
« Reply #5 on: July 26, 2025, 11:30:29 am »
In unit Main of the translation_demo make the following changes:
Code: Pascal  [Select][+][-]
  1. procedure TMainForm.SelectLanguage(ALang: String);
  2. var
  3.   i, p: Integer;
  4.   lang: String;
  5. begin
  6.   // Switch language - this is in LCLTranslator
  7.   lang := SetDefaultLang(ALang);    // <--- Replace ALang by lang
  8.  
  9.   if lang <> '' then                // <--- Replace ALang by lang
  10.   begin
  11.     // Switch default settings by calling the procedure provided in BasicLocalizedForm.pas.
  12.     UpdateFormatSettings(ALang);
  13.     [...]

PeterHu

  • Jr. Member
  • **
  • Posts: 60
Re: Translation and ShowMessage
« Reply #6 on: July 26, 2025, 01:30:29 pm »
In unit Main of the translation_demo make the following changes:
Code: Pascal  [Select][+][-]
  1. procedure TMainForm.SelectLanguage(ALang: String);
  2. var
  3.   i, p: Integer;
  4.   lang: String;
  5. begin
  6.   // Switch language - this is in LCLTranslator
  7.   lang := SetDefaultLang(ALang);    // <--- Replace ALang by lang
  8.  
  9.   if lang <> '' then                // <--- Replace ALang by lang
  10.   begin
  11.     // Switch default settings by calling the procedure provided in BasicLocalizedForm.pas.
  12.     UpdateFormatSettings(ALang);
  13.     [...]

Fixed! Thank you!

 

TinyPortal © 2005-2018