Added a menu option for selecting the application language

This commit is contained in:
Leon Styhre 2024-07-02 23:57:44 +02:00
parent e3148e6cda
commit eeccee307d
5 changed files with 59 additions and 3 deletions

View file

@ -473,6 +473,31 @@ void GuiMenu::openUIOptions()
themeTransitionsFunc(Settings::getInstance()->getString("Theme"),
Settings::getInstance()->getString("ThemeTransitions"));
// Application language.
auto applicationLanguage = std::make_shared<OptionListComponent<std::string>>(
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<OptionListComponent<std::string>>(
getHelpStyle(), "QUICK SYSTEM SELECT", false);

View file

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

View file

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

View file

@ -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<std::string, std::string>& localePair)
void setLocale()
{
// Only detect locale once (on application startup).
if (Settings::getInstance()->getString("DetectedLocale") == "") {
const std::pair<std::string, std::string> 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<std::string> localeVector;
std::pair<std::string, std::string> 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;

View file

@ -24,7 +24,7 @@ namespace Utils
{{"en"}, {"US"}}, {{"sv"}, {"SE"}}};
std::pair<std::string, std::string> getLocale();
void setLocale(const std::pair<std::string, std::string>& localePair);
void setLocale();
} // namespace Localization