Fix redundant glTexEnv calls
[neverball] / share / lang.c
1 /*
2  * Copyright (C) 2006 Jean Privat
3  * Part of the Neverball Project http://icculus.org/neverball/
4  *
5  * NEVERBALL is  free software; you can redistribute  it and/or modify
6  * it under the  terms of the GNU General  Public License as published
7  * by the Free  Software Foundation; either version 2  of the License,
8  * or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
12  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
13  * General Public License for more details.
14  */
15
16 #include <string.h>
17 #include <locale.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <errno.h>
21
22 #include "lang.h"
23 #include "common.h"
24 #include "base_config.h"
25 #include "fs.h"
26
27 /*---------------------------------------------------------------------------*/
28
29 #define DEFAULT_CODESET "UTF-8"
30
31 /*---------------------------------------------------------------------------*/
32
33 void lang_init(const char *domain)
34 {
35 #if ENABLE_NLS
36     char *dir = strdup(getenv("NEVERBALL_LOCALE"));
37
38     if (!dir)
39     {
40         if (path_is_abs(CONFIG_LOCALE))
41             dir = strdup(CONFIG_LOCALE);
42         else
43             dir = concat_string(fs_base_dir(), "/", CONFIG_LOCALE, NULL);
44     }
45
46     errno = 0;
47
48     if (!setlocale(LC_ALL, ""))
49     {
50         fprintf(stderr, "Failed to set LC_ALL to native locale: %s\n",
51                 errno ? strerror(errno) : "Unknown error");
52     }
53
54     /* The C locale is guaranteed (sort of) to be available. */
55
56     setlocale(LC_NUMERIC, "C");
57
58     bindtextdomain(domain, dir);
59     bind_textdomain_codeset(domain, DEFAULT_CODESET);
60     textdomain(domain);
61
62     free(dir);
63 #else
64     return;
65 #endif
66 }
67
68 const char *sgettext(const char *msgid)
69 {
70 #if ENABLE_NLS
71     const char *msgval = gettext(msgid);
72 #else
73     const char *msgval = msgid;
74 #endif
75
76     if (msgval == msgid)
77     {
78         if ((msgval = strrchr(msgid, '^')))
79             msgval++;
80         else msgval = msgid;
81     }
82     return msgval;
83 }
84
85 const char *get_local_text(const char *msgid)
86 {
87 #if ENABLE_NLS
88     char *msgstr, *domain = textdomain(NULL);
89
90     bind_textdomain_codeset(domain, "");
91     msgstr = gettext(msgid);
92     bind_textdomain_codeset(domain, DEFAULT_CODESET);
93
94     return msgstr;
95 #else
96     return msgid;
97 #endif
98 }
99
100 /*---------------------------------------------------------------------------*/