1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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 //-----------------------------------------------------------------------------
19 //-----------------------------------------------------------------------------
21 #include "nel/misc/i18n.h"
22 #include "game_share/utils.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
31 using namespace NLMISC
;
34 //-----------------------------------------------------------------------------
35 // some local constants
36 //-----------------------------------------------------------------------------
38 static const ucstring EmptyString
;
39 static const char* DefaultLanguageName
= "default";
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 CLangText::CLangText(const CSString
& languageCode
)
57 const CSString
& CLangText::getLanguageCode() const
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());
76 void CLangText::set(const CSString
& tokenName
,const CSString
& txt
)
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
)
92 for (TTexts::iterator it
=_Texts
.begin();it
!=_Texts
.end();++it
)
93 result
.insert(it
->first
);
97 //-----------------------------------------------------------------------------
99 //-----------------------------------------------------------------------------
101 void CText::setLanguage(const CSString
& languageCode
)
103 _ActiveLanguage
= languageCode
;
106 bool CText::read(const CSString
& fileName
)
110 CI18N::readTextFile(fileName
,ucFileBody
);
111 CSString fileBody
= ucFileBody
.toUtf8();
112 if (fileBody
.empty())
116 CVectorSString lines
;
117 fileBody
.splitLines(lines
);
119 for (uint32 i
=0;i
<lines
.size();++i
)
121 CSString line
= lines
[i
].strip();
125 if (line
.left(2)=="//")
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);
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
);
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...
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
);
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
);
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
];
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
)
213 for (uint32 i
=0;i
<_LangTexts
.size();++i
)
214 _LangTexts
[i
].getTokenNameSet(result
,false);
218 //-----------------------------------------------------------------------------