1 /**
2 * Translate game texts
3
4 * Copyright (C) 2008, 2009 Sylvain Beucler
5
6 * This file is part of GNU FreeDink
7
8 * GNU FreeDink is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 3 of the
11 * License, or (at your option) any later version.
12
13 * GNU FreeDink is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <locale.h>
28 #include "gettext.h"
29 #include <alloca.h>
30 #include <ctype.h>
31
32 #include "paths.h"
33 #include "str_util.h"
34
35 /**
36 * Copy a translation for 'latin1_source' in 'utf8_dest' or, if there
37 * isn't, convert 'latin1_source' to UTF-8. 'utf8_dest' will always be
38 * NULL-terminated, and won't be longer than max_size bytes (including
39 * trailing '\0').
40 *
41 * The reason the text is always converted to UTF-8 is because Dink
42 * (freeware version) was used in European countries where some
43 * non-ASCII characters were used (e.g. 0xB4 or "single acute accent"
44 * which is present in Finnish keyboards and used instead of the more
45 * common single quote "'" - check the Milderrr! series for instance).
46 */
i18n_translate(char * scriptname,unsigned int line,char * latin1_source)47 char* i18n_translate(char* scriptname, unsigned int line, char* latin1_source)
48 {
49 /* Don't translate the empty string, which has a special meaning for
50 gettext */
51 if (strlen(latin1_source) == 0)
52 return strdup("");
53
54 const char* translation = "";
55
56 /* Try with a context */
57 char* context = alloca(strlen(scriptname) + 1 + strlen("4294967295") + 1);
58 sprintf(context, "%s:%d", scriptname, line);
59 char *pc = context;
60 while (*pc) { *pc = tolower(*pc); pc++; }
61 translation = dpgettext_expr(paths_getdmodname(), context, latin1_source);
62 if (translation != latin1_source)
63 {
64 /* Copy the translation */
65 return strdup(translation);
66 }
67
68 /* Try without context */
69 translation = dgettext(paths_getdmodname(), latin1_source);
70 if (translation != latin1_source)
71 {
72 /* Copy the translation */
73 return strdup(translation);
74 }
75
76 /* No translation available */
77 /* Let's manually convert from Latin-1 to UTF-8, so that
78 'TTF_RenderUTF8_Solid' can parse it correctly. */
79 return latin1_to_utf8(latin1_source);
80 }
81