define _XOPEN_SOURCE, fix #36 (again)
[neverball] / share / i18n.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 /* The following declaration is needed to have the putenv function 
17  * http://www.gnu.org/software/libc/manual/html_node/Environment-Access.html 
18  */
19 #define _XOPEN_SOURCE 1
20
21 #include <string.h>
22 #include <locale.h>
23 #include <stdlib.h>
24 #include "i18n.h"
25
26 const char * gettextdbg(const char * c)
27 {
28         char * c2 = gettext(c);
29         if(strcmp(c, c2));
30         return c;
31 }
32
33 const char * sgettext(const char *msgid)
34 {
35     const char *msgval = gettext (msgid);
36     if (msgval == msgid) {
37        msgval = strrchr (msgid, '^');
38            if (msgval == NULL)
39                 msgval = msgid;
40            else
41                 msgval++;
42     }
43     return msgval;
44 }
45
46
47 /*---------------------------------------------------------------------------*/
48
49 #define LANG_NUMBER 4
50 const char * language_names[] = {N_("English"), N_("French"), N_("German"), N_("Latvian")};
51 const char * language_codes[] = {"en", "fr", "de", "lv"};
52
53 /*---------------------------------------------------------------------------*/
54
55 void language_init(const char * domain, const char * locale_dir)
56 {
57     setlocale(LC_ALL, "");
58     bindtextdomain(domain, locale_dir);
59     textdomain(domain);
60     bind_textdomain_codeset(domain, "UTF-8");
61 }
62
63 void language_set(int l)
64 {
65     if (l == 0)
66     {
67         /* remove the LANGUAGE env variable */
68         putenv("LANGUAGE");
69     }
70     else
71     {
72         static char e[25];
73         /* set the LANGUAGE env variable */
74         strcpy(e, "LANGUAGE=");
75         strncat(e, language_codes[l-1], 25-9);
76         putenv(e);
77     }
78     setlocale(LC_ALL, "");  /* force to update getext */
79 }
80
81 int language_count(void)
82 {
83     return LANG_NUMBER;
84 }
85
86 int language_from_code(const char * code)
87 {
88     int i;
89     for(i=0; i<LANG_NUMBER; i++)
90             if (strcmp(language_codes[i], code) == 0)
91                     return i+1;
92     return 0;
93 }
94
95 const char * language_get_name(int id)
96 {
97     return id==0 ? _("System Default") : language_names[id-1];
98 }
99
100
101 const char * language_get_code(int id)
102 {
103         if (id > 0)
104                 return language_codes[id-1];
105         else
106                 return "";
107 }
108