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