locale: Implemented SDL_GetPreferredLocales().

This was something I proposed a long time ago, Sylvain Becker did
additional work on it, then back to me.

Fixes Bugzilla #2131.
This commit is contained in:
Ryan C. Gordon
2020-05-04 02:27:29 -04:00
parent 1e5dd06f83
commit fa23e3d00b
42 changed files with 1318 additions and 4 deletions

103
src/locale/SDL_locale.c Normal file
View File

@@ -0,0 +1,103 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../SDL_internal.h"
#include "SDL_syslocale.h"
#include "SDL_hints.h"
static SDL_Locale *
build_locales_from_csv_string(char *csv)
{
size_t num_locales = 1; /* at least one */
size_t slen;
size_t alloclen;
char *ptr;
SDL_Locale *loc;
SDL_Locale *retval;
if (!csv || !csv[0]) {
return NULL; /* nothing to report */
}
for (ptr = csv; *ptr; ptr++) {
if (*ptr == ',') {
num_locales++;
}
}
num_locales++; /* one more for terminator */
slen = ((size_t) (ptr - csv)) + 1; /* strlen(csv) + 1 */
alloclen = slen + (num_locales * sizeof (SDL_Locale));
loc = retval = (SDL_Locale *) SDL_calloc(1, alloclen);
if (!retval) {
SDL_OutOfMemory();
return NULL; /* oh well */
}
ptr = (char *) (retval + num_locales);
SDL_strlcpy(ptr, csv, slen);
while (SDL_TRUE) { /* parse out the string */
while (*ptr == ' ') ptr++; /* skip whitespace. */
if (*ptr == '\0') {
break;
}
loc->language = ptr++;
while (SDL_TRUE) {
const char ch = *ptr;
if (ch == '_') {
*(ptr++) = '\0';
loc->country = ptr;
} else if (ch == ' ') {
*(ptr++) = '\0'; /* trim ending whitespace and keep going. */
} else if (ch == ',') {
*(ptr++) = '\0';
loc++;
break;
} else if (ch == '\0') {
loc++;
break;
} else {
ptr++; /* just keep going, still a valid string */
}
}
}
return retval;
}
SDL_Locale *
SDL_GetPreferredLocales(void)
{
char locbuf[128]; /* enough for 21 "xx_YY," language strings. */
const char *hint = SDL_GetHint(SDL_HINT_PREFERRED_LOCALES);
if (hint) {
SDL_strlcpy(locbuf, hint, sizeof (locbuf));
} else {
SDL_zeroa(locbuf);
SDL_SYS_GetPreferredLocales(locbuf, sizeof (locbuf));
}
return build_locales_from_csv_string(locbuf);
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,29 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
/* This is the system specific header for the SDL locale API */
#include "SDL_locale.h"
extern void SDL_SYS_GetPreferredLocales(char *buf, size_t buflen);
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,32 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#include "../SDL_syslocale.h"
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{
Android_JNI_GetLocale(buf, buflen);
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,33 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#include "../SDL_syslocale.h"
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{
/* dummy implementation. Caller already zero'd out buffer. */
SDL_Unsupported();
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,72 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <emscripten.h>
#include "../../SDL_internal.h"
#include "../SDL_syslocale.h"
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{
EM_ASM({
var buf = $0;
var buflen = $1;
var list = undefined;
if (navigator.languages && navigator.languages.length) {
list = navigator.languages;
} else {
var oneOfThese = navigator.userLanguage || navigator.language || navigator.browserLanguage || navigator.systemLanguage;
if (oneOfThese !== undefined) {
list = [ oneOfThese ];
}
}
if (list === undefined) {
return; /* we've got nothing. */
}
var str = ""; /* Can't do list.join() because we need to fit in buflen. */
for (var i = 0; i < list.length; i++) {
var item = list[i];
if ((str.length + item.length + 1) > buflen) {
break; /* don't add, we're out of space. */
}
if (str.length > 0) {
str += ",";
}
str += item;
}
str = str.replace(/-/g, "_");
if (buflen > str.length) {
buflen = str.length; /* clamp to size of string. */
}
for (var i = 0; i < buflen; i++) {
setValue(buf + i, str.charCodeAt(i), "i8"); /* fill in C array. */
}
}, buf, buflen);
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,75 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <LocaleRoster.h>
#include "../../SDL_internal.h"
#include "../SDL_syslocale.h"
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{
BLocaleRoster *roster = BLocaleRoster::Default();
roster->Refresh();
BMessage msg;
if (roster->GetPreferredLanguages(&msg) != B_OK) {
SDL_SetError("BLocaleRoster couldn't get preferred languages");
return;
}
const char *key = "language";
type_code typ = B_ANY_TYPE;
int32 numlangs = 0;
if ((msg.GetInfo(key, &typ, &numlangs) != B_OK) || (typ != B_STRING_TYPE)) {
SDL_SetError("BLocaleRoster message was wrong");
return;
}
for (int32 i = 0; i < numlangs; i++) {
const char *str = NULL;
if (msg.FindString(key, i, &str) != B_OK) {
continue;
}
const size_t len = SDL_strlen(str);
if (buflen <= len) {
break; // can't fit it, we're done.
}
SDL_strlcpy(buf, str, buflen);
buf += len;
buflen -= len;
if (i < (numlangs - 1)) {
if (buflen <= 1) {
break; // out of room, stop looking.
}
buf[0] = ','; // add a comma between entries.
buf[1] = '\0';
buf++;
buflen--;
}
}
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,76 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#include "../SDL_syslocale.h"
#import <Foundation/Foundation.h>
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{ @autoreleasepool {
NSArray *languages = NSLocale.preferredLanguages;
size_t numlangs = 0;
size_t i;
numlangs = (size_t) [languages count];
for (i = 0; i < numlangs; i++) {
NSString *nsstr = [languages objectAtIndex:i];
size_t len;
char *ptr;
if (nsstr == nil) {
break;
}
[nsstr getCString:buf maxLength:buflen encoding:NSASCIIStringEncoding];
len = SDL_strlen(buf);
// convert '-' to '_'...
// These are always full lang-COUNTRY, so we search from the back,
// so things like zh-Hant-CN find the right '-' to convert.
if ((ptr = SDL_strrchr(buf, '-')) != NULL) {
*ptr = '_';
}
if (buflen <= len) {
*buf = '\0'; // drop this one and stop, we can't fit anymore.
break;
}
buf += len;
buflen -= len;
if (i < (numlangs - 1)) {
if (buflen <= 1) {
break; // out of room, stop looking.
}
buf[0] = ','; // add a comma between entries.
buf[1] = '\0';
buf++;
buflen--;
}
}
}}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,110 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#include "../SDL_syslocale.h"
#include "SDL_assert.h"
static void
normalize_locale_str(char *dst, char *str, size_t buflen)
{
char *ptr;
ptr = SDL_strchr(str, '.'); /* chop off encoding if specified. */
if (ptr != NULL) {
*ptr = '\0';
}
ptr = SDL_strchr(str, '@'); /* chop off extra bits if specified. */
if (ptr != NULL) {
*ptr = '\0';
}
/* The "C" locale isn't useful for our needs, ignore it if you see it. */
if ((str[0] == 'C') && (str[1] == '\0')) {
return;
}
if (*str) {
if (*dst) {
SDL_strlcat(dst, ",", buflen); /* SDL has these split by commas */
}
SDL_strlcat(dst, str, buflen);
}
}
static void
normalize_locales(char *dst, char *src, size_t buflen)
{
char *ptr;
/* entries are separated by colons */
while ((ptr = SDL_strchr(src, ':')) != NULL) {
*ptr = '\0';
normalize_locale_str(dst, src, buflen);
src = ptr + 1;
}
normalize_locale_str(dst, src, buflen);
}
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{
/* !!! FIXME: should we be using setlocale()? Or some D-Bus thing? */
SDL_bool isstack;
const char *envr;
char *tmp;
SDL_assert(buflen > 0);
tmp = SDL_small_alloc(char, buflen, &isstack);
if (!tmp) {
SDL_OutOfMemory();
return;
}
*tmp = '\0';
/* LANG is the primary locale (maybe) */
envr = SDL_getenv("LANG");
if (envr) {
SDL_strlcpy(tmp, envr, buflen);
}
/* fallback languages */
envr = SDL_getenv("LANGUAGE");
if (envr) {
if (*tmp) {
SDL_strlcat(tmp, ":", buflen);
}
SDL_strlcat(tmp, envr, buflen);
}
if (*tmp == '\0') {
SDL_SetError("LANG environment variable isn't set");
} else {
normalize_locales(buf, tmp, buflen);
}
SDL_small_free(tmp, isstack);
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,119 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#include "../../core/windows/SDL_windows.h"
#include "../SDL_syslocale.h"
#include "SDL_assert.h"
typedef BOOL (WINAPI *pfnGetUserPreferredUILanguages)(DWORD,PULONG,PZZWSTR,PULONG);
#ifndef MUI_LANGUAGE_NAME
#define MUI_LANGUAGE_NAME 0x8
#endif
static pfnGetUserPreferredUILanguages pGetUserPreferredUILanguages = NULL;
static HMODULE kernel32 = 0;
/* this is the fallback for WinXP...one language, not a list. */
static void
SDL_SYS_GetPreferredLocales_winxp(char *buf, size_t buflen)
{
const char **retval = NULL;
char lang[16];
char country[16];
const int langrc = GetLocaleInfoA(LOCALE_USER_DEFAULT,
LOCALE_SISO639LANGNAME,
lang, sizeof (lang));
const int ctryrc = GetLocaleInfoA(LOCALE_USER_DEFAULT,
LOCALE_SISO3166CTRYNAME,
country, sizeof (country));
/* Win95 systems will fail, because they don't have LOCALE_SISO*NAME ... */
if (langrc == 0) {
SDL_SetError("Couldn't obtain language info");
} else {
SDL_snprintf(buf, buflen, "%s%s%s", lang, ctryrc ? "_" : "", ctryrc ? country : "");
}
}
/* this works on Windows Vista and later. */
static void
SDL_SYS_GetPreferredLocales_vista(char *buf, size_t buflen)
{
ULONG numlangs = 0;
WCHAR *wbuf = NULL;
ULONG wbuflen = 0;
SDL_bool isstack;
SDL_assert(pGetUserPreferredUILanguages != NULL);
pGetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numlangs, NULL, &wbuflen);
wbuf = SDL_small_alloc(WCHAR, wbuflen, &isstack);
if (!wbuf) {
SDL_OutOfMemory();
return;
}
if (!pGetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numlangs, wbuf, &wbuflen)) {
SDL_SYS_GetPreferredLocales_winxp(buf, buflen); /* oh well, try the fallback. */
} else {
const ULONG endidx = SDL_min(buflen, wbuflen - 1);
ULONG str_start = 0;
ULONG i;
for (i = 0; i < endidx; i++) {
const char ch = (char) wbuf[i]; /* these should all be low-ASCII, safe to cast */
if (ch == '\0') {
buf[i] = ','; /* change null separators to commas */
str_start = i;
} else if (ch == '-') {
buf[i] = '_'; /* change '-' to '_' */
} else {
buf[i] = ch; /* copy through as-is. */
}
}
buf[str_start] = '\0'; /* terminate string, chop off final ',' */
}
SDL_small_free(wbuf, isstack);
}
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{
if (!kernel32) {
kernel32 = LoadLibraryW(L"kernel32.dll");
if (kernel32) {
pGetUserPreferredUILanguages = (pfnGetUserPreferredUILanguages) GetProcAddress(kernel32, "GetUserPreferredUILanguages");
}
}
if (pGetUserPreferredUILanguages == NULL) {
SDL_SYS_GetPreferredLocales_winxp(buf, buflen); /* this is always available */
} else {
SDL_SYS_GetPreferredLocales_vista(buf, buflen); /* available on Vista and later. */
}
}
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -0,0 +1,58 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <Windows.h>
#include "../../SDL_internal.h"
#include "../SDL_syslocale.h"
/*using namespace Windows::Graphics::Display;*/
#include <wchar.h>
void
SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{
WCHAR wbuffer[128] = L"";
int ret = 0;
/* !!! FIXME: do we not have GetUserPreferredUILanguages on WinPhone or UWP? */
# if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
ret = GetLocaleInfoEx(LOCALE_NAME_SYSTEM_DEFAULT, LOCALE_SNAME, wbuffer, SDL_arraylen(wbuffer));
# else
ret = GetSystemDefaultLocaleName(wbuffer, SDL_arraysize(wbuffer));
# endif
if (ret > 0)
{
/* Need to convert LPWSTR to LPSTR, that is wide char to char. */
int i;
if (ret >= (buflen - 1) ) {
ret = (int) (buflen - 1);
}
for (i = 0; i < ret; i++) {
buf[i] = (char) wbuffer[i]; /* assume this was ASCII anyhow. */
}
}
}
/* vi: set ts=4 sw=4 expandtab: */