Change language on the fly when using Lazarus resources lazarus / 31-LCL ---------------------------------------------------------------------- kupferstecher · 2016-06-18 15:28 It took me quite some time to find out how to change the language while running my program (not in the initialisation phase) with the constraint of having the PO-file integrated in the binary as Lazarus resource. I just want to share it here and if there is no reservation I may also add it to the Lazarus wiki. According to the wiki changing the program while running is possible with the procedure SetDefaultLang. But using Lazarus resources is not possible here. The instructions how to use PO-files embedded in the binary as Lazarus resource is described, but changing on the fly is not handled. http://wiki.freepascal.org/Translations_/_i18n_/_localizations_for_pro grams http://wiki.freepascal.org/Everything_else_about_translations And thats how it works (Lazarus 1.6): --- Convert the .po file(s) to .lrs using tools/lazres: Code: Diff  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ./lazres language.lrs testprog1.de.po testprog1.en.po Create a new unit (not a form!) and insert the below function: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  LResources, LCLTranslator, Translations; function TranslateTo(POFileName: string): boolean;var  res: TLResource;  ii: Integer;  POFile: TPOFile;  LocalTranslator: TUpdateTranslator;begin  Result:= false;  res:=LazarusResources.Find(POFileName,'PO');  if res = nil then EXIT;  POFile:=TPOFile.Create(False);  //if Full=True then you can get a crash (Issue #0026021)  try    POFile.ReadPOText(res.Value);    Result:= Translations.TranslateResourceStrings(POFile);    if not Result then EXIT;        LocalTranslator := TPOTranslator.Create(POFile);     LRSTranslator := LocalTranslator;    for ii := 0 to Screen.CustomFormCount-1      do LocalTranslator.UpdateTranslation(Screen.CustomForms[ii]);    for ii := 0 to Screen.DataModuleCount-1      do LocalTranslator.UpdateTranslation(Screen.DataModules[ii]);    LRSTranslator:= nil;    LocalTranslator.Destroy;    //POFile.Destroy; //DONT! Already done in LocalTranslator.Destroy  finally end;end;Also insert the include statement (here the above mentioned lrs-file is in subfolder "locale"): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i initialization  {$I locale/language.lrs} Then the language can be changed (examples German and English) with Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TranslateTo('testprog1.de');and Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TranslateTo('testprog1.en'); --- Hop it helps~ Regards ---------------------------------------------------------------------- wp · 2016-06-18 16:41 You probably don't know http://wiki.freepascal.org/Step-by- step_instructions_for_creating_multi-language_applications which contains a section on changing language while running. And there is also a demo program in the examples/translation folder of Lazarus. ---------------------------------------------------------------------- BeniBela · 2016-06-18 16:54 By the way,  is there any reasonable explanation why Alt+R automatically creates resourcestrings  from  string literals and puts those resourcestrings in fucking strutils? ---------------------------------------------------------------------- kupferstecher · 2016-06-19 19:11 Quote from: wp on June 18, 2016, 06:41:22 pmYou probably don't know http://wiki.freepascal.org/Step-by- step_instructions_for_creating_multi-language_applications which contains a section on changing language while running. And there is also a demo program in the examples/translation folder of Lazarus. I know that wiki page and also checked the example page now. For both the method of changing the language while running is calling the SetDefaultLang function. This works fine, but requires (if I didn't miss something) the po-file in the filesystem, means a standalone executable is not possible. Calling them from within a Lazarus resource is not possible. So I still guess my solution is interesting for some users. ---------------------------------------------------------------------- wp · 2016-06-19 19:19 Sorry, as usual, I did not read carefully enough ---------------------------------------------------------------------- kupferstecher · 2016-07-05 16:48 I added the topic here in the wiki: http://wiki.freepascal.org/Everything_else_about_translations#Compilin g_po_files_into_the_executable_and_change_language_while_running