Fixed a problem with Makefile.mingw where make wasn't rebuilding targets
[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 <stdio.h>
20 #include "i18n.h"
21
22 static const char *languages[][2] = {
23     { "de", N_("German")  },
24     { "en", N_("English") },
25     { "es", N_("Spanish") },
26     { "fr", N_("French")  },
27     { "lv", N_("Latvian") },
28     { "nn", N_("Norwegian Nynorsk") },
29 };
30
31 /*---------------------------------------------------------------------------*/
32
33 void language_init(const char *domain, const char *locale_dir)
34 {
35     setlocale(LC_ALL, "");
36     bindtextdomain(domain, locale_dir);
37     textdomain(domain);
38
39     bind_textdomain_codeset(domain, "UTF-8");
40 }
41
42 void language_set(int l)
43 {
44     if (l == 0)
45         putenv("LANGUAGE");
46     else
47     {
48         static char e[25];
49
50         strcpy(e, "LANGUAGE=");
51         strncat(e, languages[l - 1][0], 25 - 9);
52
53         putenv(e);
54     }
55
56     /* Force to update gettext. */
57     setlocale(LC_ALL, "");
58 }
59
60 int language_count(void)
61 {
62     return sizeof (languages) / sizeof (languages[0]);
63 }
64
65 int language_from_code(const char *code)
66 {
67     int i;
68
69     for (i = 0; i < language_count(); i++)
70         if (strcmp(languages[i][0], code) == 0)
71             return i + 1;
72
73     return 0;
74 }
75
76 const char *language_name(int id)
77 {
78     return id == 0 ? N_("System Default") : languages[id - 1][1];
79 }
80
81
82 const char *language_code(int id)
83 {
84     return id > 0  ? languages[id - 1][0] : "";
85 }
86
87 /*---------------------------------------------------------------------------*/
88
89 const char *sgettext(const char *msgid)
90 {
91     const char *msgval = gettext(msgid);
92
93     if (msgval == msgid)
94     {
95         if ((msgval = strrchr(msgid, '^')))
96             msgval++;
97         else
98             msgval = msgid;
99     }
100     return msgval;
101 }
102
103 /*---------------------------------------------------------------------------*/