From eeccee307dc534063dd1b15f21744a79e3812bb5 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Tue, 2 Jul 2024 23:57:44 +0200 Subject: [PATCH] Added a menu option for selecting the application language --- es-app/src/guis/GuiMenu.cpp | 25 +++++++++++++++++++++ es-app/src/main.cpp | 2 +- es-core/src/Settings.cpp | 3 +++ es-core/src/utils/LocalizationUtil.cpp | 30 +++++++++++++++++++++++++- es-core/src/utils/LocalizationUtil.h | 2 +- 5 files changed, 59 insertions(+), 3 deletions(-) diff --git a/es-app/src/guis/GuiMenu.cpp b/es-app/src/guis/GuiMenu.cpp index e55c8b9e7..9006ba87c 100644 --- a/es-app/src/guis/GuiMenu.cpp +++ b/es-app/src/guis/GuiMenu.cpp @@ -473,6 +473,31 @@ void GuiMenu::openUIOptions() themeTransitionsFunc(Settings::getInstance()->getString("Theme"), Settings::getInstance()->getString("ThemeTransitions")); + // Application language. + auto applicationLanguage = std::make_shared>( + getHelpStyle(), "APPLICATION LANGUAGE", false); + std::string selectedApplicationLanguage { + Settings::getInstance()->getString("ApplicationLanguage")}; + applicationLanguage->add("AUTOMATIC", "automatic", selectedApplicationLanguage == "automatic"); + applicationLanguage->add("ENGLISH (AMERICAN)", "en_US", selectedApplicationLanguage == "en_US"); + applicationLanguage->add("SWEDISH", "sv_SE", selectedApplicationLanguage == "sv_SE"); + // If there are no objects returned, then there must be a manually modified entry in the + // configuration file. Simply set the application langauge to "automatic" in this case. + if (applicationLanguage->getSelectedObjects().size() == 0) + applicationLanguage->selectEntry(0); + s->addWithLabel("APPLICATION LANGUAGE", applicationLanguage); + s->addSaveFunc([this, applicationLanguage, s] { + if (applicationLanguage->getSelected() != + Settings::getInstance()->getString("ApplicationLanguage")) { + Settings::getInstance()->setString("ApplicationLanguage", + applicationLanguage->getSelected()); + Utils::Localization::setLocale(); + s->setNeedsSaving(); + s->setNeedsCloseMenu([this] { delete this; }); + s->setNeedsRescanROMDirectory(); + } + }); + // Quick system select (navigate between systems in the gamelist view). auto quickSystemSelect = std::make_shared>( getHelpStyle(), "QUICK SYSTEM SELECT", false); diff --git a/es-app/src/main.cpp b/es-app/src/main.cpp index 81717f03f..110c4c0c9 100644 --- a/es-app/src/main.cpp +++ b/es-app/src/main.cpp @@ -733,7 +733,7 @@ int main(int argc, char* argv[]) return 0; } - Utils::Localization::setLocale(Utils::Localization::getLocale()); + Utils::Localization::setLocale(); Scripting::fireEvent("startup"); #if defined(__EMSCRIPTEN__) diff --git a/es-core/src/Settings.cpp b/es-core/src/Settings.cpp index 58a423a46..e67ccc447 100644 --- a/es-core/src/Settings.cpp +++ b/es-core/src/Settings.cpp @@ -43,6 +43,7 @@ namespace // These options are only used internally during the application session: "PortableMode", + "DetectedLocale", "DebugGrid", "DebugText", "DebugImage", @@ -168,6 +169,7 @@ void Settings::setDefaults() mStringMap["ThemeFontSize"] = {"", ""}; mStringMap["ThemeAspectRatio"] = {"", ""}; mStringMap["ThemeTransitions"] = {"automatic", "automatic"}; + mStringMap["ApplicationLanguage"] = {"automatic", "automatic"}; mStringMap["QuickSystemSelect"] = {"leftrightshoulders", "leftrightshoulders"}; mStringMap["StartupSystem"] = {"", ""}; mStringMap["SystemsSorting"] = {"default", "default"}; @@ -363,6 +365,7 @@ void Settings::setDefaults() mIntMap["ApplicationRelease"] = {0, 0}; mStringMap["ApplicationUpdaterLastCheck"] = {"", ""}; + mStringMap["DetectedLocale"] = {"", ""}; mBoolMap["PortableMode"] = {false, false}; mBoolMap["DebugFlag"] = {false, false}; mBoolMap["DebugGrid"] = {false, false}; diff --git a/es-core/src/utils/LocalizationUtil.cpp b/es-core/src/utils/LocalizationUtil.cpp index 52da7b0de..d646cc925 100644 --- a/es-core/src/utils/LocalizationUtil.cpp +++ b/es-core/src/utils/LocalizationUtil.cpp @@ -10,6 +10,7 @@ #include "utils/LocalizationUtil.h" #include "Log.h" +#include "Settings.h" #include "resources/ResourceManager.h" #include "utils/StringUtil.h" @@ -72,8 +73,35 @@ namespace Utils #endif } - void setLocale(const std::pair& localePair) + void setLocale() { + // Only detect locale once (on application startup). + if (Settings::getInstance()->getString("DetectedLocale") == "") { + const std::pair detectedLocale {getLocale()}; + if (detectedLocale.second == "") + Settings::getInstance()->setString("DetectedLocale", detectedLocale.first); + else { + Settings::getInstance()->setString( + "DetectedLocale", detectedLocale.first + "_" + detectedLocale.second); + } + } + + std::string languageSetting {Settings::getInstance()->getString("ApplicationLanguage")}; + std::vector localeVector; + std::pair localePair; + + if (languageSetting == "automatic") { + localeVector = Utils::String::delimitedStringToVector( + Settings::getInstance()->getString("DetectedLocale"), "_"); + } + else { + localeVector = Utils::String::delimitedStringToVector(languageSetting, "_"); + } + if (localeVector.size() == 1) + localePair = std::make_pair(localeVector[0], ""); + else + localePair = std::make_pair(localeVector[0], localeVector[1]); + std::string locale; std::string localePairCombined; diff --git a/es-core/src/utils/LocalizationUtil.h b/es-core/src/utils/LocalizationUtil.h index 8f094310a..cf902dd08 100644 --- a/es-core/src/utils/LocalizationUtil.h +++ b/es-core/src/utils/LocalizationUtil.h @@ -24,7 +24,7 @@ namespace Utils {{"en"}, {"US"}}, {{"sv"}, {"SE"}}}; std::pair getLocale(); - void setLocale(const std::pair& localePair); + void setLocale(); } // namespace Localization