src/locale/windows/SDL_syslocale.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 21 May 2020 03:48:56 -0400
changeset 13843 cbd001bb0b89
parent 13797 55ec5ae4aa0b
child 13845 210fb62f0c96
permissions -rw-r--r--
locale: Fixed compiler warning on Visual Studio.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 #include "../../SDL_internal.h"
    23 #include "../../core/windows/SDL_windows.h"
    24 #include "../SDL_syslocale.h"
    25 #include "SDL_assert.h"
    26 
    27 typedef BOOL (WINAPI *pfnGetUserPreferredUILanguages)(DWORD,PULONG,PZZWSTR,PULONG);
    28 #ifndef MUI_LANGUAGE_NAME
    29 #define MUI_LANGUAGE_NAME 0x8 
    30 #endif
    31 
    32 static pfnGetUserPreferredUILanguages pGetUserPreferredUILanguages = NULL;
    33 static HMODULE kernel32 = 0;
    34 
    35 
    36 /* this is the fallback for WinXP...one language, not a list. */
    37 static void
    38 SDL_SYS_GetPreferredLocales_winxp(char *buf, size_t buflen)
    39 {
    40     const char **retval = NULL;
    41     char lang[16];
    42     char country[16];
    43 
    44 	const int langrc = GetLocaleInfoA(LOCALE_USER_DEFAULT,
    45                                       LOCALE_SISO639LANGNAME,
    46                                       lang, sizeof (lang));
    47 
    48 	const int ctryrc =  GetLocaleInfoA(LOCALE_USER_DEFAULT,
    49                                        LOCALE_SISO3166CTRYNAME,
    50                                        country, sizeof (country));
    51 
    52     /* Win95 systems will fail, because they don't have LOCALE_SISO*NAME ... */
    53     if (langrc == 0) {
    54         SDL_SetError("Couldn't obtain language info");
    55     } else {
    56         SDL_snprintf(buf, buflen, "%s%s%s", lang, ctryrc ? "_" : "", ctryrc ? country : "");
    57     }
    58 }
    59 
    60 /* this works on Windows Vista and later. */
    61 static void
    62 SDL_SYS_GetPreferredLocales_vista(char *buf, size_t buflen)
    63 {
    64     ULONG numlangs = 0;
    65     WCHAR *wbuf = NULL;
    66     ULONG wbuflen = 0;
    67     SDL_bool isstack;
    68 
    69     SDL_assert(pGetUserPreferredUILanguages != NULL);
    70     pGetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numlangs, NULL, &wbuflen);
    71 
    72     wbuf = SDL_small_alloc(WCHAR, wbuflen, &isstack);
    73     if (!wbuf) {
    74         SDL_OutOfMemory();
    75         return;
    76     }
    77 
    78     if (!pGetUserPreferredUILanguages(MUI_LANGUAGE_NAME, &numlangs, wbuf, &wbuflen)) {
    79         SDL_SYS_GetPreferredLocales_winxp(buf, buflen);  /* oh well, try the fallback. */
    80     } else {
    81         const ULONG endidx = (ULONG) SDL_min(buflen, wbuflen - 1);
    82         ULONG str_start = 0;
    83         ULONG i;
    84         for (i = 0; i < endidx; i++) {
    85             const char ch = (char) wbuf[i];  /* these should all be low-ASCII, safe to cast */
    86             if (ch == '\0') {
    87                 buf[i] = ',';  /* change null separators to commas */
    88                 str_start = i;
    89             } else if (ch == '-') {
    90                 buf[i] = '_';  /* change '-' to '_' */
    91             } else {
    92                 buf[i] = ch;   /* copy through as-is. */
    93             }
    94         }
    95         buf[str_start] = '\0';  /* terminate string, chop off final ',' */
    96     }
    97 
    98     SDL_small_free(wbuf, isstack);
    99 }
   100 
   101 void
   102 SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
   103 {
   104     if (!kernel32) {
   105         kernel32 = LoadLibraryW(L"kernel32.dll");
   106         if (kernel32) {
   107             pGetUserPreferredUILanguages = (pfnGetUserPreferredUILanguages) GetProcAddress(kernel32, "GetUserPreferredUILanguages");
   108         }
   109     }
   110 
   111     if (pGetUserPreferredUILanguages == NULL) {
   112         SDL_SYS_GetPreferredLocales_winxp(buf, buflen);  /* this is always available */
   113     } else {
   114         SDL_SYS_GetPreferredLocales_vista(buf, buflen);  /* available on Vista and later. */
   115     }
   116 }
   117 
   118 /* vi: set ts=4 sw=4 expandtab: */
   119