From 6ca71a872d1b3fe1f408ca4a7fe747caa171e716 Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Sat, 28 Dec 2024 13:31:37 +0200 Subject: [PATCH] Use SDL locale --- .../Engine/Platform/Linux/LinuxPlatform.cpp | 10 ++++---- Source/Engine/Platform/Linux/LinuxPlatform.h | 4 ++++ Source/Engine/Platform/SDL/SDLPlatform.cpp | 24 +++++++++++++++++++ Source/Engine/Platform/SDL/SDLPlatform.h | 1 + .../Platform/Windows/WindowsPlatform.cpp | 6 +++-- .../Engine/Platform/Windows/WindowsPlatform.h | 4 ++++ 6 files changed, 43 insertions(+), 6 deletions(-) diff --git a/Source/Engine/Platform/Linux/LinuxPlatform.cpp b/Source/Engine/Platform/Linux/LinuxPlatform.cpp index 5ed6c16e6..202f4a851 100644 --- a/Source/Engine/Platform/Linux/LinuxPlatform.cpp +++ b/Source/Engine/Platform/Linux/LinuxPlatform.cpp @@ -2112,6 +2112,7 @@ bool LinuxPlatform::Init() DeviceId.D = (uint32)UnixCpu.ClockSpeed * UnixCpu.LogicalProcessorCount * UnixCpu.ProcessorCoreCount * UnixCpu.CacheLineSize; } +#if !PLATFORM_SDL // Get user locale string setlocale(LC_ALL, ""); const char* locale = setlocale(LC_CTYPE, NULL); @@ -2121,6 +2122,7 @@ bool LinuxPlatform::Init() UserLocale.Replace('_', '-'); if (UserLocale == TEXT("C")) UserLocale = TEXT("en"); +#endif // Get computer name string gethostname(buffer, UNIX_APP_BUFF_SIZE); @@ -2685,6 +2687,7 @@ void LinuxPlatform::Exit() #endif } +#if !PLATFORM_SDL int32 LinuxPlatform::GetDpi() { return SystemDpi; @@ -2694,6 +2697,7 @@ String LinuxPlatform::GetUserLocaleName() { return UserLocale; } +#endif String LinuxPlatform::GetComputerName() { @@ -2901,14 +2905,12 @@ bool LinuxPlatform::SetWorkingDirectory(const String& path) return chdir(StringAsANSI<>(*path).Get()) != 0; } +#if !PLATFORM_SDL Window* LinuxPlatform::CreateWindow(const CreateWindowSettings& settings) { -#if PLATFORM_SDL - return New(settings); -#else return New(settings); -#endif } +#endif extern char **environ; diff --git a/Source/Engine/Platform/Linux/LinuxPlatform.h b/Source/Engine/Platform/Linux/LinuxPlatform.h index 4154de737..0d7b5d0ac 100644 --- a/Source/Engine/Platform/Linux/LinuxPlatform.h +++ b/Source/Engine/Platform/Linux/LinuxPlatform.h @@ -122,8 +122,10 @@ public: static void Tick(); static void BeforeExit(); static void Exit(); +#if !PLATFORM_SDL static int32 GetDpi(); static String GetUserLocaleName(); +#endif static String GetComputerName(); static bool GetHasFocus(); static bool CanOpenUrl(const StringView& url); @@ -138,7 +140,9 @@ public: static Guid GetUniqueDeviceId(); static String GetWorkingDirectory(); static bool SetWorkingDirectory(const String& path); +#if !PLATFORM_SDL static Window* CreateWindow(const CreateWindowSettings& settings); +#endif static void GetEnvironmentVariables(Dictionary& result); static bool GetEnvironmentVariable(const String& name, String& value); static bool SetEnvironmentVariable(const String& name, const String& value); diff --git a/Source/Engine/Platform/SDL/SDLPlatform.cpp b/Source/Engine/Platform/SDL/SDLPlatform.cpp index 57e1b27b9..cb3d0b659 100644 --- a/Source/Engine/Platform/SDL/SDLPlatform.cpp +++ b/Source/Engine/Platform/SDL/SDLPlatform.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #if PLATFORM_LINUX #include "Engine/Engine/CommandLine.h" @@ -32,6 +33,7 @@ uint32 SDLPlatform::DraggedWindowId = 0; namespace { int32 SystemDpi = 96; + String UserLocale("en"); } bool SDLPlatform::Init() @@ -82,6 +84,23 @@ bool SDLPlatform::Init() if (!SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) Platform::Fatal(String::Format(TEXT("Failed to initialize SDL: {0}."), String(SDL_GetError()))); + int localesCount = 0; + auto locales = SDL_GetPreferredLocales(&localesCount); + for (int i = 0; i < localesCount; i++) + { + auto language = StringAnsiView(locales[i]->language); + auto country = StringAnsiView(locales[i]->country); + if (language.StartsWith("en")) + { + if (country != nullptr) + UserLocale = String::Format(TEXT("{0}-{1}"), String(language), String(locales[i]->country)); + else + UserLocale = String(language); + break; + } + } + SDL_free(locales); + if (InitPlatform()) return true; @@ -227,6 +246,11 @@ int32 SDLPlatform::GetDpi() return SystemDpi; } +String SDLPlatform::GetUserLocaleName() +{ + return UserLocale; +} + void SDLPlatform::OpenUrl(const StringView& url) { StringAnsi urlStr(url); diff --git a/Source/Engine/Platform/SDL/SDLPlatform.h b/Source/Engine/Platform/SDL/SDLPlatform.h index 5e4f001c2..d67495688 100644 --- a/Source/Engine/Platform/SDL/SDLPlatform.h +++ b/Source/Engine/Platform/SDL/SDLPlatform.h @@ -70,6 +70,7 @@ public: static void SetHighDpiAwarenessEnabled(bool enable); static BatteryInfo GetBatteryInfo(); static int32 GetDpi(); + static String GetUserLocaleName(); static void OpenUrl(const StringView& url); static Float2 GetMousePosition(); static void SetMousePosition(const Float2& pos); diff --git a/Source/Engine/Platform/Windows/WindowsPlatform.cpp b/Source/Engine/Platform/Windows/WindowsPlatform.cpp index 022bb4faf..4b2c237b5 100644 --- a/Source/Engine/Platform/Windows/WindowsPlatform.cpp +++ b/Source/Engine/Platform/Windows/WindowsPlatform.cpp @@ -669,11 +669,13 @@ bool WindowsPlatform::Init() DWORD tmp; Char buffer[256]; +#if !PLATFORM_SDL // Get user locale string if (GetUserDefaultLocaleName(buffer, LOCALE_NAME_MAX_LENGTH)) { UserLocale = String(buffer); } +#endif // Get computer name string if (GetComputerNameW(buffer, &tmp)) @@ -822,6 +824,7 @@ BatteryInfo WindowsPlatform::GetBatteryInfo() return info; } +#if !PLATFORM_SDL int32 WindowsPlatform::GetDpi() { return SystemDpi; @@ -831,6 +834,7 @@ String WindowsPlatform::GetUserLocaleName() { return UserLocale; } +#endif String WindowsPlatform::GetComputerName() { @@ -1201,12 +1205,10 @@ int32 WindowsPlatform::CreateProcess(CreateProcessSettings& settings) } #if !PLATFORM_SDL - Window* WindowsPlatform::CreateWindow(const CreateWindowSettings& settings) { return New(settings); } - #endif void* WindowsPlatform::LoadLibrary(const Char* filename) diff --git a/Source/Engine/Platform/Windows/WindowsPlatform.h b/Source/Engine/Platform/Windows/WindowsPlatform.h index 8d377ed56..cae08f49e 100644 --- a/Source/Engine/Platform/Windows/WindowsPlatform.h +++ b/Source/Engine/Platform/Windows/WindowsPlatform.h @@ -63,8 +63,10 @@ public: #endif static void SetHighDpiAwarenessEnabled(bool enable); static BatteryInfo GetBatteryInfo(); +#if !PLATFORM_SDL static int32 GetDpi(); static String GetUserLocaleName(); +#endif static String GetComputerName(); static bool GetHasFocus(); static bool CanOpenUrl(const StringView& url); @@ -78,7 +80,9 @@ public: static bool GetEnvironmentVariable(const String& name, String& value); static bool SetEnvironmentVariable(const String& name, const String& value); static int32 CreateProcess(CreateProcessSettings& settings); +#if !PLATFORM_SDL static Window* CreateWindow(const CreateWindowSettings& settings); +#endif static void* LoadLibrary(const Char* filename); #if CRASH_LOG_ENABLE static Array GetStackFrames(int32 skipCount = 0, int32 maxDepth = 60, void* context = nullptr);