18 template <> LanguageManager* Singleton<LanguageManager>::msInstance =
nullptr;
19 template <>
const char* Singleton<LanguageManager>::mClassTypeName =
"LanguageManager";
23 mXmlLanguageTagName(
"Language")
46 mIsInitialise =
false;
51 std::string default_lang;
52 bool event_change =
false;
56 while (root.
next(mXmlLanguageTagName))
63 while (info.
next(
"Info"))
72 while (source_info.
next(
"Source"))
81 while (source_info.next(
"Source"))
83 std::string file_source = source_info->
getContent();
85 mMapFile[name].push_back(file_source);
88 if (name == mCurrentLanguageName)
90 loadLanguage(file_source,
false);
99 if (!default_lang.empty())
101 else if (event_change)
107 MapListString::iterator item = mMapFile.find(_name);
108 if (item == mMapFile.end())
110 MYGUI_LOG(Error,
"Language '" << _name <<
"' is not found");
114 mMapLanguage.clear();
115 mCurrentLanguageName = _name;
117 for (VectorString::const_iterator iter = item->second.begin(); iter != item->second.end(); ++iter)
119 loadLanguage(*iter,
false);
125 bool LanguageManager::loadLanguage(
const std::string& _file,
bool _user)
130 MYGUI_LOG(Error,
"file '" << _file <<
"' not found");
134 if (_file.find(
".xml") != std::string::npos)
135 _loadLanguageXML(data.
getData(), _user);
137 _loadLanguage(data.
getData(), _user);
142 void LanguageManager::_loadLanguageXML(IDataStream* _stream,
bool _user)
146 if (doc.open(_stream))
152 while (tag.next(
"Tag"))
155 mUserMapLanguage[tag->findAttribute(
"name")] = tag->getContent();
157 mMapLanguage[tag->findAttribute(
"name")] = tag->getContent();
163 void LanguageManager::_loadLanguage(IDataStream* _stream,
bool _user)
167 while (!_stream->eof())
169 _stream->readline(read,
'\n');
170 if (read.empty())
continue;
173 if ((
uint8)read[0] == 0xEF && read.size() > 2)
178 if (read[read.size() - 1] ==
'\r') read.erase(read.size() - 1, 1);
179 if (read.empty())
continue;
181 size_t pos = read.find_first_of(
" \t");
184 if (pos == std::string::npos) mUserMapLanguage[read] =
"";
185 else mUserMapLanguage[read.substr(0, pos)] = read.substr(pos + 1, std::string::npos);
189 if (pos == std::string::npos) mMapLanguage[read] =
"";
190 else mMapLanguage[read.substr(0, pos)] = read.substr(pos + 1, std::string::npos);
199 bool replace =
false;
202 result = replaceTagsPass(result, replace);
211 MapLanguageString::iterator iter = mMapLanguage.find(_tag);
212 if (iter != mMapLanguage.end())
217 MapLanguageString::iterator iterUser = mUserMapLanguage.find(_tag);
218 if (iterUser != mUserMapLanguage.end())
220 return iterUser->second;
228 return mCurrentLanguageName;
233 mUserMapLanguage[_tag] = _replace;
238 mUserMapLanguage.clear();
243 return loadLanguage(_file,
true);
246 UString LanguageManager::replaceTagsPass(
const UString& _line,
bool& _replaceResult)
248 _replaceResult =
false;
281 size_t start = iter - line.begin();
282 size_t len = (iter2 - line.begin()) - start - 1;
283 const UString& tag = line.substr(start + 1, len);
288 MapLanguageString::iterator replace = mMapLanguage.find(tag);
289 if (replace != mMapLanguage.end())
291 replacement = replace->second;
296 replace = mUserMapLanguage.find(tag);
297 if (replace != mUserMapLanguage.end())
299 replacement = replace->second;
316 iter = line.insert(iter,
'#') + size_t(len + 2);
322 _replaceResult =
true;
324 iter = line.erase(iter -
size_t(1), iter2 +
size_t(1));
325 size_t pos = iter - line.begin();
326 line.insert(pos, replacement);
327 iter = line.begin() + pos + replacement.length();