macOS system language detection.

It appears the system language detection doesn't work on [some versions
of?] macOS.  Fixed.

Strangely, querying the Cocoa API for the system locale on a system in
US with the preferred language set to Korean produces the invalid locale
"ko-US" instead of the expected "ko-KR".  This behavior of returning the
language with a invalid region qualifier for the language appears to
happen only with languages where macOS does not have regional variants
(this issue does not seem to occur with Canadian English, en-CA, for
example.)  A fuzzy matching locale function has been added to handle
this issue.
This commit is contained in:
Mark Kim 2022-02-06 20:15:40 -05:00
parent c2ab6d461c
commit c9e195549f
6 changed files with 96 additions and 4 deletions

View file

@ -34,6 +34,9 @@
#define MACOS_PICTURES_PATH "%s/Pictures"
static char *APPLE_LOCALE = NULL;
static void setupApplicationMenu(void)
{
/*
@ -130,10 +133,35 @@ static void removeSdlMenu(void)
void apple_init(void)
{
/* Override SDL's default menu with our gettext-translatable menu. We do
* this by adding our menus, then removing the menus installed by SDL. */
* this by removing the menus added by SDL, then adding ours. */
removeSdlMenu();
setupApplicationMenu();
setupWindowMenu();
removeSdlMenu();
}
const char *apple_locale(void)
{
if(!APPLE_LOCALE) {
const char *locale = [[[NSLocale preferredLanguages] firstObject] UTF8String];
/* Copy to writable memory */
APPLE_LOCALE = strdup(locale);
if(!APPLE_LOCALE) {
perror("apple_locale");
return "C"; /* Default to C */
}
/* Change the locale hyphen separator to underscore (e.g., en-US to en_US) */
if(APPLE_LOCALE[2] == '-') {
APPLE_LOCALE[2] = '_';
}
}
DEBUG_PRINTF("locale=%s\n", APPLE_LOCALE);
return APPLE_LOCALE;
}