From fc696fa0b38a070e25b6e0c5ff3be7c349617961 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Tue, 27 Jun 2023 22:33:42 +0300 Subject: [PATCH] Added detection of OS language on first run --- launcher/mainwindow_moc.cpp | 32 +++++++++++++++++++++++++++++++- launcher/mainwindow_moc.h | 1 + lib/Languages.h | 27 +++++++++++++++------------ 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/launcher/mainwindow_moc.cpp b/launcher/mainwindow_moc.cpp index 17e01cea0..ccacca57e 100644 --- a/launcher/mainwindow_moc.cpp +++ b/launcher/mainwindow_moc.cpp @@ -15,6 +15,7 @@ #include "../lib/CConfigHandler.h" #include "../lib/VCMIDirs.h" +#include "../lib/Languages.h" #include "../lib/filesystem/Filesystem.h" #include "../lib/logging/CBasicLogConfigurator.h" @@ -74,6 +75,12 @@ MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent), ui(new Ui::MainWindow) { load(); // load FS before UI + + bool setupCompleted = settings["launcher"]["setupCompleted"].Bool(); + + if (!setupCompleted) + detectPreferredLanguage(); + updateTranslation(); // load translation ui->setupUi(this); @@ -103,7 +110,6 @@ MainWindow::MainWindow(QWidget * parent) computeSidePanelSizes(); bool h3DataFound = CResourceHandler::get()->existsResource(ResourceID("DATA/GENRLTXT.TXT")); - bool setupCompleted = settings["launcher"]["setupCompleted"].Bool(); if (h3DataFound && setupCompleted) ui->tabListWidget->setCurrentIndex(TabRows::MODS); @@ -116,6 +122,30 @@ MainWindow::MainWindow(QWidget * parent) UpdateDialog::showUpdateDialog(false); } +void MainWindow::detectPreferredLanguage() +{ + auto preferredLanguages = QLocale::system().uiLanguages(); + + std::string selectedLanguage; + + for (auto const & userLang : preferredLanguages) + { + logGlobal->info("Preferred language: %s", userLang.toStdString()); + + for (auto const & vcmiLang : Languages::getLanguageList()) + if (vcmiLang.tagIETF == userLang.toStdString()) + selectedLanguage = vcmiLang.identifier; + } + + logGlobal->info("Selected language: %s", selectedLanguage); + + if (!selectedLanguage.empty()) + { + Settings node = settings.write["general"]["language"]; + node->String() = selectedLanguage; + } +} + void MainWindow::enterSetup() { ui->startGameButton->setEnabled(false); diff --git a/launcher/mainwindow_moc.h b/launcher/mainwindow_moc.h index f16329ee3..bdc766046 100644 --- a/launcher/mainwindow_moc.h +++ b/launcher/mainwindow_moc.h @@ -53,6 +53,7 @@ public: void updateTranslation(); void computeSidePanelSizes(); + void detectPreferredLanguage(); void enterSetup(); void exitSetup(); void switchToModsTab(); diff --git a/lib/Languages.h b/lib/Languages.h index f87ae6e0b..53201463e 100644 --- a/lib/Languages.h +++ b/lib/Languages.h @@ -46,6 +46,9 @@ struct Options /// encoding that is used by H3 for this language std::string encoding; + /// primary IETF language tag + std::string tagIETF; + /// VCMI is capable of detecting H3 install in this language bool hasDetection = false; @@ -57,19 +60,19 @@ inline const auto & getLanguageList() { static const std::array languages { { - { "chinese", "Chinese", "简体中文", "GBK", true, true }, // Note: actually Simplified Chinese - { "english", "English", "English", "CP1252", true, true }, - { "french", "French", "Français", "CP1252", true, true }, - { "german", "German", "Deutsch", "CP1252", true, true }, - { "korean", "Korean", "한국어", "CP949", false, false }, - { "polish", "Polish", "Polski", "CP1250", true, true }, - { "russian", "Russian", "Русский", "CP1251", true, true }, - { "spanish", "Spanish", "Español", "CP1252", false, true }, - { "ukrainian", "Ukrainian", "Українська", "CP1251", true, true }, + { "chinese", "Chinese", "简体中文", "GBK", "zh", true, true }, // Note: actually Simplified Chinese + { "english", "English", "English", "CP1252", "en", true, true }, + { "french", "French", "Français", "CP1252", "fr", true, true }, + { "german", "German", "Deutsch", "CP1252", "de", true, true }, + { "korean", "Korean", "한국어", "CP949", "ko", false, false }, + { "polish", "Polish", "Polski", "CP1250", "pl", true, true }, + { "russian", "Russian", "Русский", "CP1251", "ru", true, true }, + { "spanish", "Spanish", "Español", "CP1252", "es", false, true }, + { "ukrainian", "Ukrainian", "Українська", "CP1251", "uk", true, true }, - { "other_cp1250", "Other (East European)", "", "CP1251", false, false }, - { "other_cp1251", "Other (Cyrillic Script)", "", "CP1250", false, false }, - { "other_cp1252", "Other (West European)", "", "CP1252", false, false } + { "other_cp1250", "Other (East European)", "", "CP1251", "", false, false }, + { "other_cp1251", "Other (Cyrillic Script)", "", "CP1250", "", false, false }, + { "other_cp1252", "Other (West European)", "", "CP1252", "", false, false } } }; static_assert(languages.size() == static_cast(ELanguages::COUNT), "Languages array is missing a value!");