1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright (C) 1997-2025 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 "../SDL_syslocale.h" |
24 | |
25 | static void normalize_locale_str(char *dst, char *str, size_t buflen) |
26 | { |
27 | char *ptr; |
28 | |
29 | ptr = SDL_strchr(str, '.'); // chop off encoding if specified. |
30 | if (ptr) { |
31 | *ptr = '\0'; |
32 | } |
33 | |
34 | ptr = SDL_strchr(str, '@'); // chop off extra bits if specified. |
35 | if (ptr) { |
36 | *ptr = '\0'; |
37 | } |
38 | |
39 | // The "C" locale isn't useful for our needs, ignore it if you see it. |
40 | if ((str[0] == 'C') && (str[1] == '\0')) { |
41 | return; |
42 | } |
43 | |
44 | if (*str) { |
45 | if (*dst) { |
46 | SDL_strlcat(dst, "," , buflen); // SDL has these split by commas |
47 | } |
48 | SDL_strlcat(dst, str, buflen); |
49 | } |
50 | } |
51 | |
52 | static void normalize_locales(char *dst, char *src, size_t buflen) |
53 | { |
54 | char *ptr; |
55 | |
56 | // entries are separated by colons |
57 | while ((ptr = SDL_strchr(src, ':')) != NULL) { |
58 | *ptr = '\0'; |
59 | normalize_locale_str(dst, src, buflen); |
60 | src = ptr + 1; |
61 | } |
62 | normalize_locale_str(dst, src, buflen); |
63 | } |
64 | |
65 | bool SDL_SYS_GetPreferredLocales(char *buf, size_t buflen) |
66 | { |
67 | // !!! FIXME: should we be using setlocale()? Or some D-Bus thing? |
68 | bool isstack; |
69 | const char *envr; |
70 | char *tmp; |
71 | |
72 | SDL_assert(buflen > 0); |
73 | tmp = SDL_small_alloc(char, buflen, &isstack); |
74 | if (!tmp) { |
75 | return false; |
76 | } |
77 | |
78 | *tmp = '\0'; |
79 | |
80 | // LANG is the primary locale (maybe) |
81 | envr = SDL_getenv("LANG" ); |
82 | if (envr) { |
83 | SDL_strlcpy(tmp, envr, buflen); |
84 | } |
85 | |
86 | // fallback languages |
87 | envr = SDL_getenv("LANGUAGE" ); |
88 | if (envr) { |
89 | if (*tmp) { |
90 | SDL_strlcat(tmp, ":" , buflen); |
91 | } |
92 | SDL_strlcat(tmp, envr, buflen); |
93 | } |
94 | |
95 | if (*tmp == '\0') { |
96 | SDL_SetError("LANG environment variable isn't set" ); |
97 | } else { |
98 | normalize_locales(buf, tmp, buflen); |
99 | } |
100 | |
101 | SDL_small_free(tmp, isstack); |
102 | return true; |
103 | } |
104 | |