Recent

Author Topic: [SOLVED] LCLTranslator: update of current form possible?  (Read 3705 times)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
[SOLVED] LCLTranslator: update of current form possible?
« on: April 09, 2022, 03:59:03 pm »
I'm developing an app where the opening dialog has a combo with a drop-down offering various language translations.
Picking one of the drop-down offerings calls
Code: Pascal  [Select][+][-]
  1. LCLTranslator.SetDefaultLang(lang);
where "lang" is picked up from the selected language,
and the subsequent forms and dialogs are all duly translated correctly.

Is it possible for the current dialog - the one with the language selection combo - also to be updated and its texts rewritten in the just-selected language?
« Last Edit: April 09, 2022, 10:54:27 pm by howardpc »

Sieben

  • Sr. Member
  • ****
  • Posts: 310
Re: LCLTranslator: update of current form possible?
« Reply #1 on: April 09, 2022, 04:06:17 pm »
Assuming that the dialog inherites from TCustomForm you can try:

Code: Pascal  [Select][+][-]
  1.  LCLTranslator.UpdateTranslation(MyDialog);
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: LCLTranslator: update of current form possible?
« Reply #2 on: April 09, 2022, 06:34:41 pm »
I presume you mean
Code: Pascal  [Select][+][-]
  1. with TDefaultTranslator.Create(moFile) do
  2.  
  3.    UpdateTranslation(MyDialog);
however, I had already tried that, and it has no apparent effect, though it compiles and runs without error.

Sieben

  • Sr. Member
  • ****
  • Posts: 310
Re: LCLTranslator: update of current form possible?
« Reply #3 on: April 09, 2022, 06:47:55 pm »
Sorry, yes, should have been LRSTranslator instead of LCLTranslator. Had another look at SetDefaultlang() and it should call UpdateTranslation() anyway for all Screen.CustomForms as ForceUpdate defaults to True. Doesn't work? I've never really tried so far, I have to admit.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: LCLTranslator: update of current form possible?
« Reply #4 on: April 09, 2022, 07:01:21 pm »
Well it works fine for all the forms not yet showing, but does not affect the current one for some reason, even if I call Invalidate immediately afterwards.

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: LCLTranslator: update of current form possible?
« Reply #5 on: April 09, 2022, 07:15:02 pm »
I don't understand what you are doing exactly. Doesn't the project in examples/translations (or examples/techniques/translations in Laz-main) do what you want to? Could you set up a small demo project?
« Last Edit: April 09, 2022, 07:17:59 pm by wp »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
[SOLVED] Re: LCLTranslator: update of current form possible?
« Reply #6 on: April 09, 2022, 10:53:03 pm »
Ah, thank you Werner and Sieben. I was unaware of the Techniques example.

All I needed to do was steal TLocalizedForm, and make my initial dialog a descendent form of that class (rather than of the TForm class), and override the protected UpdateTranslations method.

Perhaps a further entry could be added to the Module node of the New... dialog in the IDE offering TLocalizedForm as the basis for multi-language windows?
It is too useful to be hidden away in an example project.

AL

  • Sr. Member
  • ****
  • Posts: 261
Re: [SOLVED] LCLTranslator: update of current form possible?
« Reply #7 on: April 10, 2022, 05:00:28 am »
 form.repaint or form.update  after SetDefaultLang(lang)  has done the trick for me
Laz 3.1, fpc 3.2.2, Win10
Laz 3.1  fpc 3.2.2, MacOS Monterey running on VMWare/Win 10
Laz 3.1  fpc 3.2.2 Ubuntu 20.04

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: [SOLVED] Re: LCLTranslator: update of current form possible?
« Reply #8 on: April 10, 2022, 05:59:49 am »
All I needed to do was steal TLocalizedForm, and make my initial dialog a descendent form of that class (rather than of the TForm class), and override the protected UpdateTranslations method.

Perhaps a further entry could be added to the Module node of the New... dialog in the IDE offering TLocalizedForm as the basis for multi-language windows?
It is too useful to be hidden away in an example project.

Sounds reasonable. But looking at the implementation, it has a lot of Windows-specific code. That might be the reason its not already in the New > Module > Form options. Does it work for other platforms?

@AL?

Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [SOLVED] Re: LCLTranslator: update of current form possible?
« Reply #9 on: April 10, 2022, 10:06:38 am »
Does it work for other platforms?
The utilities to do with LCID and FormatSettings are Windows-only. And the LCID functions only deal with a couple of dozen of the many language ids. But I suspect this unit is a work in progress.
The UpdateTranslation functionality is cross-platform.

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: [SOLVED] LCLTranslator: update of current form possible?
« Reply #10 on: April 10, 2022, 11:29:22 am »
I wrote the translation example in response to some forum discussions some years ago (cannot find them ATM) in order to show to basic steps required for translating an application. The code, in no way, was meant to be general enough to be included in the IDE's template list.

The problem is that LCLTranslator itself, written by Maxim Ganetsky, is only a simplification. Why does it translate only Captions, not items in TStrings descendants such as TCombobox.Items, TRadiogroup.Item? Because, quite often, there may be cases when the items should not be translated; suppose a list of file names or printer names in the combobox where translation certainly will not be appropriate. 

The essential idea behind TLocalizedForms is to equip translateable forms with a way to translate all the controls not covered by LCLTranslator.

And a good translation should also cover the issue with the format settings (month and day names, currency symbols, separators etc). This is not even handled by FPC in a cross-platform way. Localization of format settings is handled in the example code by an ugly windows-only IFDEF which, in my opinion, is not acceptable as a feature tied closely to the IDE.

So, after all, my hope was, that a user who wants to translate an application studies the code (along with the tutorial, https://wiki.lazarus.freepascal.org/Step-by-step_instructions_for_creating_multi-language_applications) and understands the concept of the po files. And when he does understand it he will be able to adapt it to his own requirements by a few lines of code, or by copy&pasting from the examples project.
« Last Edit: April 10, 2022, 11:34:42 am by wp »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [SOLVED] LCLTranslator: update of current form possible?
« Reply #11 on: April 10, 2022, 12:32:39 pm »
So, after all, my hope was, that a user who wants to translate an application studies the code (along with the tutorial, https://wiki.lazarus.freepascal.org/Step-by-step_instructions_for_creating_multi-language_applications) and understands the concept of the po files. And when he does understand it he will be able to adapt it to his own requirements by a few lines of code, or by copy&pasting from the examples project.
Well, at least in my case your hope has been fulfilled.
Thanks for the explanation.

Sieben

  • Sr. Member
  • ****
  • Posts: 310
Re: [SOLVED] LCLTranslator: update of current form possible?
« Reply #12 on: April 10, 2022, 01:55:36 pm »
Quote from: AL
form.repaint or form.update  after SetDefaultLang(lang)  has done the trick for me

I just set up a small sample project here, and yes, SetDefaultLang() (or TranslateFromResources() resp.) followed by Invalidate works for me as well - with the limitations wp mentioned, of course. However, the currently active form is translated in just the same way as any form not yet created.

Main difference with OP's setup I guess is that I was using *.po-files instead of *.mo, which reminds me of this here thread. There seem to be some weird limitations when using *.mo-files...
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

 

TinyPortal © 2005-2018