From 404369cd9f59dea1e66a3828b1dba0baaba958a9 Mon Sep 17 00:00:00 2001 From: Inori Date: Sat, 31 Aug 2019 14:42:16 -0400 Subject: [PATCH] heck --- src/lang-fun.cpp | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ src/lang-fun.h | 3 ++ 2 files changed, 97 insertions(+) create mode 100644 src/lang-fun.cpp create mode 100644 src/lang-fun.h diff --git a/src/lang-fun.cpp b/src/lang-fun.cpp new file mode 100644 index 00000000..be61275b --- /dev/null +++ b/src/lang-fun.cpp @@ -0,0 +1,94 @@ +#if defined(__WIN32__) + +#include +#include + +#else +#include + +#ifdef __APPLE__ +extern "C" { +#include +#include +} +#endif + +#endif + +#include +#include "lang-fun.h" + +// ====================================================================== +// https://github.com/wine-mirror/wine/blob/master/dlls/kernel32/locale.c +// ====================================================================== + +#ifdef __APPLE__ +/*********************************************************************** + * get_mac_locale + * + * Return a locale identifier string reflecting the Mac locale, in a form + * that parse_locale_name() will understand. So, strip out unusual + * things like script, variant, etc. Or, rather, just construct it as + * [_].UTF-8. + */ +static const char* get_mac_locale(void) +{ + static char mac_locale[50]; + + if (!mac_locale[0]) + { + CFLocaleRef locale = CFLocaleCopyCurrent(); + CFStringRef lang = (CFStringRef)CFLocaleGetValue( locale, kCFLocaleLanguageCode ); + CFStringRef country = (CFStringRef)CFLocaleGetValue( locale, kCFLocaleCountryCode ); + CFStringRef locale_string; + + if (country) + locale_string = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@_%@"), lang, country); + else + locale_string = CFStringCreateCopy(NULL, lang); + + CFStringGetCString(locale_string, mac_locale, sizeof(mac_locale), kCFStringEncodingUTF8); + strcat(mac_locale, ".UTF-8"); + + CFRelease(locale); + CFRelease(locale_string); + } + + return mac_locale; +} +#endif + +const char *getUserLanguage() +{ + static char buf[50] = {0}; +#if defined(__WIN32__) + wchar_t wbuf[50] = {0}; + LANGID lid = GetUserDefaultLangId(); + LCIDToLocaleName(lid, wbuf, sizeof(wbuf), 0); + wcstombs(buf, wbuf, sizeof(buf)); +#else + strncpy(buf, std::locale("").name().c_str(), sizeof(buf)); +#ifdef __APPLE__ + if (!buf[0]) strncpy(buf, get_mac_locale(), sizeof(buf)); +#endif +#endif + + for (int i = 0; i < strlen(buf); i++) + { +#ifdef __WIN32__ + if (buf[i] == '-') + { + buf[i] = '_'; +#else + if (buf[i] == '.') + { + buf[i] = 0; +#endif + break; + } + } + + return buf; +} + + diff --git a/src/lang-fun.h b/src/lang-fun.h new file mode 100644 index 00000000..cd78310b --- /dev/null +++ b/src/lang-fun.h @@ -0,0 +1,3 @@ +#pragma once + +const char *getUserLanguage();