Debian Codegraph @

src/bnetd/i18n.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 HarpyWar ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include "common/setup_before.h"
19
20 #include <algorithm>
21 #include <cctype>
22 #include <cstdio>
23 #include <cerrno>
24 #include <cstring>
25 #include <ctime>
26 #include <cstdlib>
27 #include <sstream>
28 #include <iostream>
29 #include <fstream>
30 #include <vector>
31 #include <utility>
32 #include <map>
33 #include <string.h>
34
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
37 #endif
38
39 #include "compat/strcasecmp.h"
40 #include "compat/pdir.h"
41
42 #include "common/token.h"
43
44 #include "common/list.h"
45 #include "common/eventlog.h"
46 #include "common/xalloc.h"
47 #include "common/xstring.h"
48 #include "common/util.h"
49 #include "common/tag.h"
50 #include "common/pugixml.h"
51 #include "common/format.h"
52
53 #include "account.h"
54 #include "connection.h"
55 #include "message.h"
56 #include "helpfile.h"
57 #include "channel.h"
58 #include "prefs.h"
59 #include "account_wrap.h"
60 #include "command.h"
61 #include "i18n.h"
62 #include "common/setup_after.h"
63
64 namespace pvpgn
65 {
66
67 namespace bnetd
68 {
69 const char * commonfile = "common.xml"; // filename template, actually file name is "common-{lang}.xml"
70
71 /* Array with string translations, each string has array with pair language=translation
72 {
73 original = {
74 { language = translate },
75 ...
76 },
77 ...
78 }
79 */
80 std::map<std::string, std::map<t_gamelang, std::string> > translations = std::map<std::string, std::map<t_gamelang, std::string> >();
81
82 const char * _find_string(char const * text, t_gamelang gamelang);
83
84 // http://msdn.microsoft.com/en-us/goglobal/bb896001.aspx
85 // https://www.microsoft.com/resources/msdn/goglobal/default.mspx
86 std::vector<t_language> languages = {
87 { GAMELANG_ENGLISH_UINT, "English", { "USA" } } /* enUS */
88
89 /* other countries should be loaded from common.xml of other languages */
90 };
91
92
93 int convert_utf8_to_windows1251(const char* utf8, char* windows1251, size_t n);
94 int convert_windows1252_to_utf8(char * windows1252);
95
96
97 extern int i18n_reload(void)
98 {
99 translations.clear();
100 i18n_load();
101
102 return 0;
103 }
104
105 extern int i18n_load(void)
106 {
107 std::string lang_filename;
108 pugi::xml_document doc;
109 std::string original, translate;
110
111
112 std::vector<std::string> files = dir_getfiles(prefs_get_i18ndir(), ".xml", true);
113
114 // load common.xml from each directory
115 for (int i = 0; i < files.size(); ++i)
116 {
117 lang_filename = files[i]; //i18n_filename(prefs_get_localizefile(), languages[i]);
118 if (FILE *f = fopen(lang_filename.c_str(), "r"))
119 {
120 fclose(f);
121
122 if (!doc.load_file(lang_filename.c_str()))
123 {
124 ERROR1("could not parse localization file \"{}\"", lang_filename);
125 continue;
126 }
127 }
128 else
129 {
130 // file not exists, ignore it
131 continue;
132 }
133
134 // root node
135 pugi::xml_node root_node = doc.child("root");
136 pugi::xml_node meta_node = root_node.child("meta");
137
138 // read language tag
139 std::string lang_name = meta_node.child("language").child_value();
140 std::string lang_tag = meta_node.child("language").attribute("tag").as_string();
141 t_gamelang lang_tag_uint = tag_str_to_uint(lang_tag.c_str());
142
143 std::vector<std::string> countries;
144 // read all country codes for the current language
145 pugi::xml_node country_nodes = root_node.child("meta").child("countries");
146 for (pugi::xml_node node = country_nodes.child("country"); node; node = node.next_sibling("country"))
147 {
148 countries.push_back( node.child_value() );
149 }
150
151 // if the tag not found in languages then add it
152 bool found;
153 language_find_by_tag(lang_tag_uint, found);
154 if (!found)
155 {
156 languages.push_back({ lang_tag_uint, xstrdup(lang_name.c_str()), countries });
157 }
158
159
160 // read xml strings to map
161 pugi::xml_node item_nodes = root_node.child("items");
162 for (pugi::xml_node node = item_nodes.child("item"); node; node = node.next_sibling("item"))
163 {
164 original = node.child_value("original");
165 if (original[0] == '\0')
166 continue;
167
168 //std::map<const char *, std::map<t_gamelang, const char *> >::iterator it = translations.find(original);
169 // if not found then init
170 //if (it == translations.end())
171 // translations[original] = std::map<t_gamelang, const char *>();
172
173
174 // check if translate string has a reference to another translation
175 if (pugi::xml_attribute attr = node.child("translate").attribute("refid"))
176 {
177 if (pugi::xml_node n = item_nodes.find_child_by_attribute("id", attr.value()))
178 translate = n.child_value("translate");
179 else
180 {
181 translate = original;
182 //WARN2("could not find translate reference refid=\"{}\", use original string ({})", attr.value(), lang_filename.c_str());
183 }
184 }
185 else
186 {
187 translate = node.child_value("translate");
188 if (translate[0] == '\0')
189 {
190 translate = original;
191 //WARN2("empty translate for \"{}\", use original string ({})", original.c_str(), lang_filename.c_str());
192 }
193 }
194 translations[original][lang_tag_uint] = translate;
195 }
196
197 INFO1("localization file loaded \"{}\"", lang_filename);
198 }
199
200 return 0;
201 }
202
203 extern std::string _localize(t_connection * c, const char * func, const char * fmt, const fmt::ArgList &args)
204 {
205 const char *format = fmt;
206 std::string output(fmt);
207 t_gamelang lang;
208
209 if (!c) {
210 eventlog(eventlog_level_error, __FUNCTION__, "got bad connection");
211 return format;
212 }
213 try
214 {
215 if (lang = conn_get_gamelang_localized(c))
216 if (!(format = _find_string(fmt, lang)))
217 format = fmt; // if not found use original
218
219 output = fmt::format(format, args);
220
221 char tmp[MAX_MESSAGE_LEN];
222 std::snprintf(tmp, sizeof tmp, "%s", output.c_str());
223
224 i18n_convert(c, tmp);
225 output = tmp;
226 }
227 catch (const std::exception& e)
228 {
229 WARN2("Can't format translation string \"{}\" ({})", fmt, e.what());
230 }
231
232 return output;
233 }
234
235
236 /* Find localized text for the given language */
237 const char * _find_string(char const * text, t_gamelang gamelang)
238 {
239 std::map<std::string, std::map<t_gamelang, std::string> >::iterator it = translations.find(text);
240 if (it != translations.end())
241 {
242 if (!it->second[gamelang].empty())
243 return it->second[gamelang].c_str();
244 }
245 return NULL;
246 }
247
248 /* Add a locale tag into filename
249 example: motd.txt -> motd-ruRU.txt */
250 extern std::string i18n_filename(const char * filename, t_tag gamelang)
251 {
252 std::string path = fmt::format("{}/{}/{}", prefs_get_i18ndir(), tag_uint_to_str2(gamelang), filename);
253
254 // if localized file not found
255 struct stat sfile = {};
256 if (stat(path.c_str(), &sfile) < 0)
257 {
258 // use default file
259 path = fmt::format("{}/{}", prefs_get_i18ndir(), filename);
260 }
261
262 return path;
263 }
264
265
266 extern t_gamelang conn_get_gamelang_localized(t_connection * c)
267 {
268 t_gamelang gamelang = conn_get_gamelang(c);
269
270 // force localize by user country
271 if (prefs_get_localize_by_country())
272 if (const char * country = conn_get_country(c))
273 gamelang = gamelang_get_by_country(country);
274
275 // if user set own language
276 if (t_account * a = conn_get_account(c))
277 if (const char * l = account_get_userlang(a))
278 gamelang = tag_str_to_uint(l);
279
280 // FIXME: Russian text displays not correctly directly in game in non-russian Starcraft
281 // but most Russians use English Starcraft.
282 // Instead of next hack we can add "if (conn_get_game(c) {}" in i18n_convert() and return transliterated text
283 //
284 // Return English text in game for Russian
285 if (t_clienttag clienttag = conn_get_clienttag(c))
286 if (gamelang == GAMELANG_RUSSIAN_UINT && conn_get_game(c) &&
287 (clienttag == CLIENTTAG_STARCRAFT_UINT || clienttag == CLIENTTAG_BROODWARS_UINT || clienttag == CLIENTTAG_STARJAPAN_UINT || clienttag == CLIENTTAG_SHAREWARE_UINT ||
288 clienttag == CLIENTTAG_DIABLORTL_UINT || clienttag == CLIENTTAG_DIABLOSHR_UINT || clienttag == CLIENTTAG_WARCIIBNE_UINT))
289 {
290 gamelang = GAMELANG_ENGLISH_UINT;
291 }
292
293
294 return gamelang;
295 }
296
297
298 /* Set custom user language (command) */
299 extern int handle_language_command(t_connection * c, char const *text)
300 {
301 // split command args
302 std::vector<std::string> args = split_command(text, 3);
303 if (args[1].empty())
304 {
305 // display command help
306 describe_command(c, args[0].c_str());
307
308
309 std::string out = " ";
310 std::string lang_str;
311 // display available language list
312 for (int i = 0; i < languages.size(); i++)
313 {
314 lang_str = tag_uint_to_str2(languages[i].gamelang);
315
316 // select with brackets current user language
317 if (languages[i].gamelang == conn_get_gamelang_localized(c))
318 out += "[" + lang_str + "]";
319 else
320 out += lang_str;
321 if (i < languages.size() - 1)
322 out += ", ";
323 }
324 message_send_text(c, message_type_info, c, out.c_str());
325
326 return -1;
327 }
328
329 const char * userlang = args[1].c_str();
330
331 // validate given language
332 bool found;
333 t_language lang = language_find_by_tag(tag_str_to_uint(userlang), found);
334 if (!found)
335 {
336 message_send_text(c, message_type_error, c, localize(c, "Bad language code."));
337 return -1;
338 }
339
340 if (t_account * account = conn_get_account(c))
341 {
342 account_set_userlang(account, tag_uint_to_str2(lang.gamelang));
343 message_send_text(c, message_type_error, c, localize(c, "Set your language to {} ({})", tag_uint_to_str2(lang.gamelang), lang.name));
344 }
345 return 0;
346 }
347
348
349
350 /*
351 Return language tag by country code (for example: "USA")
352 If not found then return default language (enUS)
353 */
354 extern t_language language_find_by_country(const char * code, bool &found)
355 {
356 found = false;
357 if (!code || code[0] == '\0')
358 return languages[0];
359
360 for (int i = 0; i < languages.size(); i++)
361 {
362 for (int j = 0; j < languages[i].countries.size(); j++)
363 if (strcasecmp(code, languages[i].countries[j].c_str()) == 0)
364 {
365 found = true;
366 return languages[i];
367 }
368 }
369 return languages[0]; // default
370 }
371 /*
372 Return language tag by gamelang
373 If not found then return default language (enUS)
374 */
375 extern t_language language_find_by_tag(t_tag gamelang, bool &found)
376 {
377 found = false;
378 t_language lang;
379
380 for (int i = 0; i < languages.size(); i++)
381 {
382 if (strcasecmp(tag_uint_to_str2(languages[i].gamelang).c_str(), tag_uint_to_str2(gamelang).c_str()) == 0)
383 {
384 found = true;
385 return languages[i];
386 }
387 }
388 return languages[0];
389 }
390
391
392 extern t_language language_get_by_country(const char * code)
393 {
394 bool found;
395 return language_find_by_country(code, found);
396 }
397 extern t_gamelang gamelang_get_by_country(const char * code)
398 {
399 bool found;
400 return language_find_by_country(code, found).gamelang;
401 }
402
403 extern t_language language_get_by_tag(t_gamelang gamelang)
404 {
405 bool found;
406 return language_find_by_tag(gamelang, found);
407 }
408
409
410
411
412 /* Convert text with user native encoding */
413 extern char * i18n_convert(t_connection * c, char * buf)
414 {
415 t_gamelang gamelang = conn_get_gamelang_localized(c);
416 t_clienttag clienttag = conn_get_clienttag(c);
417
418 switch (gamelang)
419 {
420 case GAMELANG_RUSSIAN_UINT:
421 // All Blizzard games except Warcraft 3
422 if (clienttag == CLIENTTAG_STARCRAFT_UINT || clienttag == CLIENTTAG_BROODWARS_UINT || clienttag == CLIENTTAG_STARJAPAN_UINT || clienttag == CLIENTTAG_SHAREWARE_UINT ||
423 clienttag == CLIENTTAG_DIABLORTL_UINT || clienttag == CLIENTTAG_DIABLOSHR_UINT || clienttag == CLIENTTAG_WARCIIBNE_UINT ||
424 clienttag == CLIENTTAG_DIABLO2DV_UINT || clienttag == CLIENTTAG_DIABLO2XP_UINT)
425 {
426 convert_utf8_to_windows1251(buf, buf, MAX_MESSAGE_LEN);
427 }
428 // There is an additional conversion in Starcraft and Diablo 2
429 if (clienttag == CLIENTTAG_STARCRAFT_UINT || clienttag == CLIENTTAG_BROODWARS_UINT || clienttag == CLIENTTAG_STARJAPAN_UINT || clienttag == CLIENTTAG_SHAREWARE_UINT ||
430 clienttag == CLIENTTAG_DIABLO2DV_UINT || clienttag == CLIENTTAG_DIABLO2XP_UINT)
431 {
432 convert_windows1252_to_utf8(buf);
433 }
434 break;
435
436 default:
437 break;
438 }
439 return buf;
440 }
441
442
443 /*
444 * Text conversion for Russian text
445 */
446 #ifndef REGION_CONVERT_RUSSIAN
447
448 typedef struct ConvLetter
449 {
450 unsigned char win1251;
451 int unicode;
452 } Letter;
453
454 static Letter g_letters[] = {
455 { 0x82, 0x201A }, { 0x83, 0x0453 }, { 0x84, 0x201E }, { 0x85, 0x2026 },
456 { 0x86, 0x2020 }, { 0x87, 0x2021 }, { 0x88, 0x20AC }, { 0x89, 0x2030 },
457 { 0x8A, 0x0409 }, { 0x8B, 0x2039 }, { 0x8C, 0x040A }, { 0x8D, 0x040C },
458 { 0x8E, 0x040B }, { 0x8F, 0x040F }, { 0x90, 0x0452 }, { 0x91, 0x2018 },
459 { 0x92, 0x2019 }, { 0x93, 0x201C }, { 0x94, 0x201D }, { 0x95, 0x2022 },
460 { 0x96, 0x2013 }, { 0x97, 0x2014 }, { 0x99, 0x2122 }, { 0x9A, 0x0459 },
461 { 0x9B, 0x203A }, { 0x9C, 0x045A }, { 0x9D, 0x045C }, { 0x9E, 0x045B },
462 { 0x9F, 0x045F }, { 0xA0, 0x00A0 }, { 0xA1, 0x040E }, { 0xA2, 0x045E },
463 { 0xA3, 0x0408 }, { 0xA4, 0x00A4 }, { 0xA5, 0x0490 }, { 0xA6, 0x00A6 },
464 { 0xA7, 0x00A7 }, { 0xA8, 0x0401 }, { 0xA9, 0x00A9 }, { 0xAA, 0x0404 },
465 { 0xAB, 0x00AB }, { 0xAC, 0x00AC }, { 0xAD, 0x00AD }, { 0xAE, 0x00AE },
466 { 0xAF, 0x0407 }, { 0xB0, 0x00B0 }, { 0xB1, 0x00B1 }, { 0xB2, 0x0406 },
467 { 0xB3, 0x0456 }, { 0xB4, 0x0491 }, { 0xB5, 0x00B5 }, { 0xB6, 0x00B6 },
468 { 0xB7, 0x00B7 }, { 0xB8, 0x0451 }, { 0xB9, 0x2116 }, { 0xBA, 0x0454 },
469 { 0xBB, 0x00BB }, { 0xBC, 0x0458 }, { 0xBD, 0x0405 }, { 0xBE, 0x0455 },
470 { 0xBF, 0x0457 }
471 };
472
473 // https://code.google.com/p/convert-utf8-to-cp1251/
474 int convert_utf8_to_windows1251(const char* utf8, char* windows1251, size_t n)
475 {
476 int i = 0;
477 int j = 0;
478 for (; i < (int)n && utf8[i] != 0; ++i) {
479 char prefix = utf8[i];
480 char suffix = utf8[i + 1];
481 if ((prefix & 0x80) == 0) {
482 windows1251[j] = (char)prefix;
483 ++j;
484 }
485 else if ((~prefix) & 0x20) {
486 int first5bit = prefix & 0x1F;
487 first5bit <<= 6;
488 int sec6bit = suffix & 0x3F;
489 int unicode_char = first5bit + sec6bit;
490
491 if (unicode_char >= 0x410 && unicode_char <= 0x44F) {
492 windows1251[j] = (char)(unicode_char - 0x350);
493 }
494 else if (unicode_char >= 0x80 && unicode_char <= 0xFF) {
495 windows1251[j] = (char)(unicode_char);
496 }
497 else if (unicode_char >= 0x402 && unicode_char <= 0x403) {
498 windows1251[j] = (char)(unicode_char - 0x382);
499 }
500 else {
501 int count = sizeof(g_letters) / sizeof(Letter);
502 for (int k = 0; k < count; ++k) {
503 if (unicode_char == g_letters[k].unicode) {
504 windows1251[j] = g_letters[k].win1251;
505 goto NEXT_LETTER;
506 }
507 }
508 // can't convert this char
509 return 0;
510 }
511 NEXT_LETTER:
512 ++i;
513 ++j;
514 }
515 else {
516 // can't convert this chars
517 return 0;
518 }
519 }
520 windows1251[j] = 0;
521 return 1;
522 }
523
524 int convert_windows1252_to_utf8(char * windows1252)
525 {
526 int i = 0, j = 0;
527 unsigned char c, temp[512];
528
529 while (windows1252[i] != 0)
530 {
531 c = windows1252[i];
532
533 if (c < 128)
534 {
535 temp[j] = c;
536 j++;
537 }
538 else
539 {
540 temp[j] = (c >> 6) | 192;
541 temp[j + 1] = (c & 63) | 128;
542 j += 2;
543 }
544 i++;
545 }
546 temp[j] = 0;
547 memcpy(windows1252, temp, j + 1);
548
549 return 0;
550 }
551 #endif
552
553
554
555
556
557
558 }
559 }