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