diff -r 593a658cba8e -r 73e448b2943d src/mainwindow.cpp --- a/src/mainwindow.cpp Thu Oct 03 11:45:44 2019 +0300 +++ b/src/mainwindow.cpp Thu Oct 03 23:44:28 2019 +0300 @@ -9,10 +9,13 @@ ui{std::make_unique()}, documents{this} { - ui->setupUi(this); + this->ui->setupUi(this); connect(ui->actionNew, &QAction::triggered, this, &MainWindow::newModel); + connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::openModel); connect(ui->actionQuit, &QAction::triggered, this, &QMainWindow::close); - updateTitle(); + this->updateTitle(); + this->loadLocales(); + changeLanguage(QLocale::system().name()); } MainWindow::~MainWindow() @@ -30,7 +33,7 @@ void MainWindow::openModel() { const QString path = QFileDialog::getOpenFileName( - this, "Open model", "", "LDraw models (*.ldr *.dat)"); + this, tr("Open model"), "", tr("LDraw models (*.ldr *.dat)")); if (not path.isEmpty()) { QFile file{path}; @@ -41,8 +44,8 @@ } else { - QMessageBox::critical(this, "Problem opening file", - QString{"Could not open %1: %2"} + QMessageBox::critical(this, tr("Problem opening file"), + tr("Could not open %1: %2") .arg(path) .arg(file.errorString())); } @@ -50,6 +53,51 @@ } /** + * @brief Changes the application language to the specified language + * @param localeCode Code of the locale to translate to + */ +void MainWindow::changeLanguage(const QString& localeCode) +{ + if (not localeCode.isEmpty() and localeCode != this->currentLanguage) + { + this->currentLanguage = localeCode; + QLocale::setDefault({localeCode}); + qApp->removeTranslator(&this->translator); + const bool loadSuccessful = this->translator.load(pathToTranslation(localeCode)); + if (loadSuccessful) + { + qApp->installTranslator(&this->translator); + } + } +} + +void MainWindow::languageChangeRequested() +{ + QAction* const senderAction = qobject_cast(sender()); + if (senderAction != nullptr) + { + const QString localeCode = senderAction->data().toString(); + this->changeLanguage(localeCode); + } +} + +void MainWindow::changeEvent(QEvent* event) +{ + if (event != nullptr) + { + switch (event->type()) + { + case QEvent::LanguageChange: + this->ui->retranslateUi(this); + break; + default: + break; + } + } + QMainWindow::changeEvent(event); +} + +/** * @brief Creates a new tab widget for the specified model. * @param model Model to get a new tab widget for * @return widget @@ -105,3 +153,31 @@ title += fullVersionString(); setWindowTitle(title); } + +QString MainWindow::pathToTranslation(const QString& localeCode) +{ + QDir dir {":/locale"}; + return dir.filePath(localeCode + ".qm"); +} + +void MainWindow::loadLocales() +{ + QDir dir {":/locale"}; + this->ui->menuLanguage->clear(); + QVector localeCodes = {"en"}; + for (const QFileInfo& file : dir.entryInfoList(QDir::Files)) + { + localeCodes.append(file.baseName()); + } + for (const QString& localeCode : localeCodes) + { + const QLocale locale{localeCode}; + const QString languageName = QLocale::languageToString(locale.language()); + const QIcon flag{":/flags/" + localeCode + ".png"}; + QAction* action = new QAction{languageName, this}; + action->setData(localeCode); + action->setIcon(flag); + this->ui->menuLanguage->addAction(action); + connect(action, &QAction::triggered, this, &MainWindow::languageChangeRequested); + } +}