Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locales(langs) detecting #72

Merged
merged 3 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions src/util/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,62 @@
#include <sstream>
#include <iomanip>
#include <time.h>
#include <iostream>

#include "../typedefs.h"

std::string platform::detect_locale() {
// TODO: implement
std::string name = setlocale(LC_ALL, nullptr);
if (name.find("ru_RU") != std::string::npos) {
namespace platform {
const std::string DEFAULT_LOCALE = "en_EN";
}

/*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 <Windows.h>

#include "./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(){
}

#endif
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
1 change: 1 addition & 0 deletions src/util/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace platform {
extern void configure_encoding();
extern std::string detect_locale();
extern std::string get_locale_by_lang(std::string lang);
}

#endif // UTIL_PLATFORM_H_
Loading