diff options
author | Andrei Karas <akaras@inbox.ru> | 2014-11-20 15:13:10 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2014-11-20 15:13:10 +0300 |
commit | 8c7713fd898900e7afe936e647c051f0a92e07e1 (patch) | |
tree | 6a169bc1d569f794c50cb50b09a38f0dca5e5260 /src | |
parent | 092b7e47263555de36ac7c34bfc681e680b71bbf (diff) | |
download | evol-hercules-8c7713fd898900e7afe936e647c051f0a92e07e1.tar.gz evol-hercules-8c7713fd898900e7afe936e647c051f0a92e07e1.tar.bz2 evol-hercules-8c7713fd898900e7afe936e647c051f0a92e07e1.tar.xz evol-hercules-8c7713fd898900e7afe936e647c051f0a92e07e1.zip |
Add lang.c with translation related functionality.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/map/lang.c | 209 | ||||
-rw-r--r-- | src/map/lang.h | 17 |
3 files changed, 228 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index e3cc46f..e46f894 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -19,6 +19,8 @@ MAP_SRC = map/clif.c \ map/dummy.c \ map/dummy.h \ map/init.c \ + map/lang.c \ + map/lang.h \ map/npc.c \ map/npc.h \ map/parse.c \ diff --git a/src/map/lang.c b/src/map/lang.c new file mode 100644 index 0000000..ef965dd --- /dev/null +++ b/src/map/lang.c @@ -0,0 +1,209 @@ +// Copyright (c) Copyright (c) Hercules Dev Team, licensed under GNU GPL. +// Copyright (c) 2014 Evol developers + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "../../../common/HPMi.h" +#include "../../../common/malloc.h" +#include "../../../common/mmo.h" +#include "../../../common/socket.h" +#include "../../../common/strlib.h" +#include "../../../map/npc.h" +#include "../../../map/pc.h" + +#include "map/lang.h" +#include "map/session.h" +#include "map/sessionext.h" + +#define MAX_LANGS 100 + +struct DBMap *translate_db = NULL; +char *lang_langs[MAX_LANGS]; +int lang_num = 0; + +static int langsdb_readdb (void); +static int langsdb_readlangs (void); + +void do_init_langs (void) +{ + translate_db = strdb_alloc(DB_OPT_RELEASE_BOTH, 1000); + + langsdb_readlangs (); + langsdb_readdb (); +} + +static int langsdb_readlangs (void) +{ + FILE *fp; + lang_num = 0; + fp = fopen("langs/langs.txt", "r"); + if (fp == NULL) + { + printf ("can't read langs/langs.txt\n"); + return 1; + } + char line[100]; + char text[101]; + while (fgets (line, 99, fp)) + { + if (sscanf(line, "%99s\n", text) < 1) + continue; + + lang_langs[lang_num] = aStrdup (text); + lang_num ++; + } + return 0; +} + +static int langsdb_readdb (void) +{ + char line[1020], line1[1020], line2[1020]; + char filename[1000]; + char **strings = NULL; + char *idx; + int i; + int sz; + for (i = 0; i < lang_num; i ++) + { + strcpy (filename, "langs/lang_"); + strcat (filename, lang_langs[i]); + strcat (filename, ".txt"); + + FILE *fp = fopen(filename, "r"); + if (fp == NULL) + { + printf ("can't read %s\n", filename); + return 1; + } + + if (!fgets (line, 1010, fp)) + { + printf ("can't read %s\n", filename); + continue; + } + + line1[0] = 0; + line2[0] = 0; + while (fgets (line, 1010, fp)) + { + if (*line) + { + idx = strrchr (line, '\n'); + if (idx) + *idx = 0; + } + + if (!*line) + { + line1[0] = 0; + line2[0] = 0; + continue; + } + else if (!*line1) + { + strcpy (line1, line); + continue; + } + strcpy (line2, line); + + strings = (char **)strdb_get(translate_db, line1); + if (!strings) + { + strings = aCalloc (lang_num, sizeof(int*)); + sz = strlen(line1) + 1; + strings[0] = aCalloc (sz < 24 ? 24 : sz, sizeof(char)); + strcpy (strings[0], line1); + strdb_put(translate_db, aStrdup (line1), strings); + } + + sz = strlen(line2) + 1; + strings[i] = aCalloc (sz < 24 ? 24 : sz, sizeof(char)); + strcpy (strings[i], line2); + + *line1 = 0; + *line2 = 0; + } + fclose (fp); + } + return 0; +} + +const char* lang_trans(const char *str, int lng, int flg) +{ + if (!str) + return 0; + + if (lng < 0 || lng >= lang_num) + return str; + + char **strings = (char **)strdb_get(translate_db, str); + if (!strings) + { + if (flg) + printf ("no translations for: %s\n", str); + return str; + } + + if (!strings[lng]) + { + if (flg) + printf ("no lang string (%s) for: %s\n", lang_langs[lng], str); + return str; + } + + return strings[lng]; +} + +const char* lang_pctrans(const char *str, TBL_PC *sd) +{ + int lng = 0; + int flg = 1; + if (!str) + return 0; + + if (*str == '#') + flg = 0; + if (sd) + { + struct SessionExt *data = session_get_bysd(sd); + if (data) + lng = data->language; + } + + return lang_trans(str, lng, flg); +} + +int lang_getId(const char *str) +{ + char *str1 = aStrdup(str); + char *str2 = NULL; + int f; + + if ((str2 = strchr(str, '.'))) + *str2 = 0; + + for (f = 0; f < MAX_LANGS && lang_langs[f]; f ++) + { + if (!strcmp(str, lang_langs[f])) + { + aFree (str1); + return f; + } + } + + if ((str2 = strchr(str1, '_'))) + *str2 = 0; + + for (f = 0; f < MAX_LANGS && lang_langs[f]; f ++) + { + if (strstr(lang_langs[f], str1) == lang_langs[f]) + { + aFree (str1); + return f; + } + } + aFree (str1); + return -1; +} diff --git a/src/map/lang.h b/src/map/lang.h new file mode 100644 index 0000000..0c1e0fa --- /dev/null +++ b/src/map/lang.h @@ -0,0 +1,17 @@ +// Copyright (c) Copyright (c) Hercules Dev Team, licensed under GNU GPL. +// Copyright (c) 2014 Evol developers + +#ifndef EVOL_MAP_LANG +#define EVOL_MAP_LANG + +extern struct DBMap *translate_db; + +void do_init_langs (void); + +const char* lang_trans(const char *str, int lng, int flg); + +const char* lang_pctrans(const char *str, TBL_PC *sd); + +int lang_getId(const char *str); + +#endif // EVOL_MAP_LANG |