Skip to content

Commit 55d310b

Browse files
authored
i18n: use GetUserPreferredUILanguages on Windows and NSLocale on macOS (#2256)
1 parent 3339416 commit 55d310b

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

core/oslib/i18n.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,30 @@ const char *translatePlural(const char *msg, const char *msgPlural, int num) {
196196
return tr.c_str();
197197
}
198198

199-
#if defined(_WIN32) && !defined(LIBRETRO)
199+
#if !defined(LIBRETRO) && defined(_WIN32)
200200
std::string getCurrentLocale()
201201
{
202-
wchar_t wname[128];
203-
if (GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SNAME, wname, std::size(wname)) == 0) {
204-
ERROR_LOG(COMMON, "GetLocaleEx failed: %x", GetLastError());
202+
ULONG numLanguages = 0;
203+
ULONG bufferSize = 0;
204+
if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numLanguages, nullptr, &bufferSize) || bufferSize == 0)
205+
{
206+
ERROR_LOG(COMMON, "GetUserPreferredUILanguages(size) failed: %lx", GetLastError());
207+
return "en";
208+
}
209+
210+
std::vector<wchar_t> buffer(bufferSize);
211+
if (!GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numLanguages, buffer.data(), &bufferSize))
212+
{
213+
ERROR_LOG(COMMON, "GetUserPreferredUILanguages(data) failed: %lx", GetLastError());
205214
return "en";
206215
}
216+
207217
nowide::stackstring name;
208-
if (name.convert(wname))
218+
if (name.convert(buffer.data()))
209219
return name.get();
210-
else
211-
return "en";
220+
221+
ERROR_LOG(COMMON, "UTF-16 to UTF-8 conversion failed");
222+
return "en";
212223
}
213224
#endif
214225
}

core/oslib/i18n.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ using std::locale;
3838

3939
void init();
4040

41-
#if !defined(LIBRETRO) && !defined(__SWITCH__) && !defined(_WIN32)
41+
#if !defined(LIBRETRO) && !defined(__SWITCH__) && !defined(_WIN32) && !defined(TARGET_MAC)
4242

4343
static inline std::string getCurrentLocale() {
4444
return setlocale(LC_MESSAGES, nullptr);

shell/apple/emulator-osx/emulator-osx/osx-main.mm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,13 @@ void os_VideoRoutingTermVk()
281281
}
282282

283283
}
284+
285+
namespace i18n
286+
{
287+
288+
std::string getCurrentLocale()
289+
{
290+
return [[[NSLocale preferredLanguages] objectAtIndex:0] UTF8String];
291+
}
292+
293+
}

0 commit comments

Comments
 (0)