From 552f540a499b38404296bb496aee1866a397d8bb Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Sun, 30 Jun 2024 23:10:04 +0200 Subject: [PATCH] (macOS) Added language auto-detection --- es-core/src/utils/LocalizationUtil.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/es-core/src/utils/LocalizationUtil.cpp b/es-core/src/utils/LocalizationUtil.cpp index 0d8721592..07aa50963 100644 --- a/es-core/src/utils/LocalizationUtil.cpp +++ b/es-core/src/utils/LocalizationUtil.cpp @@ -13,6 +13,8 @@ #include "resources/ResourceManager.h" #include "utils/StringUtil.h" +#include + #include #include @@ -38,6 +40,21 @@ namespace Utils locale.erase(locale.find('\0')); return locale; +#elif defined(__APPLE__) + // The SDL locale function does not seem to always return the correct result + // (at least not on Windows) but for macOS it's very annoying to use the OS-supplied + // locale facilities, so here we still use the SDL method. + SDL_Locale* preferredLocales {SDL_GetPreferredLocales()}; + + if (preferredLocales == nullptr) + return "en_US"; + + std::string primaryLocale {preferredLocales->language}; + if (preferredLocales->country != nullptr) + primaryLocale.append("_").append(preferredLocales->country); + + SDL_free(preferredLocales); + return primaryLocale; #else std::string language; @@ -103,6 +120,9 @@ namespace Utils const LCID localeID {LocaleNameToLCID(Utils::String::stringToWideString(locale).c_str(), LOCALE_ALLOW_NEUTRAL_NAMES)}; SetThreadLocale(localeID); +#elif defined(__APPLE__) + // This is seemingly needed specifically on macOS but not on Linux. + setenv("LANGUAGE", locale.c_str(), 1); #else setlocale(LC_MESSAGES, std::string {locale + ".UTF-8"}.c_str()); #endif