Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2012-07

Changing the translation of forms while the program is running

21 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Joost van der Sluis

Hi all,

I'm using the DefaultTranslator unit to translate my application, but I would also like to switch to another language on-the-fly, thus without restarting the application.

Are there any tricks to do that?

Met vriendelijke groeten, Joost van der Sluis CNOC Informatiesystemen en Netwerken http://www.cnoc.nl

Reply by email

zeljko
to Joost van der Sluis
Joost van der Sluis wrote 7 lines
Hi all, I'm using the DefaultTranslator unit to translate my application, but I would also like to switch to another language on-the-fly, thus without restarting the application. Are there any tricks to do that?

I need that too atm :)

zeljko
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120713/463766cd/attachment.html>

Reply by email

Corpsman
to zeljko

Try this one :

http://corpsman.de/index.php?doc=komponenten/multilanguage

zeljko wrote 31 lines
On Friday 13 of July 2012 15:49:37 Joost van der Sluis wrote: > Hi all, > > I'm using the DefaultTranslator unit to translate my application, but I > would also like to switch to another language on-the-fly, thus without > restarting the application. > > Are there any tricks to do that? I need that too atm :) zeljko -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Auf meiner Homepage www.Corpsman.de ist immer was los, st?ndig wird sie aktualisiert und erweitert. Da ist f?r jeden was dabei. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120713/538f75da/attachment.html>

Reply by email

Corpsman
to Joost van der Sluis

in short, no.

in a bit longer :

if you set within the application :  Language.CurrentLanguage := 'what
ever language you want';

The component tries to load that language file, if it exits your application is switched to the language, if not all translatable strings (captions, hints, .. and Ressourcestrings) will be extracted and stored to 'what ever language you want' file. With the Demo program you can edit this files ( or with any other text editor ) and translate them. On the next language changing the values will be loaded. In the demo this will be shown ( it uses nativ Lazarus methods, so it should be portable, it is successfully testet on Win32 and Linux32 ).

corpsman

Joost van der Sluis wrote 15 lines
On Fri, 2012-07-13 at 16:00 +0200, Corpsman wrote: > Try this one : > > http://corpsman.de/index.php?doc=komponenten/multilanguage Does it uses the .po and .mo files generated by Lazarus? Joost. -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Auf meiner Homepage www.Corpsman.de ist immer was los, st?ndig wird sie aktualisiert und erweitert. Da ist f?r jeden was dabei.

Reply by email

Maxim Ganetsky
to Joost van der Sluis: I'm using the DefaultTranslator unit to translate my application, but I would also like…

13.07.2012 17:49, Joost van der Sluis ?????:

Show quoted text 7 lines
Hi all, I'm using the DefaultTranslator unit to translate my application, but I would also like to switch to another language on-the-fly, thus without restarting the application. Are there any tricks to do that?

DefaultTranslator unit is quite simplistic and more meant to be an example.

In order to translate your application dynamically you should manually load .po files at runtime using functions (e. g.
TranslateUnitResourceStrings) from Translations unit. See its header for examples. Also there is a number of program sources to look at bundled with Lazarus, e. g. lazarus\tools\lazdatadesktop (see TranslateStrs procedure at the end of frmmain.pp).

Best regards, Maxim Ganetsky mailto:

Reply by email

Bart
to Maxim Ganetsky
Maxim Ganetsky wrote
In order to translate your application dynamically you should manually load .po files at runtime using functions (e. g. TranslateUnitResourceStrings) from Translations unit. See its header for examples.

And you'll have to update all captions of all controls on the current form (at least) in order to see the effect.

The IDE seems to do this, but I was unabble to track how. Manually updating all captions seems a horrible task to maintain.

Bart

Reply by email

Mattias Gaertner
to Bart
Show quoted text 12 lines
On 7/13/12, Maxim Ganetsky wrote: > In order to translate your application dynamically you should manually > load .po files at runtime using functions (e. g. > TranslateUnitResourceStrings) from Translations unit. See its header for > examples. And you'll have to update all captions of all controls on the current form (at least) in order to see the effect. The IDE seems to do this, but I was unabble to track how. Manually updating all captions seems a horrible task to maintain.

The IDE forms set all captions when creating. See the FormCreate events. The maintenance work is near zero.
Theoretically the code could be put into methods and triggered when the language changes.

Mattias

Reply by email

Maxim Ganetsky
to Bart: And you'll have to update all captions of all controls on the current form (at least) in…

15.07.2012 13:53, Bart ?????:

Show quoted text 12 lines
On 7/13/12, Maxim Ganetsky wrote: > In order to translate your application dynamically you should manually > load .po files at runtime using functions (e. g. > TranslateUnitResourceStrings) from Translations unit. See its header for > examples. And you'll have to update all captions of all controls on the current form (at least) in order to see the effect. The IDE seems to do this, but I was unabble to track how. Manually updating all captions seems a horrible task to maintain.

?

IDE can update .po files automagically. See Project Options -> i18n.

Best regards, Maxim Ganetsky mailto:

Reply by email

Sven Barth
to Maxim Ganetsky
Maxim Ganetsky wrote 18 lines
15.07.2012 13:53, Bart ?????: > On 7/13/12, Maxim Ganetsky wrote: > >> In order to translate your application dynamically you should manually >> load .po files at runtime using functions (e. g. >> TranslateUnitResourceStrings) from Translations unit. See its header for >> examples. > > And you'll have to update all captions of all controls on the current > form (at least) in order to see the effect. > > The IDE seems to do this, but I was unabble to track how. > Manually updating all captions seems a horrible task to maintain. ? IDE can update .po files automagically. See Project Options -> i18n.

Bart's remark was about the IDE updating it's own forms when the user changes the language of the IDE.

Regards,
Sven

Reply by email

Maxim Ganetsky
to Sven Barth

15.07.2012 15:35, Sven Barth ?????:

Show quoted text 23 lines
On 15.07.2012 12:24, Maxim Ganetsky wrote: > 15.07.2012 13:53, Bart ?????: >> On 7/13/12, Maxim Ganetsky wrote: >> >>> In order to translate your application dynamically you should manually >>> load .po files at runtime using functions (e. g. >>> TranslateUnitResourceStrings) from Translations unit. See its header >>> for >>> examples. >> >> And you'll have to update all captions of all controls on the current >> form (at least) in order to see the effect. >> >> The IDE seems to do this, but I was unabble to track how. >> Manually updating all captions seems a horrible task to maintain. > > ? > > IDE can update .po files automagically. See Project Options -> i18n. > Bart's remark was about the IDE updating it's own forms when the user changes the language of the IDE.

Then see Mattias' remark.

Best regards, Maxim Ganetsky mailto:

Reply by email

Bart
to Mattias Gaertner: On Sun, 15 Jul 2012 11:53:24 +0200 The IDE forms set all captions when creating. See the…
Mattias Gaertner wrote
The IDE forms set all captions when creating. See the FormCreate events. The maintenance work is near zero. Theoretically the code could be put into methods and triggered when the language changes.

This works well if you create all forms on the fly I guess. However the current active form (and all other forms still showing) will have to be updated I guess?

Say I have a mainform with a label with caption := SomeResourceString. Now i translate the resourcestring using Translations unit. This still leaves my label with the untranslated caption. So I have to do MyLabel.Caption := SomeResourceString again after the translation.

Bart

Reply by email

Mattias Gaertner
to Bart
Show quoted text 16 lines
On 7/15/12, Mattias Gaertner wrote: > The IDE forms set all captions when creating. See the FormCreate events. > The maintenance work is near zero. > Theoretically the code could be put into methods and triggered when the > language changes. This works well if you create all forms on the fly I guess. However the current active form (and all other forms still showing) will have to be updated I guess? Say I have a mainform with a label with caption := SomeResourceString. Now i translate the resourcestring using Translations unit. This still leaves my label with the untranslated caption. So I have to do MyLabel.Caption := SomeResourceString again after the translation.

Yes.

Mattias

Reply by email

Bart
to Mattias Gaertner
Mattias Gaertner wrote 5 lines
> So I have to do MyLabel.Caption := SomeResourceString again after the > translation. Yes.

In the ideal world I would set in OI that Caption uses a resourcestring, and the component has some method to re-read it's content, so one could do stuff like:

for i := 0 to Form1.ComponentCount - 1 do
begin
  C := Form1.Compnents[i];
  if C.UsesResourceStringForCaption then C.UpdateResourceString;
end;

This would make maintanance (large menu's, active devolopment of forms in question) lots easier.

Bart

Reply by email

Graeme Geldenhuys
to Bart

Hi,

Bart wrote
This would make maintanance (large menu's, active devolopment of forms in question) lots easier.

What I did is to encapsulate setting up captions to a single method, SetupCaptions, on each form. I use form inheritance, so each form simply needs to override that method. That method is called in FormCreate to setup all components. If the application language changes at runtime, I ask fpgApplication so broadcast a message to all loaded forms of the application. Those forms receive the message, and execute the SetupCaptions method again. Works like a charm.

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net

Reply by email

Joost van der Sluis
to zeljko: I need that too atm :) zeljko -------------- next part -------------- An HTML attachment…
zeljko wrote 8 lines
On Friday 13 of July 2012 15:49:37 Joost van der Sluis wrote: > I'm using the DefaultTranslator unit to translate my application, but I > would also like to switch to another language on-the-fly, thus without > restarting the application. I need that too atm :)

Try the attached patch for DefaultTranslator. With SetDefaultLang('nl') it will switch the language on-the-fly (to Dutch in this case).

It will loop through all forms in screens and will change all properties that are present in the .po-file.

Maybe it is an idea to apply this patch so that others can also use this? Or should users write their own 'DefaultTranslator' unit? (Then this one could be added to the Wiki as an example)

Met vriendelijke groeten, Joost van der Sluis CNOC Informatiesystemen en Netwerken http://www.cnoc.nl -------------- next part -------------- A non-text attachment was scrubbed... Name: SwitchTranslationsPatch.diff Type: text/x-patch Size: 7899 bytes Desc: not available URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20120716/c56173d4/attachment.bin>

Reply by email

Mattias Gaertner
to Joost van der Sluis
Show quoted text 19 lines
On Fri, 2012-07-13 at 15:51 +0200, zeljko wrote: > On Friday 13 of July 2012 15:49:37 Joost van der Sluis wrote: > > I'm using the DefaultTranslator unit to translate my application, > but I > > would also like to switch to another language on-the-fly, thus > without > > restarting the application. > I need that too atm :) Try the attached patch for DefaultTranslator. With SetDefaultLang('nl') it will switch the language on-the-fly (to Dutch in this case). It will loop through all forms in screens and will change all properties that are present in the .po-file. Maybe it is an idea to apply this patch so that others can also use this? Or should users write their own 'DefaultTranslator' unit? (Then this one could be added to the Wiki as an example)

Yes.
Why is FStackPath a private variable instead of a parameter?

Mattias

Reply by email

Mattias Gaertner
to Graeme Geldenhuys: What I did is to encapsulate setting up captions to a single method, SetupCaptions, on…
Show quoted text 15 lines
Hi, On 16 July 2012 11:27, Bart wrote: > > This would make maintanance (large menu's, active devolopment of forms > in question) lots easier. What I did is to encapsulate setting up captions to a single method, SetupCaptions, on each form. I use form inheritance, so each form simply needs to override that method. That method is called in FormCreate to setup all components. If the application language changes at runtime, I ask fpgApplication so broadcast a message to all loaded forms of the application. Those forms receive the message, and execute the SetupCaptions method again. Works like a charm.

Yes. This catches all special cases like memos, tree nodes, list views and private variables as well.

Mattias

Reply by email

Graeme Geldenhuys
to Mattias Gaertner
Mattias Gaertner wrote
Yes. This catches all special cases like memos, tree nodes, list views and private variables as well.

Correct, and I also couldn't rely on something like RTTI to iterate or query widgets on a form, because my widgets are declared private only (not published).

And yet another alternative, one could use Interfaces too. The Form implements a "translation interface", and the broadcast mechanism could simply query each loaded form for the specific interface, and if found, call the UpdateCaptions method of that interface. This way you don't need form inheritance, but the end result is similar in each case.

As always in programming, there are a 101 ways to accomplish the same task. :)

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net

Reply by email

Joost van der Sluis
to Mattias Gaertner: On Mon, 16 Jul 2012 13:58:57 +0200 Yes. Why is FStackPath a private variable instead of a…
Mattias Gaertner wrote 25 lines
On Mon, 16 Jul 2012 13:58:57 +0200 Joost van der Sluis wrote: > On Fri, 2012-07-13 at 15:51 +0200, zeljko wrote: > > On Friday 13 of July 2012 15:49:37 Joost van der Sluis wrote: > > > I'm using the DefaultTranslator unit to translate my application, > > but I > > > would also like to switch to another language on-the-fly, thus > > without > > > restarting the application. > > > I need that too atm :) > > Try the attached patch for DefaultTranslator. With SetDefaultLang('nl') > it will switch the language on-the-fly (to Dutch in this case). > > It will loop through all forms in screens and will change all properties > that are present in the .po-file. > > Maybe it is an idea to apply this patch so that others can also use > this? Or should users write their own 'DefaultTranslator' unit? (Then > this one could be added to the Wiki as an example) Yes. Why is FStackPath a private variable instead of a parameter?

Because GetIdentifierPath (which needs FStackPath) is called from within TranslateStringProperty, from which the parameters are defined in the classes-unit?

Met vriendelijke groeten, Joost van der Sluis CNOC Informatiesystemen en Netwerken http://www.cnoc.nl

Reply by email

Maxim Ganetsky
to Joost van der Sluis: Try the attached patch for DefaultTranslator. With SetDefaultLang('nl') it will switch…

16.07.2012 15:58, Joost van der Sluis ?????:

Show quoted text 19 lines
On Fri, 2012-07-13 at 15:51 +0200, zeljko wrote: > On Friday 13 of July 2012 15:49:37 Joost van der Sluis wrote: >> I'm using the DefaultTranslator unit to translate my application, > but I >> would also like to switch to another language on-the-fly, thus > without >> restarting the application. > I need that too atm :) Try the attached patch for DefaultTranslator. With SetDefaultLang('nl') it will switch the language on-the-fly (to Dutch in this case). It will loop through all forms in screens and will change all properties that are present in the .po-file. Maybe it is an idea to apply this patch so that others can also use this? Or should users write their own 'DefaultTranslator' unit? (Then this one could be added to the Wiki as an example)

Applied, thanks.

The only requirement for DefaultTranslator unit IMO is that it should just work when included to program without explicit calling any procedures.

Best regards, Maxim Ganetsky mailto:

Reply by email