detect system locale

This commit is contained in:
A-lex-Ra
2023-12-25 20:26:56 +06:00
parent 06aa6023b3
commit ca3697301e
2 changed files with 40 additions and 7 deletions
+39 -7
View File
@@ -3,11 +3,15 @@
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
#include <time.h> #include <time.h>
#include <iostream>
#include "../typedefs.h" #include "../typedefs.h"
#define SETTINGS_FILE "settings.toml" namespace platform {
#define CONTROLS_FILE "controls.json" 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; using std::filesystem::path;
@@ -20,25 +24,53 @@ path platform::get_controls_file() {
return path(CONTROLS_FILE); return path(CONTROLS_FILE);
} }
std::string platform::detect_locale() { /*System locale to engine locale mapping*/
// TODO: implement std::string platform::get_locale_by_lang(std::string lang) {
std::string name = setlocale(LC_ALL, nullptr); if (lang == "ru") {
if (name.find("ru_RU") != std::string::npos) {
return "ru_RU"; return "ru_RU";
} }
return "en_US"; return DEFAULT_LOCALE;
} }
#ifdef WIN32 #ifdef WIN32
#include <Windows.h> #include <Windows.h>
#include "../util/stringutil.h"
void platform::configure_encoding() { void platform::configure_encoding() {
// set utf-8 encoding to console output // set utf-8 encoding to console output
SetConsoleOutputCP(CP_UTF8); SetConsoleOutputCP(CP_UTF8);
setvbuf(stdout, nullptr, _IOFBF, 1000); 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 #else
void platform::configure_encoding(){ 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 #endif
+1
View File
@@ -9,6 +9,7 @@ namespace platform {
extern std::filesystem::path get_settings_file(); extern std::filesystem::path get_settings_file();
extern std::filesystem::path get_controls_file(); extern std::filesystem::path get_controls_file();
extern std::string detect_locale(); extern std::string detect_locale();
extern std::string get_locale_by_lang(std::string lang);
} }
#endif // UTIL_PLATFORM_H_ #endif // UTIL_PLATFORM_H_