Forum > Translations
Translation and ShowMessage
Philippe1:
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:
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 [+][-]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";}};} ---program xxx;{$mode objfpc}{$H+} uses {$IFDEF UNIX} cthreads,clocale, {$ENDIF} Interfaces, // this includes the LCL widgetset Forms, main, tachartlazaruspkg, about, lazutf8, Translations, SysUtils; procedure TranslateLCL;var PODirectory, Lang, FallbackLang: String;begin PODirectory:= ExtractFilePath(Application.ExeName) +'languages/'; Lang:=''; FallbackLang:=''; LazGetLanguageIDs(Lang,FallbackLang); Translations.TranslateUnitResourceStrings('LCLStrConsts', PODirectory+'lclstrconsts.%s.po',Lang,FallbackLang); Translations.TranslateUnitResourceStrings('lr_const', PODirectory+'lr_const.%s.po',Lang,FallbackLang);end; {$R *.res} begin Application.Title:='xxx'; TranslateLCL; RequireDerivedFormResource := True; Application.Initialize; Application.CreateForm(TMainForm, MainForm); Application.Run;end.
To translate string in the MessageBox Add resourcestring after var with string which you want to show in the MessageBox:
--- 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";}};} ---resourcestringMyMessageBoxStr = 'My message'; ShowMessage(MyMessageBoxStr); Find it in the .po file and translate to French.
PeterHu:
--- Quote from: mig-31 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 [+][-]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";}};} ---program xxx; procedure TranslateLCL;var PODirectory, Lang, FallbackLang: String;begin Translations.TranslateUnitResourceStrings('LCLStrConsts', PODirectory+'lclstrconsts.%s.po',Lang,FallbackLang); Translations.TranslateUnitResourceStrings('lr_const', PODirectory+'lr_const.%s.po',Lang,FallbackLang);end;
To translate string in the MessageBox Add resourcestring after var with string which you want to show in the MessageBox:
--- 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";}};} ---resourcestringMyMessageBoxStr = 'My message'; ShowMessage(MyMessageBoxStr); Find it in the .po file and translate to French.
--- End quote ---
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.
wp:
--- Quote from: Philippe1 on October 14, 2024, 02:19:06 pm ---I can't figure out how to translate a string which is to be shown by ShowMessage : ShowMessage('my text to be translated').
--- End quote ---
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);
--- End quote ---
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:
--- Quote from: wp on July 25, 2025, 03:00:37 pm ---
--- Quote from: Philippe1 on October 14, 2024, 02:19:06 pm ---I can't figure out how to translate a string which is to be shown by ShowMessage : ShowMessage('my text to be translated').
--- End quote ---
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);
--- End quote ---
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
--- End quote ---
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?
Navigation
[0] Message Index
[#] Next page