Transform default game/locale data paths based on executable name
[neverball] / share / lang.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 <errno.h>
21
22 #include "lang.h"
23 #include "common.h"
24 #include "base_config.h"
25
26 /*---------------------------------------------------------------------------*/
27
28 #define DEFAULT_CODESET "UTF-8"
29
30 /*---------------------------------------------------------------------------*/
31
32 void lang_init(const char *domain, const char *default_dir)
33 {
34 #if ENABLE_NLS
35     char *dir = getenv("NEVERBALL_LOCALE");
36
37     if (!dir)
38         dir = path_resolve(config_exec_path, default_dir);
39
40     errno = 0;
41
42     if (!setlocale(LC_ALL, ""))
43     {
44         fprintf(stderr, "Failed to set LC_ALL to native locale: %s\n",
45                 errno ? strerror(errno) : "Unknown error");
46     }
47
48     /* The C locale is guaranteed (sort of) to be available. */
49
50     setlocale(LC_NUMERIC, "C");
51
52     bindtextdomain(domain, dir);
53     bind_textdomain_codeset(domain, DEFAULT_CODESET);
54     textdomain(domain);
55 #else
56     return;
57 #endif
58 }
59
60 const char *sgettext(const char *msgid)
61 {
62 #if ENABLE_NLS
63     const char *msgval = gettext(msgid);
64 #else
65     const char *msgval = msgid;
66 #endif
67
68     if (msgval == msgid)
69     {
70         if ((msgval = strrchr(msgid, '^')))
71             msgval++;
72         else msgval = msgid;
73     }
74     return msgval;
75 }
76
77 const char *get_local_text(const char *msgid)
78 {
79 #if ENABLE_NLS
80     char *msgstr, *domain = textdomain(NULL);
81
82     bind_textdomain_codeset(domain, "");
83     msgstr = gettext(msgid);
84     bind_textdomain_codeset(domain, DEFAULT_CODESET);
85
86     return msgstr;
87 #else
88     return msgid;
89 #endif
90 }
91
92 /*---------------------------------------------------------------------------*/