1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
// Copyright (c) Copyright (c) Hercules Dev Team, licensed under GNU GPL.
// Copyright (c) 2014 - 2015 Evol developers
#include "common/hercules.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common/HPMi.h"
#include "common/memmgr.h"
#include "common/mmo.h"
#include "common/socket.h"
#include "common/strlib.h"
#include "map/npc.h"
#include "map/pc.h"
#include "emap/config.h"
#include "emap/lang.h"
#include "emap/data/session.h"
#include "emap/struct/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 delete_lang_sub(DBKey key __attribute__ ((unused)),
DBData *data,
va_list args __attribute__ ((unused)))
{
int f;
char **strings = DB->data2ptr(data);
for (f = 0; f < lang_num; f ++)
{
aFree(strings[f]);
strings[f] = NULL;
}
return 0;
}
void do_final_langs(void)
{
translate_db->destroy(translate_db, delete_lang_sub);
int f;
for (f = 0; f < lang_num; f ++)
aFree(lang_langs[f]);
}
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 ++;
}
fclose(fp);
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], line2);
strdb_put(translate_db, aStrdup (line1), strings);
}
else
{
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 || !translate_db || !translate_db->get)
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 (warn_missing_translation && 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;
}
|