Debian Codegraph @

common/languages.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 #include "global.h"
15
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 /**
21 * One message.
22 */
23 typedef struct i18n_message {
24 sstring code; /**< Message code, usually the English version. */
25 sstring message; /**< Message to display. */
26 } i18n_message;
27
28 /**
29 * One available language.
30 */
31 typedef struct i18n_file {
32 sstring code; /**< Language code, "message." extension. */
33 sstring name; /**< Language's name, in its native version. */
34 int count; /**< How many items in messages. */
35 struct i18n_message *messages; /**< Available messages for this language. */
36 } i18n_file;
37
38 /** Number of defined languages. */
39 static int i18n_count = 0;
40 /** Defined languages. */
41 static struct i18n_file *i18n_files = NULL;
42 /** Index of "English" in the i18nfiles array. */
43 static int i18n_default = -1;
44
45 static int i18n_message_compare_code(const i18n_message *a, const i18n_message *b) {
46 return strcmp(a->code, b->code);
47 }
48
49 /**
50 * Returns the i18n language index associated with the given object.
51 * This only has a meaning for players.
52 * @param op The player object to get the language of
53 * @return The language numerical code. If none is associated, get_language returns 0
54 */
55 int get_language(object *op) {
56 if (!op->contr)
57 return 0;
58 if (op->contr->language < 0 || op->contr->language >= i18n_count)
59 return 0;
60 return op->contr->language;
61 }
62
63 /**
64 * Translate a message in the appropriate language.
65 * @param who who to translate for.
66 * @param code string to translate, usually the English version.
67 * @return translated message, or code if not found or who's language is invalid.
68 */
69 const char *i18n(const object *who, const char *code) {
70 i18n_message search, *found;
71
72 if (!who || !who->contr)
73 return code;
74
75 if (who->contr->language < 0 || who->contr->language >= i18n_count)
76 return code;
77
78 search.code = add_string(code);
79
80 found = bsearch(&search, i18n_files[who->contr->language].messages, i18n_files[who->contr->language].count, sizeof(i18n_message), (int (*)(const void *, const void *))i18n_message_compare_code);
81
82 free_string(search.code);
83
84 if (found)
85 return found->message;
86
87 return code;
88 }
89
90 /**
91 * Attempt to find the identifier of a language from its code.
92 * @param code language code.
93 * @return index, -1 if not found.
94 */
95 int i18n_find_language_by_code(const char *code) {
96 int index;
97 for (index = 0; index < i18n_count; index++) {
98 if (strcmp(code, i18n_files[index].code) == 0)
99 return index;
100 }
101
102 return -1;
103 }
104
105 /**
106 * Find the identifier of a language from its code.
107 * @param code language's code.
108 * @return language's code, or the default language if code is invalid.
109 */
110 int i18n_get_language_by_code(const char *code) {
111 int try = i18n_find_language_by_code(code);
112 if (try != -1)
113 return try;
114 return i18n_default;
115 }
116
117 /**
118 * Return the code of a specified language.
119 * @param language identifier of the language.
120 * @return language's code, or default language's code if identifier is invalid.
121 */
122 sstring i18n_get_language_code(int language) {
123 if (language < 0 || language >= i18n_count)
124 return i18n_files[i18n_default].code;
125 return i18n_files[language].code;
126 }
127
128 /**
129 * List all languages for who.
130 * @param who who to display languages for.
131 */
132 void i18n_list_languages(object *who) {
133 int index;
134 for (index = 0; index < i18n_count; index++) {
135 draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE,
136 "[fixed]%s: %s",
137 i18n_files[index].code,
138 i18n_files[index].name
139 );
140 }
141 }
142
143 /**
144 * Replaces '\n' by a newline char.
145 *
146 * Since we are replacing 2 chars by 1, no overflow should happen.
147 *
148 * @param line
149 * text to replace into.
150 */
151 static void convert_newline(char *line) {
152 char *next;
153 char buf[MAX_BUF];
154
155 while ((next = strstr(line, "\\n")) != NULL) {
156 *next = '\n';
157 *(next+1) = '\0';
158 snprintf(buf, MAX_BUF, "%s%s", line, next+2);
159 strcpy(line, buf);
160 }
161 }
162
163 /**
164 * Initializes the i18n subsystem.
165 * Will load all found strings.
166 * If there is an error, calls fatal().
167 */
168 void i18n_init(void) {
169 char dirname[MAX_BUF], filename[MAX_BUF*2], line[HUGE_BUF];
170 FILE *fp;
171 char *token;
172 DIR *dir;
173 struct dirent *file;
174 i18n_message code, *found;
175
176 snprintf(dirname, sizeof(dirname), "%s/i18n/", settings.datadir);
177
178 dir = opendir(dirname);
179 if (dir == NULL) {
180 LOG(llevError, "i18n: couldn't open %s\n", dirname);
181 fatal(SEE_LAST_ERROR);
182 }
183
184 code.code = add_string("LN");
185
186 while ((file = readdir(dir)) != NULL) {
187 if (strncmp(file->d_name, "messages.", 9) != 0)
188 continue;
189
190 snprintf(filename, sizeof(filename), "%s%s", dirname, file->d_name);
191 if ((fp = fopen(filename, "r")) == NULL) {
192 LOG(llevError, "i18n: couldn't open %s\n",
193 filename, strerror(errno));
194 fatal(SEE_LAST_ERROR);
195 }
196
197 i18n_files = realloc(i18n_files, (i18n_count + 1) * sizeof(i18n_file));
198 i18n_files[i18n_count].code = add_string(file->d_name + 9);
199 i18n_files[i18n_count].count = 0;
200 i18n_files[i18n_count].messages = NULL;
201
202 while (fgets(line, MAX_BUF, fp)) {
203 if (line[0] != '#') {
204 line[strlen(line)-1] = '\0'; /* erase the final newline that messes things. */
205
206 i18n_files[i18n_count].messages = realloc(i18n_files[i18n_count].messages, (i18n_files[i18n_count].count + 1) * sizeof(i18n_message));
207
208 token = strtok(line, "|");
209 convert_newline(token);
210 i18n_files[i18n_count].messages[i18n_files[i18n_count].count].code = add_string(token);
211 token = strtok(NULL, "|");
212 if (token != NULL) {
213 convert_newline(token);
214 i18n_files[i18n_count].messages[i18n_files[i18n_count].count].message = add_string(token);
215 } else {
216 i18n_files[i18n_count].messages[i18n_files[i18n_count].count].message = add_refcount(i18n_files[i18n_count].messages[i18n_files[i18n_count].count].code);
217 }
218 i18n_files[i18n_count].count++;
219 }
220 }
221 fclose(fp);
222
223 qsort(i18n_files[i18n_count].messages, i18n_files[i18n_count].count, sizeof(i18n_message), (int (*)(const void *, const void *))i18n_message_compare_code);
224 found = bsearch(&code, i18n_files[i18n_count].messages, i18n_files[i18n_count].count, sizeof(i18n_message), (int (*)(const void *, const void *))i18n_message_compare_code);
225 if (found == NULL) {
226 LOG(llevError, "i18n: no language set in %s\n", filename);
227 fatal(SEE_LAST_ERROR);
228 }
229
230 i18n_files[i18n_count].name = found->message;
231 LOG(llevDebug, "i18n: %d strings for %s\n",
232 i18n_files[i18n_count].count, found->message);
233
234 if (strcmp(i18n_files[i18n_count].code, "en") == 0)
235 i18n_default = i18n_count;
236
237 i18n_count++;
238 }
239 closedir(dir);
240
241 free_string(code.code);
242
243 if (i18n_default == -1) {
244 LOG(llevError, "i18n: couldn't find default language (en)\n");
245 fatal(SEE_LAST_ERROR);
246 }
247 }
248
249 /**
250 * Clears all i18n-related data.
251 */
252 void i18n_free(void) {
253 int file, message;
254
255 for (file = 0; file < i18n_count; file++) {
256 free_string(i18n_files[file].code); /* name is a copy of a message */
257 for (message = 0; message < i18n_files[file].count; message++) {
258 free_string(i18n_files[file].messages[message].code);
259 free_string(i18n_files[file].messages[message].message);
260 }
261 free(i18n_files[file].messages);
262 }
263 free(i18n_files);
264 i18n_files = NULL;
265 i18n_count = 0;
266 }