PDA

View Full Version : QCalendarWidget and dynamic language switching



tora
21st September 2009, 15:04
I'm using in my application a QCalendarWidget but I'm not able to find where the translations related to this widget are. Looking in qt_*.ts there is no hint to this widget.
What do I have to do to dynamically change the language of this widget?

Thanks in advance

Matriarch
22nd September 2009, 11:54
use QLocale

QCalendarWidget *w = new QCalendarWidget;

w->setLocale( QLocale::English );

tora
22nd September 2009, 13:19
The problem is that I'm doing dynamic language switching like explained in "C++ GUI Programming with Qt4", i.e. implementing in every form/window a method retranslateUi() where all the GUI strings are set and overriding the changeEvent(QEvent*) method so that every time the event is a LanguageChange event, retranslateUi() is called. in the event handler changeEvent I've no information about which language was set (which qm file was loaded) and then I cannot map it to the correct locale.
I do not understand why QCalender widget behaves so differently, the reason why its translateble strings are not in qt_*.ts.

Matriarch
23rd September 2009, 09:38
I never used Qt internalization, so I don't know how it exactly works. But If your customer needs behaviour like you described, just try some "quick and dirty" trick.

1) create ISO language name holding label, which is translatable and non visible for user.

QLabel *locale = new QLabel(this);
locale->setText( tr("en") );
locale->hide();

2) Put other ISO language names to your .ts file and use QLocale::QLocale ( const QString & name ) constructor. Look for inspiration in: http://www.w3.org/WAI/ER/IG/ert/iso639.htm

switch (e->type()) {
case QEvent::LanguageChange:
retranslateUi();
myCalendarWidget->setLocale(QLocale(locale->text));
break;
default:
break;
}

There will be surely much clearer way to achieve this, but as i said i never used internalization...

tora
23rd September 2009, 12:06
Thank you Matriarch for your help.
Yes the only solution seems to be to have a "global" string that contains the locale corresponding to the last loaded .qm.