Add infos into target window
[ryzomcore.git] / ryzom / server / src / general_utilities_service / gus_text.cpp
blobe84d0676a4b115d1480707322e777f6e1624d098
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //-----------------------------------------------------------------------------
18 // includes
19 //-----------------------------------------------------------------------------
21 #include "nel/misc/i18n.h"
22 #include "game_share/utils.h"
23 #include "gus_text.h"
26 //-----------------------------------------------------------------------------
27 // namespaces
28 //-----------------------------------------------------------------------------
30 using namespace std;
31 using namespace NLMISC;
34 //-----------------------------------------------------------------------------
35 // some local constants
36 //-----------------------------------------------------------------------------
38 static const ucstring EmptyString;
39 static const char* DefaultLanguageName= "default";
42 //-----------------------------------------------------------------------------
43 // GUS namespace
44 //-----------------------------------------------------------------------------
46 namespace GUS
48 //-----------------------------------------------------------------------------
49 // methods CLangText
50 //-----------------------------------------------------------------------------
52 CLangText::CLangText(const CSString& languageCode)
54 _Code= languageCode;
57 const CSString& CLangText::getLanguageCode() const
59 return _Code;
62 const ucstring& CLangText::get(const CSString& tokenName) const
64 TTexts::const_iterator it= _Texts.find(tokenName);
65 return (it==_Texts.end())? EmptyString: it->second;
68 void CLangText::set(const CSString& tokenName,const ucstring& txt)
70 ucstring& ucs= _Texts[tokenName];
71 if (!ucs.empty() && ucs!=txt)
72 nlwarning("Language %s: Replacing text for token: %s with: %s",_Code.c_str(),tokenName.c_str(),txt.toUtf8().c_str());
73 ucs= txt;
76 void CLangText::set(const CSString& tokenName,const CSString& txt)
78 ucstring uct;
79 uct.fromUtf8(txt);
80 set(tokenName,uct);
83 void CLangText::display() const
85 InfoLog->displayNL("Language %s: %d texts",_Code.c_str(),_Texts.size());
88 void CLangText::getTokenNameSet(std::set<NLMISC::CSString>& result,bool clearResultFirst)
90 if (clearResultFirst)
91 result.clear();
92 for (TTexts::iterator it=_Texts.begin();it!=_Texts.end();++it)
93 result.insert(it->first);
97 //-----------------------------------------------------------------------------
98 // methods CTextSet
99 //-----------------------------------------------------------------------------
101 void CText::setLanguage(const CSString& languageCode)
103 _ActiveLanguage= languageCode;
106 bool CText::read(const CSString& fileName)
108 // load the file
109 ucstring ucFileBody;
110 CI18N::readTextFile(fileName,ucFileBody);
111 CSString fileBody= ucFileBody.toUtf8();
112 if (fileBody.empty())
113 return false;
115 // parse the file
116 CVectorSString lines;
117 fileBody.splitLines(lines);
118 bool ok=true;
119 for (uint32 i=0;i<lines.size();++i)
121 CSString line= lines[i].strip();
122 if (line.empty())
123 continue;
124 // skip comments
125 if (line.left(2)=="//")
126 continue;
127 // see whether this is a "<lang>:<keyword>" or just a "<keyword>"
128 if (line.word(1)==":")
130 // get the langugae code from the line
131 CSString lang= line.firstWord(true);
132 // skip the ':'
133 line.firstWord(true);
134 // get the keyword from the line
135 CSString keyword= line.firstWord(true);
136 // make sure there's still some text left in the line
137 DROP_IF (line.empty(),"Invalid line: "+lines[i],ok=false;continue);
138 // add the text to our database...
139 set(lang,keyword,line);
141 else
143 // get the keyword from the line
144 CSString keyword= line.firstWord(true);
145 // make sure there's still some text left in the line
146 DROP_IF (line.empty(),"Invalid line: "+lines[i],ok=false;continue);
147 // add the text to our database...
148 set(keyword,line);
152 return ok;
155 const ucstring& CText::get(const CSString& tokenName) const
157 const ucstring *result;
158 result= &get(_ActiveLanguage,tokenName);
159 if (result->empty() && _ActiveLanguage!=DefaultLanguageName)
160 return get(DefaultLanguageName,tokenName);
161 return *result;
164 const ucstring& CText::get(const CSString& languageCode,const CSString& tokenName) const
166 for (uint32 i=0;i<_LangTexts.size();++i)
168 if (_LangTexts[i].getLanguageCode()==languageCode)
169 return _LangTexts[i].get(tokenName);
171 return EmptyString;
174 void CText::set(const CSString& languageCode,const CSString& tokenName,const ucstring& txt)
176 CLangText* theLangText= NULL;
178 // look for the container for the given language code...
179 for (uint32 i=0;i<_LangTexts.size();++i)
181 if (_LangTexts[i].getLanguageCode()==languageCode)
183 theLangText= &_LangTexts[i];
184 break;
188 // create a new container ifor this lang if needed
189 if (theLangText==NULL)
191 _LangTexts.push_back(CLangText(languageCode));
192 theLangText= &_LangTexts.back();
195 // perform the 'set' operation
196 theLangText->set(tokenName,txt);
199 void CText::set(const CSString& tokenName,const ucstring& txt)
201 set(DefaultLanguageName,tokenName,txt);
204 void CText::display() const
206 for (uint32 i=0;i<_LangTexts.size();++i)
207 _LangTexts[i].display();
210 void CText::getTokenNameSet(std::set<NLMISC::CSString>& result)
212 result.clear();
213 for (uint32 i=0;i<_LangTexts.size();++i)
214 _LangTexts[i].getTokenNameSet(result,false);
218 //-----------------------------------------------------------------------------