summaryrefslogtreecommitdiff
path: root/src/lib/evil/evil_locale.c
blob: a4d056ad9301769a246475348e03f592bcfc18e7 (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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <string.h>
#include <locale.h>
#include <errno.h>

#include "evil_private.h"

/*
 * LOCALE_SISO639LANGNAME and LOCALE_SISO3166CTRYNAME need at least a buffer
 * of 9 char each (including NULL char). So we need 2*8 + the trailing NULL
 * char + '_', so 18 char.
 */
static char _evil_locale_buf[18];

char *evil_setlocale(int category, const char *locale)
{
   char buf[9];
   int l1;
   int l2;

   if (category != LC_MESSAGES)
     return setlocale(category, locale);

   if (locale != NULL)
     {
        errno = EINVAL;
        return NULL;
     }

   l1 = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO639LANGNAME,
                      buf, sizeof(buf));
   if (!l1) return NULL;

   memcpy(_evil_locale_buf, buf, l1 - 1);
   _evil_locale_buf[l1 - 1] = '_';

   l2 = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO3166CTRYNAME,
                      buf, sizeof(buf));
   if (!l2) return NULL;

   memcpy(_evil_locale_buf + l1, buf, l2);

   return _evil_locale_buf;
}