add auto-detect system language in i18n example
It makes this example more complete since we move linguist related examples into qttools Task-number: QTBUG-28434 Change-Id: Ic6bbbd2702b5a0a304b5e8ce59da37ef95e4b42e Reviewed-by: Marc Mutz <marc.mutz@kdab.com>bb10
parent
6797413db2
commit
e1cbc8c68f
|
|
@ -32,7 +32,9 @@
|
|||
|
||||
\brief The Internationalization (I18N) example demonstrates Qt's support for translated
|
||||
text. Developers can write the initial application text in one language, and
|
||||
translations can be provided later without any modifications to the code.
|
||||
translations can be provided later without any modifications to the code. It also
|
||||
demonstrates how to detect the system language settings and show the UI in the appropriate
|
||||
language.
|
||||
|
||||
\image i18n-example.png
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ extern void qt_mac_set_menubar_merge(bool merge);
|
|||
QT_END_NAMESPACE
|
||||
#endif
|
||||
|
||||
LanguageChooser::LanguageChooser(QWidget *parent)
|
||||
LanguageChooser::LanguageChooser(const QString& defaultLang, QWidget *parent)
|
||||
: QDialog(parent, Qt::WindowStaysOnTopHint)
|
||||
{
|
||||
groupBox = new QGroupBox("Languages");
|
||||
|
|
@ -61,6 +61,8 @@ LanguageChooser::LanguageChooser(QWidget *parent)
|
|||
QCheckBox *checkBox = new QCheckBox(languageName(qmFiles[i]));
|
||||
qmFileForCheckBoxMap.insert(checkBox, qmFiles[i]);
|
||||
connect(checkBox, SIGNAL(toggled(bool)), this, SLOT(checkBoxToggled()));
|
||||
if (languageMatch(defaultLang, qmFiles[i]))
|
||||
checkBox->setCheckState(Qt::Checked);
|
||||
groupBoxLayout->addWidget(checkBox, i / 2, i % 2);
|
||||
}
|
||||
groupBox->setLayout(groupBoxLayout);
|
||||
|
|
@ -87,6 +89,14 @@ LanguageChooser::LanguageChooser(QWidget *parent)
|
|||
setWindowTitle("I18N");
|
||||
}
|
||||
|
||||
bool LanguageChooser::languageMatch(const QString& lang, const QString& qmFile)
|
||||
{
|
||||
//qmFile: i18n_xx.qm
|
||||
const QString prefix = "i18n_";
|
||||
const int langTokenLength = 2; /*FIXME: is checking two chars enough?*/
|
||||
return qmFile.midRef(qmFile.indexOf(prefix) + prefix.length(), langTokenLength) == lang.leftRef(langTokenLength);
|
||||
}
|
||||
|
||||
bool LanguageChooser::eventFilter(QObject *object, QEvent *event)
|
||||
{
|
||||
if (event->type() == QEvent::Close) {
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ class LanguageChooser : public QDialog
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
LanguageChooser(QWidget *parent = 0);
|
||||
explicit LanguageChooser(const QString& defaultLang = QString(), QWidget *parent = 0);
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *object, QEvent *event);
|
||||
|
|
@ -73,6 +73,7 @@ private:
|
|||
QStringList findQmFiles();
|
||||
QString languageName(const QString &qmFile);
|
||||
QColor colorForLanguage(const QString &language);
|
||||
static bool languageMatch(const QString& lang, const QString& qmFile);
|
||||
|
||||
QGroupBox *groupBox;
|
||||
QDialogButtonBox *buttonBox;
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@
|
|||
****************************************************************************/
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
#include <QLocale>
|
||||
#include "languagechooser.h"
|
||||
#include "mainwindow.h"
|
||||
|
||||
|
|
@ -48,7 +48,7 @@ int main(int argc, char *argv[])
|
|||
Q_INIT_RESOURCE(i18n);
|
||||
|
||||
QApplication app(argc, argv);
|
||||
LanguageChooser chooser;
|
||||
LanguageChooser chooser(QLocale::system().name());
|
||||
chooser.show();
|
||||
return app.exec();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue