summaryrefslogtreecommitdiff
path: root/intl/langprefs.c
blob: de66ad8ccb80a7bedce460b22cf80c082ea0daa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* Determine the user's language preferences.
   Copyright (C) 2004-2007 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU Library General Public License as published
   by the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
   USA.  */

/* Written by Bruno Haible <bruno@clisp.org>.
   Win32 code originally by Michele Cicciotti <hackbunny@reactos.com>.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>

#if HAVE_CFPREFERENCESCOPYAPPVALUE
# include <string.h>
# include <CoreFoundation/CFPreferences.h>
# include <CoreFoundation/CFPropertyList.h>
# include <CoreFoundation/CFArray.h>
# include <CoreFoundation/CFString.h>
extern void _nl_locale_name_canonicalize (char *name);
#endif

#if defined _WIN32 || defined __WIN32__
# define WIN32_NATIVE
#endif

#ifdef WIN32_NATIVE
# define WIN32_LEAN_AND_MEAN
# include <windows.h>

# ifndef MUI_LANGUAGE_NAME
# define MUI_LANGUAGE_NAME 8
# endif
# ifndef STATUS_BUFFER_OVERFLOW
# define STATUS_BUFFER_OVERFLOW 0x80000005
# endif

extern void _nl_locale_name_canonicalize (char *name);
extern const char *_nl_locale_name_from_win32_LANGID (LANGID langid);
extern const char *_nl_locale_name_from_win32_LCID (LCID lcid);

/* Get the preferences list through the MUI APIs. This works on Windows Vista
   and newer.  */
static const char *
_nl_language_preferences_win32_mui (HMODULE kernel32)
{
  /* DWORD GetUserPreferredUILanguages (ULONG dwFlags,
                                        PULONG pulNumLanguages,
                                        PWSTR pwszLanguagesBuffer,
                                        PULONG pcchLanguagesBuffer);  */
  typedef DWORD (WINAPI *GetUserPreferredUILanguages_func) (ULONG, PULONG, PWSTR, PULONG);
  GetUserPreferredUILanguages_func p_GetUserPreferredUILanguages;

  p_GetUserPreferredUILanguages =
   (GetUserPreferredUILanguages_func)
   GetProcAddress (kernel32, "GetUserPreferredUILanguages");
  if (p_GetUserPreferredUILanguages != NULL)
    {
      ULONG num_languages;
      ULONG bufsize;
      DWORD ret;

      bufsize = 0;
      ret = p_GetUserPreferredUILanguages (MUI_LANGUAGE_NAME,
                                           &num_languages,
                                           NULL, &bufsize);
      if (ret == 0
          && GetLastError () == STATUS_BUFFER_OVERFLOW
          && bufsize > 0)
        {
          WCHAR *buffer = (WCHAR *) malloc (bufsize * sizeof (WCHAR));
          if (buffer != NULL)
            {
              ret = p_GetUserPreferredUILanguages (MUI_LANGUAGE_NAME,
                                                   &num_languages,
                                                   buffer, &bufsize);
              if (ret)
                {
                  /* Convert the list from NUL-delimited WCHAR[] Win32 locale
                     names to colon-delimited char[] Unix locale names.
                     We assume that all these locale names are in ASCII,
                     nonempty and contain no colons.  */
                  char *languages =
                    (char *) malloc (bufsize + num_languages * 10 + 1);
                  if (languages != NULL)
                    {
                      const WCHAR *p = buffer;
                      char *q = languages;
                      ULONG i;
                      for (i = 0; i < num_languages; i++)
                        {
                          char *q1;
                          char *q2;

                          q1 = q;
                          if (i > 0)
                            *q++ = ':';
                          q2 = q;
                          for (; *p != (WCHAR)'\0'; p++)
                            {
                              if ((unsigned char) *p != *p || *p == ':')
                                {
                                  /* A non-ASCII character or a colon inside
                                     the Win32 locale name! Punt.  */
                                  q = q1;
                                  break;
                                }
                              *q++ = (unsigned char) *p;
                            }
                          if (q == q1)
                            /* An unexpected Win32 locale name occurred.  */
                            break;
                          *q = '\0';
                          _nl_locale_name_canonicalize (q2);
                          q = q2 + strlen (q2);
                          p++;
                        }
                      *q = '\0';
                      if (q > languages)
                        {
                          free (buffer);
                          return languages;
                        }
                      free (languages);
                    }
                }
              free (buffer);
            }
        }
    }
  return NULL;
}

/* Get a preference.  This works on Windows ME and newer.  */
static const char *
_nl_language_preferences_win32_ME (HMODULE kernel32)
{
  /* LANGID GetUserDefaultUILanguage (void);  */
  typedef LANGID (WINAPI *GetUserDefaultUILanguage_func) (void);
  GetUserDefaultUILanguage_func p_GetUserDefaultUILanguage;

  p_GetUserDefaultUILanguage =
   (GetUserDefaultUILanguage_func)
   GetProcAddress (kernel32, "GetUserDefaultUILanguage");
  if (p_GetUserDefaultUILanguage != NULL)
    return _nl_locale_name_from_win32_LANGID (p_GetUserDefaultUILanguage ());
  return NULL;
}

/* Get a preference.  This works on Windows 95 and newer.  */
static const char *
_nl_language_preferences_win32_95 ()
{
  HKEY desktop_resource_locale_key;

  if (RegOpenKeyExA (HKEY_CURRENT_USER,
                     "Control Panel\\Desktop\\ResourceLocale",
                     0, KEY_QUERY_VALUE, &desktop_resource_locale_key)
      == NO_ERROR)
    {
      DWORD type;
      char data[8 + 1];
      DWORD data_size = sizeof (data);
      DWORD ret;

      ret = RegQueryValueExA (desktop_resource_locale_key, NULL, NULL,
                              &type, data, &data_size);
      RegCloseKey (desktop_resource_locale_key);

      if (ret == NO_ERROR)
        {
          /* We expect a string, at most 8 bytes long, that parses as a
             hexadecimal number.  */
          if (type == REG_SZ
              && data_size <= sizeof (data)
              && (data_size < sizeof (data)
                  || data[sizeof (data) - 1] == '\0'))
            {
              LCID lcid;
              char *endp;
              /* Ensure it's NUL terminated.  */
              if (data_size < sizeof (data))
                data[data_size] = '\0';
              /* Parse it as a hexadecimal number.  */
              lcid = strtoul (data, &endp, 16);
              if (endp > data && *endp == '\0')
                return _nl_locale_name_from_win32_LCID (lcid);
            }
        }
    }
  return NULL;
}

/* Get the system's preference.  This can be used as a fallback.  */
static BOOL CALLBACK
ret_first_language (HMODULE h, LPCSTR type, LPCSTR name, WORD lang, LONG_PTR param)
{
  *(const char **)param = _nl_locale_name_from_win32_LANGID (lang);
  return FALSE;
}
static const char *
_nl_language_preferences_win32_system (HMODULE kernel32)
{
  const char *languages = NULL;
  /* Ignore the warning on mingw here. mingw has a wrong definition of the last
     parameter type of ENUMRESLANGPROC.  */
  EnumResourceLanguages (kernel32, RT_VERSION, MAKEINTRESOURCE (1),
                         ret_first_language, (LONG_PTR)&languages);
  return languages;
}

#endif

/* Determine the user's language preferences, as a colon separated list of
   locale names in XPG syntax
     language[_territory][.codeset][@modifier]
   The result must not be freed; it is statically allocated.
   The LANGUAGE environment variable does not need to be considered; it is
   already taken into account by the caller.  */

const char *
_nl_language_preferences_default (void)
{
#if HAVE_CFPREFERENCESCOPYAPPVALUE /* MacOS X 10.2 or newer */
  {
    /* Cache the preferences list, since CoreFoundation calls are expensive.  */
    static const char *cached_languages;
    static int cache_initialized;

    if (!cache_initialized)
      {
        CFTypeRef preferences =
          CFPreferencesCopyAppValue (CFSTR ("AppleLanguages"),
                                     kCFPreferencesCurrentApplication);
        if (preferences != NULL
            && CFGetTypeID (preferences) == CFArrayGetTypeID ())
          {
            CFArrayRef prefArray = (CFArrayRef)preferences;
            int n = CFArrayGetCount (prefArray);
            char buf[256];
            size_t size = 0;
            int i;

            for (i = 0; i < n; i++)
              {
                CFTypeRef element = CFArrayGetValueAtIndex (prefArray, i);
                if (element != NULL
                    && CFGetTypeID (element) == CFStringGetTypeID ()
                    && CFStringGetCString ((CFStringRef)element,
                                           buf, sizeof (buf),
                                           kCFStringEncodingASCII))
                  {
                    _nl_locale_name_canonicalize (buf);
                    size += strlen (buf) + 1;
                    /* Most GNU programs use msgids in English and don't ship
                       an en.mo message catalog.  Therefore when we see "en"
                       in the preferences list, arrange for gettext() to
                       return the msgid, and ignore all further elements of
                       the preferences list.  */
                    if (strcmp (buf, "en") == 0)
                      break;
                  }
                else
                  break;
              }
            if (size > 0)
              {
                char *languages = (char *) malloc (size);

                if (languages != NULL)
                  {
                    char *p = languages;

                    for (i = 0; i < n; i++)
                      {
                        CFTypeRef element =
                          CFArrayGetValueAtIndex (prefArray, i);
                        if (element != NULL
                            && CFGetTypeID (element) == CFStringGetTypeID ()
                            && CFStringGetCString ((CFStringRef)element,
                                                   buf, sizeof (buf),
                                                   kCFStringEncodingASCII))
                          {
                            _nl_locale_name_canonicalize (buf);
                            strcpy (p, buf);
                            p += strlen (buf);
                            *p++ = ':';
                            if (strcmp (buf, "en") == 0)
                              break;
                          }
                        else
                          break;
                      }
                    *--p = '\0';

                    cached_languages = languages;
                  }
              }
          }
        cache_initialized = 1;
      }
    if (cached_languages != NULL)
      return cached_languages;
  }
#endif

#ifdef WIN32_NATIVE
  {
    /* Cache the preferences list, since computing it is expensive.  */
    static const char *cached_languages;
    static int cache_initialized;

    /* Activate the new code only when the GETTEXT_MUI environment variable is
       set, for the time being, since the new code is not well tested.  */
    if (!cache_initialized && getenv ("GETTEXT_MUI") != NULL)
      {
        const char *languages = NULL;
        HMODULE kernel32 = GetModuleHandle ("kernel32");

        if (kernel32 != NULL)
          languages = _nl_language_preferences_win32_mui (kernel32);

        if (languages == NULL && kernel32 != NULL)
          languages = _nl_language_preferences_win32_ME (kernel32);

        if (languages == NULL)
          languages = _nl_language_preferences_win32_95 ();

        if (languages == NULL && kernel32 != NULL)
          languages = _nl_language_preferences_win32_system (kernel32);

        cached_languages = languages;
        cache_initialized = 1;
      }
    if (cached_languages != NULL)
      return cached_languages;
  }
#endif

  return NULL;
}