1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +02:00

Added detection of OS language on first run

This commit is contained in:
Ivan Savenko 2023-06-27 22:33:42 +03:00
parent 1f525c59c3
commit fc696fa0b3
3 changed files with 47 additions and 13 deletions

View File

@ -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);

View File

@ -53,6 +53,7 @@ public:
void updateTranslation();
void computeSidePanelSizes();
void detectPreferredLanguage();
void enterSetup();
void exitSetup();
void switchToModsTab();

View File

@ -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<Options, 12> 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<size_t>(ELanguages::COUNT), "Languages array is missing a value!");