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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "LanguageMgr.h"
#include "Containers.h"
#include "DB2Stores.h"
#include "Log.h"
#include "SpellInfo.h"
#include "SpellMgr.h"
#include "Timer.h"
#include "Util.h"
#include <sstream>
LanguageMgr::LanguageMgr() : _langsMap(), _wordsMap() { }
LanguageMgr::~LanguageMgr()
{
_langsMap.clear();
_wordsMap.clear();
}
LanguageMgr* LanguageMgr::instance()
{
static LanguageMgr instance;
return &instance;
}
void LanguageMgr::LoadSpellEffectLanguage(SpellEffectEntry const* spellEffect)
{
ASSERT(spellEffect && spellEffect->Effect == SPELL_EFFECT_LANGUAGE);
uint32 languageId = uint32(spellEffect->EffectMiscValue[0]);
_langsMap.emplace(languageId, LanguageDesc{ spellEffect->SpellID, 0 }); // register without a skill id for now
}
void LanguageMgr::LoadLanguages()
{
uint32 oldMSTime = getMSTime();
// Load languages from Languages.db2. Just the id, we don't need the name
for (LanguagesEntry const* langEntry : sLanguagesStore)
{
auto spellsRange = Trinity::Containers::MapEqualRange(_langsMap, langEntry->ID);
if (spellsRange.begin() == spellsRange.end())
_langsMap.emplace(langEntry->ID, LanguageDesc());
else
{
std::vector<LanguageDesc> langsWithSkill;
for (LanguagesMap::value_type const& spellItr : spellsRange)
for (SkillLineAbilityMap::value_type const& skillPair : Trinity::Containers::MakeIteratorPair(sSpellMgr->GetSkillLineAbilityMapBounds(spellItr.second.SpellId)))
langsWithSkill.emplace_back(LanguageDesc{ spellItr.second.SpellId, uint32(skillPair.second->SkillLine) });
for (LanguageDesc const& langDesc : langsWithSkill)
{
// erase temporary assignment that lacked skill
Trinity::Containers::MultimapErasePair(_langsMap, langEntry->ID, { langDesc.SpellId, 0 });
_langsMap.emplace(langEntry->ID, langDesc);
}
}
}
// Add the languages used in code in case they don't exist
_langsMap.emplace(LANG_UNIVERSAL, LanguageDesc());
_langsMap.emplace(LANG_ADDON, LanguageDesc());
_langsMap.emplace(LANG_ADDON_LOGGED, LanguageDesc());
// Log load time
TC_LOG_INFO("server.loading", ">> Loaded {} languages in {} ms", uint32(_langsMap.size()), GetMSTimeDiffToNow(oldMSTime));
}
void LanguageMgr::LoadLanguagesWords()
{
uint32 oldMSTime = getMSTime();
uint32 wordsNum = 0;
for (LanguageWordsEntry const* wordEntry : sLanguageWordsStore)
{
uint8 length = std::min(18U, uint32(strlen(wordEntry->Word)));
WordKey key = WordKey(wordEntry->LanguageID, length);
_wordsMap[key].push_back(wordEntry->Word);
++wordsNum;
}
// log load time
TC_LOG_INFO("server.loading", ">> Loaded {} word groups from {} words in {} ms", uint32(_wordsMap.size()), wordsNum, GetMSTimeDiffToNow(oldMSTime));
}
LanguageMgr::WordList const* LanguageMgr::FindWordGroup(uint32 language, uint32 wordLen) const
{
return Trinity::Containers::MapGetValuePtr(_wordsMap, WordKey(language, wordLen));
}
namespace
{
void StripHyperlinks(std::string const& source, std::string& dest)
{
dest.resize(source.length());
size_t destSize = 0;
bool skipSquareBrackets = false;
for (size_t i = 0; i < source.length(); ++i)
{
char c = source[i];
if (c != '|')
{
if (!skipSquareBrackets || (c != '[' && c != ']'))
dest[destSize++] = source[i];
continue;
}
if (i + 1 >= source.length())
break;
switch (source[i + 1])
{
case 'c':
case 'C':
// skip color
i += 9;
break;
case 'r':
++i;
break;
case 'H':
// skip just past first |h
i = source.find("|h", i);
if (i != std::string::npos)
i += 2;
skipSquareBrackets = true;
break;
case 'h':
++i;
skipSquareBrackets = false;
break;
case 'T':
// skip just past closing |t
i = source.find("|t", i);
if (i != std::string::npos)
i += 2;
break;
default:
break;
}
}
dest.resize(destSize);
}
void ReplaceUntranslatableCharactersWithSpace(std::string& text)
{
std::wstring wstrText;
if (!Utf8toWStr(text, wstrText))
return;
for (wchar_t& w : wstrText)
if (!isExtendedLatinCharacter(w) && !isNumeric(w) && w <= 0xFF && w != L'\\')
w = L' ';
WStrToUtf8(wstrText, text);
}
static char upper_backslash(char c)
{
return c == '/' ? '\\' : char(toupper(c));
}
static uint32 const sstr_hashtable[16] =
{
0x486E26EE, 0xDCAA16B3, 0xE1918EEF, 0x202DAFDB,
0x341C7DC7, 0x1C365303, 0x40EF2D37, 0x65FD5E49,
0xD6057177, 0x904ECE93, 0x1C38024F, 0x98FD323B,
0xE3061AE7, 0xA39B0FA1, 0x9797F25F, 0xE4444563,
};
uint32 SStrHash(char const* string, bool caseInsensitive, uint32 seed = 0x7FED7FED)
{
ASSERT(string);
uint32 shift = 0xEEEEEEEE;
while (*string)
{
char c = *string++;
if (caseInsensitive)
c = upper_backslash(c);
seed = (sstr_hashtable[c >> 4] - sstr_hashtable[c & 0xF]) ^ (shift + seed);
shift = c + seed + 33 * shift + 3;
}
return seed ? seed : 1;
}
}
std::string LanguageMgr::Translate(std::string const& msg, uint32 language, LocaleConstant locale) const
{
std::string textToTranslate;
StripHyperlinks(msg, textToTranslate);
ReplaceUntranslatableCharactersWithSpace(textToTranslate);
std::string result;
result.reserve(textToTranslate.length());
for (std::string_view str : Trinity::Tokenize(textToTranslate, ' ', false))
{
uint32 wordLen = std::min(18u, uint32(str.length()));
if (LanguageMgr::WordList const* wordGroup = FindWordGroup(language, wordLen))
{
uint32 wordHash = SStrHash(str.data(), true);
uint8 idxInsideGroup = wordHash % wordGroup->size();
char const* replacementWord = (*wordGroup)[idxInsideGroup];
switch (locale)
{
case LOCALE_koKR:
case LOCALE_zhCN:
case LOCALE_zhTW:
{
size_t length = std::min(str.length(), strlen(replacementWord));
for (size_t i = 0; i < length; ++i)
{
if (str[i] >= 'A' && str[i] <= 'Z')
result += char(toupper(replacementWord[i]));
else
result += replacementWord[i];
}
break;
}
default:
{
std::wstring wstrSourceWord;
if (Utf8toWStr(str, wstrSourceWord))
{
size_t length = std::min(wstrSourceWord.length(), strlen(replacementWord));
for (size_t i = 0; i < length; ++i)
{
if (isUpper(wstrSourceWord[i]))
result += char(toupper(replacementWord[i]));
else
result += char(tolower(replacementWord[i]));
}
}
break;
}
}
}
result += ' ';
}
if (!result.empty())
result.pop_back();
return result;
}
bool LanguageMgr::IsLanguageExist(uint32 languageId) const
{
return sLanguagesStore.HasRecord(languageId);
}
Trinity::IteratorPair<LanguageMgr::LanguagesMap::const_iterator> LanguageMgr::GetLanguageDescById(Language languageId) const
{
return Trinity::Containers::MapEqualRange(_langsMap, languageId);
}
|