Add Spanish translation and try not to mess anything up while doing
[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 *sgettext(const char *msgid)
28 {
29     const char *msgval = gettext(msgid);
30
31     if (msgval == msgid)
32     {
33         if ((msgval = strrchr(msgid, '^')))
34             msgval++;
35         else
36             msgval = msgid;
37     }
38     return msgval;
39 }
40
41 /*---------------------------------------------------------------------------*/
42
43 #define LANG_NUMBER 5
44
45 const char *language_names[] = {
46     N_("English"),
47     N_("French"),
48     N_("German"),
49     N_("Latvian"),
50     N_("Spanish"),
51 };
52 const char *language_codes[] = {"en", "fr", "de", "lv", "es"};
53
54 /*---------------------------------------------------------------------------*/
55
56 void language_init(const char *domain, const char *locale_dir)
57 {
58     setlocale(LC_ALL, "");
59     bindtextdomain(domain, locale_dir);
60     textdomain(domain);
61     bind_textdomain_codeset(domain, "UTF-8");
62 }
63
64 void language_set(int l)
65 {
66     if (l == 0)
67     {
68         /* remove the LANGUAGE env variable */
69         putenv("LANGUAGE");
70     }
71     else
72     {
73         static char e[25];
74
75         /* set the LANGUAGE env variable */
76         strcpy(e, "LANGUAGE=");
77         strncat(e, language_codes[l - 1], 25 - 9);
78         putenv(e);
79     }
80     
81     /* Force to update gettext. */
82     setlocale(LC_ALL, "");
83 }
84
85 int language_count(void)
86 {
87     return LANG_NUMBER;
88 }
89
90 int language_from_code(const char *code)
91 {
92     int i;
93
94     for (i = 0; i < LANG_NUMBER; i++)
95         if (strcmp(language_codes[i], code) == 0)
96             return i + 1;
97
98     return 0;
99 }
100
101 const char *language_get_name(int id)
102 {
103     return id == 0 ? _("System Default") : language_names[id - 1];
104 }
105
106
107 const char *language_get_code(int id)
108 {
109     return id > 0 ? language_codes[id - 1] : "";
110 }
111