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
Experienced Solver
Experienced Solver
Posts: 78
Joined: Sun Jul 19, 2009 6:12 am

How to change language at runtime

Post by Virchanza » 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.

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: 15174
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to change language at runtime

Post by doublemax » 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.
Use the source, Luke!

Virchanza
Experienced Solver
Experienced Solver
Posts: 78
Joined: Sun Jul 19, 2009 6:12 am

Re: How to change language at runtime

Post by Virchanza » Mon Feb 10, 2020 10:32 am

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: 973
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: How to change language at runtime

Post by utelle » Mon Feb 10, 2020 1:52 pm

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: 4425
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to change language at runtime

Post by ONEEYEMAN » Mon Feb 10, 2020 4:06 pm

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

Thank you.

utelle
Moderator
Moderator
Posts: 973
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: How to change language at runtime

Post by utelle » Mon Feb 10, 2020 7:16 pm

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: 4425
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to change language at runtime

Post by ONEEYEMAN » Mon Feb 10, 2020 7:55 pm

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

Thank you.

Virchanza
Experienced Solver
Experienced Solver
Posts: 78
Joined: Sun Jul 19, 2009 6:12 am

Re: How to change language at runtime

Post by Virchanza » Mon Feb 10, 2020 8:06 pm

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: 223
Joined: Fri Sep 14, 2012 8:26 am
Location: Kontich, Belgium
Contact:

Re: How to change language at runtime

Post by Nunki » Fri Feb 21, 2020 10:10 am

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