How to change language at runtime

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Virchanza
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Jul 19, 2009 6:12 am

How to change language at runtime

Post by Virchanza »

My application can change language at runtime. You can use the interface in English, then you can click the English flag and select German, and continue to use the interface in German. You can switch back and forth between languages at runtime like that.

The way my application is currently coded, the main dialog is destroyed when you change language, and then it is recreated from scratch in the new language.

Would it work out OK, instead of destroying the main dialog, if I were to call all the "SetText" methods on all the widgets and then try to resize them?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to change language at runtime

Post by doublemax »

Would it work out OK, instead of destroying the main dialog, if I were to call all the "SetText" methods on all the widgets and then try to resize them?
That would be possible, but is it worth the effort? How often do users actually change the GUI language? Usually only once and then it shouldn't matter if they have to restart the application.
Use the source, Luke!
Virchanza
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Jul 19, 2009 6:12 am

Re: How to change language at runtime

Post by Virchanza »

doublemax wrote: Sun Feb 09, 2020 9:59 pm
Would it work out OK, instead of destroying the main dialog, if I were to call all the "SetText" methods on all the widgets and then try to resize them?
That would be possible, but is it worth the effort? How often do users actually change the GUI language? Usually only once and then it shouldn't matter if they have to restart the application.
Two years ago I worked in a warehouse with guys from Lithuanian and Hungary. Sometimes I would have to walk into the office, look something up on the computer (which was in English) and then go back out into the warehouse. I want my application to useful in such a situation where people will be walking back and forth to a PC and can change language quickly by clicking the flag in the corner.
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: How to change language at runtime

Post by utelle »

Virchanza wrote: Sun Feb 09, 2020 8:09 pm My application can change language at runtime. You can use the interface in English, then you can click the English flag and select German, and continue to use the interface in German. You can switch back and forth between languages at runtime like that.

The way my application is currently coded, the main dialog is destroyed when you change language, and then it is recreated from scratch in the new language.
If this approach is feasible for your application, this is the easiest way to accomplish a change of the user interface language, because applying the translations and laying out the dialogs will be done automatically.
Virchanza wrote: Sun Feb 09, 2020 8:09 pm Would it work out OK, instead of destroying the main dialog, if I were to call all the "SetText" methods on all the widgets and then try to resize them?
In principle, yes. However, for most non-trivial dialogs there is usually more to do than just changing static text. For example, for combo boxes you have to reload the list of choices, and frequently you will have to change the order of items to sort them alphabetically in the applied language, and this requires your application to be aware of the item order. Regarding the layout the best approach is to apply all language related changes first, and then call the dialog layout function for the top level widget(s) of the dialog - nested widgets will be laid out anew automatically.

So, being able to change the user interface language on the fly is a really nice feature, but it requires some extra programming effort.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to change language at runtime

Post by ONEEYEMAN »

Hi,
Out of curiosity - how much time it takes to re-create the dialog with the different language?

Thank you.
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: How to change language at runtime

Post by utelle »

ONEEYEMAN wrote: Mon Feb 10, 2020 4:06 pm Out of curiosity - how much time it takes to re-create the dialog with the different language?
This really depends on the complexity of the dialogs. It can range from a few milliseconds (simple dialog with few static text elements) to a few seconds (for example about 2 seconds for a complex dialog with a notebook control with 3 tabs, of which each contains a treeview and associated lists with several dozens of translatable entries).
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to change language at runtime

Post by ONEEYEMAN »

Ulrich,
I was referring to this particular application. ;-)

Thank you.
Virchanza
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Jul 19, 2009 6:12 am

Re: How to change language at runtime

Post by Virchanza »

I've written a separate function that calls "SetLabel" on all the widgets after you change language, and this works fine. It even looks okay without me resizing anything, but I might take a look at calling the proper resize functions tomorrow.

At one point my application was able to switch language in a split second (i.e. faster than a human being could care about), but now it takes 2 or 3 seconds because I load a PNG of a flag (e.g. the Union Jack for English), resize it, draw text over it and set it as the picture on a button (I have a flag in the corner for changing language). If I prepare the PNG files already-resized and already-text-overlayed then it should take a split second. But anyway I'm not destroying the whole window anymore.
Nunki
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Fri Sep 14, 2012 8:26 am
Location: Kontich, Belgium
Contact:

Re: How to change language at runtime

Post by Nunki »

Hi,

You could, instead of creating the whole dialog with several wx statements, create XRC files and simply load them and display them. Then you can layout them while designing them with your wysiwig tool. Saves you a lot of coding.

with regards,
Nunki
Post Reply