TrinityCore
LanguageMgr.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "LanguageMgr.h"
19#include "Containers.h"
20#include "DB2Stores.h"
21#include "Log.h"
22#include "SpellInfo.h"
23#include "SpellMgr.h"
24#include "Timer.h"
25#include "Util.h"
26#include <sstream>
27
28LanguageMgr::LanguageMgr() : _langsMap(), _wordsMap() { }
29
31{
32 _langsMap.clear();
33 _wordsMap.clear();
34}
35
37{
38 static LanguageMgr instance;
39 return &instance;
40}
41
43{
44 ASSERT(spellEffect && spellEffect->Effect == SPELL_EFFECT_LANGUAGE);
45
46 uint32 languageId = uint32(spellEffect->EffectMiscValue[0]);
47
48 _langsMap.emplace(languageId, LanguageDesc{ spellEffect->SpellID, 0 }); // register without a skill id for now
49}
50
52{
53 uint32 oldMSTime = getMSTime();
54
55 // Load languages from Languages.db2. Just the id, we don't need the name
56 for (LanguagesEntry const* langEntry : sLanguagesStore)
57 {
58 auto spellsRange = Trinity::Containers::MapEqualRange(_langsMap, langEntry->ID);
59 if (spellsRange.begin() == spellsRange.end())
60 _langsMap.emplace(langEntry->ID, LanguageDesc());
61 else
62 {
63 std::vector<LanguageDesc> langsWithSkill;
64 for (LanguagesMap::value_type const& spellItr : spellsRange)
65 for (SkillLineAbilityMap::value_type const& skillPair : Trinity::Containers::MakeIteratorPair(sSpellMgr->GetSkillLineAbilityMapBounds(spellItr.second.SpellId)))
66 langsWithSkill.emplace_back(LanguageDesc{ spellItr.second.SpellId, uint32(skillPair.second->SkillLine) });
67
68 for (LanguageDesc const& langDesc : langsWithSkill)
69 {
70 // erase temporary assignment that lacked skill
71 Trinity::Containers::MultimapErasePair(_langsMap, langEntry->ID, { langDesc.SpellId, 0 });
72 _langsMap.emplace(langEntry->ID, langDesc);
73 }
74 }
75 }
76
77 // Add the languages used in code in case they don't exist
81
82 // Log load time
83 TC_LOG_INFO("server.loading", ">> Loaded {} languages in {} ms", uint32(_langsMap.size()), GetMSTimeDiffToNow(oldMSTime));
84}
85
87{
88 uint32 oldMSTime = getMSTime();
89
90 uint32 wordsNum = 0;
91 for (LanguageWordsEntry const* wordEntry : sLanguageWordsStore)
92 {
93 uint8 length = std::min(18U, uint32(strlen(wordEntry->Word)));
94
95 WordKey key = WordKey(wordEntry->LanguageID, length);
96
97 _wordsMap[key].push_back(wordEntry->Word);
98 ++wordsNum;
99 }
100
101 // log load time
102 TC_LOG_INFO("server.loading", ">> Loaded {} word groups from {} words in {} ms", uint32(_wordsMap.size()), wordsNum, GetMSTimeDiffToNow(oldMSTime));
103}
104
106{
107 return Trinity::Containers::MapGetValuePtr(_wordsMap, WordKey(language, wordLen));
108}
109
110namespace
111{
112 void StripHyperlinks(std::string const& source, std::string& dest)
113 {
114 dest.resize(source.length());
115 size_t destSize = 0;
116 bool skipSquareBrackets = false;
117 for (size_t i = 0; i < source.length(); ++i)
118 {
119 char c = source[i];
120 if (c != '|')
121 {
122 if (!skipSquareBrackets || (c != '[' && c != ']'))
123 dest[destSize++] = source[i];
124
125 continue;
126 }
127
128 if (i + 1 >= source.length())
129 break;
130
131 switch (source[i + 1])
132 {
133 case 'c':
134 case 'C':
135 // skip color
136 i += 9;
137 break;
138 case 'r':
139 ++i;
140 break;
141 case 'H':
142 // skip just past first |h
143 i = source.find("|h", i);
144 if (i != std::string::npos)
145 i += 2;
146 skipSquareBrackets = true;
147 break;
148 case 'h':
149 ++i;
150 skipSquareBrackets = false;
151 break;
152 case 'T':
153 // skip just past closing |t
154 i = source.find("|t", i);
155 if (i != std::string::npos)
156 i += 2;
157 break;
158 default:
159 break;
160 }
161 }
162
163 dest.resize(destSize);
164 }
165
166 void ReplaceUntranslatableCharactersWithSpace(std::string& text)
167 {
168 std::wstring wstrText;
169 if (!Utf8toWStr(text, wstrText))
170 return;
171
172 for (wchar_t& w : wstrText)
173 if (!isExtendedLatinCharacter(w) && !isNumeric(w) && w <= 0xFF && w != L'\\')
174 w = L' ';
175
176 WStrToUtf8(wstrText, text);
177 }
178
179 static char upper_backslash(char c)
180 {
181 return c == '/' ? '\\' : char(toupper(c));
182 }
183
184 static uint32 const sstr_hashtable[16] =
185 {
186 0x486E26EE, 0xDCAA16B3, 0xE1918EEF, 0x202DAFDB,
187 0x341C7DC7, 0x1C365303, 0x40EF2D37, 0x65FD5E49,
188 0xD6057177, 0x904ECE93, 0x1C38024F, 0x98FD323B,
189 0xE3061AE7, 0xA39B0FA1, 0x9797F25F, 0xE4444563,
190 };
191
192 uint32 SStrHash(char const* string, bool caseInsensitive, uint32 seed = 0x7FED7FED)
193 {
194 ASSERT(string);
195
196 uint32 shift = 0xEEEEEEEE;
197 while (*string)
198 {
199 char c = *string++;
200
201 if (caseInsensitive)
202 c = upper_backslash(c);
203
204 seed = (sstr_hashtable[c >> 4] - sstr_hashtable[c & 0xF]) ^ (shift + seed);
205 shift = c + seed + 33 * shift + 3;
206 }
207
208 return seed ? seed : 1;
209 }
210}
211
212std::string LanguageMgr::Translate(std::string const& msg, uint32 language, LocaleConstant locale) const
213{
214 std::string textToTranslate;
215 StripHyperlinks(msg, textToTranslate);
216 ReplaceUntranslatableCharactersWithSpace(textToTranslate);
217
218 std::string result;
219 result.reserve(textToTranslate.length());
220 for (std::string_view str : Trinity::Tokenize(textToTranslate, ' ', false))
221 {
222 uint32 wordLen = std::min(18u, uint32(str.length()));
223 if (LanguageMgr::WordList const* wordGroup = FindWordGroup(language, wordLen))
224 {
225 uint32 wordHash = SStrHash(str.data(), true);
226 uint8 idxInsideGroup = wordHash % wordGroup->size();
227
228 char const* replacementWord = (*wordGroup)[idxInsideGroup];
229
230 switch (locale)
231 {
232 case LOCALE_koKR:
233 case LOCALE_zhCN:
234 case LOCALE_zhTW:
235 {
236 size_t length = std::min(str.length(), strlen(replacementWord));
237 for (size_t i = 0; i < length; ++i)
238 {
239 if (str[i] >= 'A' && str[i] <= 'Z')
240 result += charToUpper(replacementWord[i]);
241 else
242 result += replacementWord[i];
243 }
244 break;
245 }
246 default:
247 {
248 std::wstring wstrSourceWord;
249 if (Utf8toWStr(str, wstrSourceWord))
250 {
251 size_t length = std::min(wstrSourceWord.length(), strlen(replacementWord));
252 for (size_t i = 0; i < length; ++i)
253 {
254 if (isUpper(wstrSourceWord[i]))
255 result += charToUpper(replacementWord[i]);
256 else
257 result += charToLower(replacementWord[i]);
258 }
259 }
260 break;
261 }
262 }
263 }
264
265 result += ' ';
266 }
267
268 if (!result.empty())
269 result.pop_back();
270
271 return result;
272}
273
275{
276 return sLanguagesStore.HasRecord(languageId);
277}
278
280{
282}
LocaleConstant
Definition: Common.h:48
@ LOCALE_zhCN
Definition: Common.h:53
@ LOCALE_zhTW
Definition: Common.h:54
@ LOCALE_koKR
Definition: Common.h:50
DB2Storage< LanguageWordsEntry > sLanguageWordsStore("LanguageWords.db2", &LanguageWordsLoadInfo::Instance)
DB2Storage< LanguagesEntry > sLanguagesStore("Languages.db2", &LanguagesLoadInfo::Instance)
uint8_t uint8
Definition: Define.h:144
uint32_t uint32
Definition: Define.h:142
#define ASSERT
Definition: Errors.h:68
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:159
Language
@ LANG_ADDON_LOGGED
@ LANG_UNIVERSAL
@ LANG_ADDON
@ SPELL_EFFECT_LANGUAGE
#define sSpellMgr
Definition: SpellMgr.h:849
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:57
uint32 getMSTime()
Definition: Timer.h:33
bool WStrToUtf8(wchar_t const *wstr, size_t size, std::string &utf8str)
Definition: Util.cpp:433
bool Utf8toWStr(char const *utf8str, size_t csize, wchar_t *wstr, size_t &wsize)
Definition: Util.cpp:383
bool isExtendedLatinCharacter(wchar_t wchar)
Definition: Util.h:133
bool isNumeric(wchar_t wchar)
Definition: Util.h:189
char charToLower(char c)
Definition: Util.h:331
char charToUpper(char c)
Definition: Util.h:330
bool isUpper(wchar_t wchar)
Definition: Util.h:325
std::pair< uint32, uint8 > WordKey
Definition: LanguageMgr.h:43
WordList const * FindWordGroup(uint32 language, uint32 wordLen) const
WordsMap _wordsMap
Definition: LanguageMgr.h:94
void LoadSpellEffectLanguage(SpellEffectEntry const *spellEffect)
Definition: LanguageMgr.cpp:42
void LoadLanguages()
Definition: LanguageMgr.cpp:51
Trinity::IteratorPair< LanguagesMap::const_iterator > GetLanguageDescById(Language languageId) const
LanguagesMap _langsMap
Definition: LanguageMgr.h:93
std::string Translate(std::string const &msg, uint32 language, LocaleConstant locale) const
static LanguageMgr * instance()
Definition: LanguageMgr.cpp:36
void LoadLanguagesWords()
Definition: LanguageMgr.cpp:86
bool IsLanguageExist(uint32 languageId) const
std::vector< char const * > WordList
Definition: LanguageMgr.h:44
Utility class to enable range for loop syntax for multimap.equal_range uses.
Definition: IteratorPair.h:32
auto MapEqualRange(M &map, typename M::key_type const &key)
Definition: IteratorPair.h:60
constexpr IteratorPair< iterator, end_iterator > MakeIteratorPair(iterator first, end_iterator second)
Definition: IteratorPair.h:48
auto MapGetValuePtr(M &map, typename M::key_type const &key)
Definition: MapUtils.h:29
void MultimapErasePair(M< K, V, Rest... > &multimap, K const &key, V const &value)
Definition: MapUtils.h:39
TC_COMMON_API std::vector< std::string_view > Tokenize(std::string_view str, char sep, bool keepEmpty)
Definition: Util.cpp:56
uint32 SpellId
Definition: LanguageMgr.h:30
std::array< int32, 2 > EffectMiscValue