From ca3697301ea90da563f2d3e8d74b15bcf72c5465 Mon Sep 17 00:00:00 2001 From: A-lex-Ra Date: Mon, 25 Dec 2023 20:26:56 +0600 Subject: [PATCH 1/2] detect system locale --- src/util/platform.cpp | 46 ++++++++++++++++++++++++++++++++++++------- src/util/platform.h | 1 + 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/util/platform.cpp b/src/util/platform.cpp index 95edbbca6..9238d6921 100644 --- a/src/util/platform.cpp +++ b/src/util/platform.cpp @@ -3,11 +3,15 @@ #include #include #include +#include #include "../typedefs.h" -#define SETTINGS_FILE "settings.toml" -#define CONTROLS_FILE "controls.json" +namespace platform { + const std::string SETTINGS_FILE = "settings.toml"; + const std::string CONTROLS_FILE = "controls.json"; + const std::string DEFAULT_LOCALE = "en_EN"; +} using std::filesystem::path; @@ -20,25 +24,53 @@ path platform::get_controls_file() { return path(CONTROLS_FILE); } -std::string platform::detect_locale() { - // TODO: implement - std::string name = setlocale(LC_ALL, nullptr); - if (name.find("ru_RU") != std::string::npos) { +/*System locale to engine locale mapping*/ +std::string platform::get_locale_by_lang(std::string lang) { + if (lang == "ru") { return "ru_RU"; } - return "en_US"; + return DEFAULT_LOCALE; } #ifdef WIN32 #include +#include "../util/stringutil.h" + void platform::configure_encoding() { // set utf-8 encoding to console output SetConsoleOutputCP(CP_UTF8); setvbuf(stdout, nullptr, _IOFBF, 1000); } + +std::string platform::detect_locale() { + LCID lcid = GetThreadLocale(); + wchar_t preferredLocaleName[LOCALE_NAME_MAX_LENGTH]; + if (LCIDToLocaleName(lcid, preferredLocaleName, LOCALE_NAME_MAX_LENGTH, 0) == 0) { + std::cout << "error in platform::detect_locale! LCIDToLocaleName failed." << std::endl; + } + wchar_t parentLocaleName[LOCALE_NAME_MAX_LENGTH]; + if (GetLocaleInfoEx(preferredLocaleName, LOCALE_SPARENT, parentLocaleName, LOCALE_NAME_MAX_LENGTH) == 0){ + std::cout << "error in platform::detect_locale! GetLocaleInfoEx failed." << std::endl; + } + std::wcout << "detected environment language locale: " << parentLocaleName << std::endl; + + std::string preferredLang = util::wstr2str_utf8(parentLocaleName); + return get_locale_by_lang(preferredLang); +} + #else void platform::configure_encoding(){ } +std::string platform::detect_locale() { + std::string programLocaleName = setlocale(LC_ALL, nullptr); + std::string preferredLocaleName = setlocale(LC_ALL, ""); + std::cout << "detected environment locale: " << preferredLocaleName << std::endl; + setlocale(LC_ALL, programLocaleName.c_str()); + + std::string preferredLang = preferredLocaleName.substr(0, 2); + return get_locale_by_lang(preferredLang); +} + #endif \ No newline at end of file diff --git a/src/util/platform.h b/src/util/platform.h index 2a3206989..30f3f9f06 100644 --- a/src/util/platform.h +++ b/src/util/platform.h @@ -9,6 +9,7 @@ namespace platform { extern std::filesystem::path get_settings_file(); extern std::filesystem::path get_controls_file(); extern std::string detect_locale(); + extern std::string get_locale_by_lang(std::string lang); } #endif // UTIL_PLATFORM_H_ \ No newline at end of file From 1dae8c2c31685bc6e378f2d3602286074ca05bf0 Mon Sep 17 00:00:00 2001 From: A-lex-Ra Date: Mon, 25 Dec 2023 20:34:39 +0600 Subject: [PATCH 2/2] postmerge fix --- src/util/platform.cpp | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/util/platform.cpp b/src/util/platform.cpp index 8ea3044b5..c4a3500f4 100644 --- a/src/util/platform.cpp +++ b/src/util/platform.cpp @@ -8,22 +8,9 @@ #include "../typedefs.h" namespace platform { - const std::string SETTINGS_FILE = "settings.toml"; - const std::string CONTROLS_FILE = "controls.json"; const std::string DEFAULT_LOCALE = "en_EN"; } -using std::filesystem::path; - - -path platform::get_settings_file() { - return path(SETTINGS_FILE); -} - -path platform::get_controls_file() { - return path(CONTROLS_FILE); -} - /*System locale to engine locale mapping*/ std::string platform::get_locale_by_lang(std::string lang) { if (lang == "ru") { @@ -35,7 +22,7 @@ std::string platform::get_locale_by_lang(std::string lang) { #ifdef WIN32 #include -#include "../util/stringutil.h" +#include "./stringutil.h" void platform::configure_encoding() { // set utf-8 encoding to console output