-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
111 lines (90 loc) · 3.25 KB
/
Copy pathmainwindow.cpp
File metadata and controls
111 lines (90 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "mainwindow.hpp"
#include <QtWidgets>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
m_langPath(QApplication::applicationDirPath())
{
m_button = new QPushButton(this);
m_button->setMinimumSize(400, 205);
setCentralWidget(m_button);
createLanguageMenu();
updateText();
}
MainWindow::~MainWindow()
{
}
void MainWindow::updateText() {
if (m_actionES == nullptr) {
// Create here?
}
QString name(". <FIXED VAL>. ");
m_button->setText(tr("Hello World!") + name + tr("Another translatable phrase"));
m_langMenu->setTitle(tr("&Language"));
m_actionEN->setText(tr("English"));
m_actionFR->setText(tr("French"));
m_actionES->setText(tr("Spanish"));
}
void MainWindow::createLanguageMenu() {
m_langMenu = menuBar()->addMenu("&Language");
m_actionEN = new QAction(tr("English"), this);
m_actionEN->setStatusTip("Change language to English");
connect(m_actionEN, &QAction::triggered, this, [this]() { MainWindow::loadLanguage("en"); });
m_langMenu->addAction(m_actionEN);
m_actionFR = new QAction("French", this);
m_actionFR->setStatusTip("Changer la langue vers le Français");
connect(m_actionFR, &QAction::triggered, this, [this]() { MainWindow::loadLanguage("fr"); });
m_langMenu->addAction(m_actionFR);
m_actionES = new QAction(tr("Spanish"), this);
m_actionES->setStatusTip("Cambiar idioma a español");
connect(m_actionES, &QAction::triggered, this, [this]() { MainWindow::loadLanguage("es"); });
m_langMenu->addAction(m_actionES);
}
bool MainWindow::switchTranslator(QTranslator& translator, const QString& filename) {
// remove the old translator
qApp->removeTranslator(&translator);
// load the new translator
auto fPath = QDir(m_langPath).filePath(filename);
if(translator.load( fPath )) { //Here Path and Filename has to be entered because the system didn't find the QM Files else
qApp->installTranslator(&translator);
return true;
} else {
qDebug() << "Failed to load " << fPath;
}
return false;
}
void MainWindow::loadLanguage(const QString& rLanguage) {
qDebug() << "Trying to change language from '" << m_currLang << "' to '" << rLanguage << "'.";
if(m_currLang != rLanguage) {
m_currLang = rLanguage;
QLocale locale = QLocale(m_currLang);
QLocale::setDefault(locale);
QString languageName = QLocale::languageToString(locale.language());
switchTranslator(m_translator, QString("QtInternationalization_%1.qm").arg(rLanguage));
//switchTranslator(m_translatorQt, QString("qt_%1.qm").arg(rLanguage));
}
}
void MainWindow::changeEvent(QEvent* event) {
if(0 != event) {
switch(event->type()) {
// this event is send if a translator is loaded
case QEvent::LanguageChange:
{
qDebug() << "Caught a QEvent::LanguageChange";
// I am NOT using UI (Qt Designer), so that's problematic...
// ui.retranslateUi(this);
// Instead I set the tr text for the button inside a slot... call it in the Ctor, and call it again for each language change
updateText();
break;
}
// this event is send, if the system, language changes
case QEvent::LocaleChange:
{
QString locale = QLocale::system().name();
locale.truncate(locale.lastIndexOf('_'));
loadLanguage(locale);
}
break;
}
}
QMainWindow::changeEvent(event);
}