[bt-devel] Changing GUI language at runtime
Jaak Ristioja
Ristioja at gmail.com
Thu Apr 23 11:36:23 MST 2009
Hello
A day or two ago I proposed a feature over IRC about changing the
language of the BibleTime at runtime, e.g. via a menu or settings
dialog. Basically this is a quite simple task. I have already a
TranslationManager class ready for the SVN head which provides a fully
operational QMenu which lists all available languages. Upon activating
an item in that menu, all widgets should receive the
QEvent::LanguageChange event via QWidget::changeEvent. For the runtime
translation feature to work, the following changes need to be made for
most UI classes:
1) All tr() calls need to be removed from directly any
constructors/methods that initialize widgets etc and need to be put in a
separate method, e.g. retranslateInterface(). That means we need to have
pointers to all widgets in that specific class that need to be
translated at run-time. That includes widget texts, titles, tooltips,
whatsThis texts etc. Putting translated strings of message boxes in that
method could speed up some things, but probably isn't practical. Sample:
MyWidget::MyWidget(QWidget *parent = 0) {
initWidgets();
retranslateInterface();
QMessageBox::information(
this, tr("Information"),
tr("MyWidget loaded successfully!")
);
}
void MyWidget::initWidgets() {
_childWidget = new QLabel(this);
}
void MyWidget::retranslateInterface() {
// Here all widgets and menus and such will be translated
_childWidget->setText(tr("Hello %1!").arg(_worldString));
}
2) Every widget needs to implement changeEvent. Sample:
void MyWidget::changeEvent(QEvent *event) {
if (event->type() == QEvent::LanguageChange) {
retranslateInterface();
}
SuperClassWidget::changeEvent(event);
}
To bind the TranslationManager to BibleTime, it will suffice to
initialize TranslationManager in main() after everything necessary for
the CBTConfig to work is initialized:
TranslationManager tm;
and to put the language menu somewhere:
_settingsMenu->addMenu(_languageMenu = new QMenu(_settingsMenu));
TranslationManager::getInstance()->setLanguageMenu(_languageMenu);
I'd like to provide a patch, but as I currently don't have the time to
explore and patch the BibleTime source code in such great extent, I will
only attach the preliminary TranslationManager class files. Currently
they are to be placed into src/util/.
What do you think?
Stay Blessed!
Jaak Ristioja
-------------- next part --------------
A non-text attachment was scrubbed...
Name: translationutil.cpp
Type: text/x-c++src
Size: 4843 bytes
Desc: not available
URL: <http://www.crosswire.org/pipermail/bt-devel/attachments/20090423/37ebf744/attachment-0002.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: translationutil.h
Type: text/x-chdr
Size: 1980 bytes
Desc: not available
URL: <http://www.crosswire.org/pipermail/bt-devel/attachments/20090423/37ebf744/attachment-0003.bin>
More information about the bt-devel
mailing list