PageRenderTime 38ms CodeModel.GetById 18ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llxuixml/lltrans.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 295 lines | 208 code | 37 blank | 50 comment | 43 complexity | 93f91de4d2e970108c82cd81a026e28f MD5 | raw file
  1/**
  2 * @file lltrans.cpp
  3 * @brief LLTrans implementation
  4 *
  5 * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "linden_common.h"
 28
 29#include "lltrans.h"
 30
 31#include "llfasttimer.h"	// for call count statistics
 32#include "llxuiparser.h"
 33#include "llsd.h"
 34#include "llxmlnode.h"
 35
 36#include <map>
 37
 38LLTrans::template_map_t LLTrans::sStringTemplates;
 39LLStringUtil::format_map_t LLTrans::sDefaultArgs;
 40
 41struct StringDef : public LLInitParam::Block<StringDef>
 42{
 43	Mandatory<std::string> name;
 44	Mandatory<std::string> value;
 45
 46	StringDef()
 47	:	name("name"),
 48		value("value")
 49	{}
 50};
 51
 52struct StringTable : public LLInitParam::Block<StringTable>
 53{
 54	Multiple<StringDef> strings;
 55	StringTable()
 56	:	strings("string")
 57	{}
 58};
 59
 60//static 
 61bool LLTrans::parseStrings(LLXMLNodePtr &root, const std::set<std::string>& default_args)
 62{
 63	std::string xml_filename = "(strings file)";
 64	if (!root->hasName("strings"))
 65	{
 66		llerrs << "Invalid root node name in " << xml_filename 
 67			<< ": was " << root->getName() << ", expected \"strings\"" << llendl;
 68	}
 69
 70	StringTable string_table;
 71	LLXUIParser parser;
 72	parser.readXUI(root, string_table, xml_filename);
 73
 74	if (!string_table.validateBlock())
 75	{
 76		llerrs << "Problem reading strings: " << xml_filename << llendl;
 77		return false;
 78	}
 79	
 80	sStringTemplates.clear();
 81	sDefaultArgs.clear();
 82	
 83	for(LLInitParam::ParamIterator<StringDef>::const_iterator it = string_table.strings.begin();
 84		it != string_table.strings.end();
 85		++it)
 86	{
 87		LLTransTemplate xml_template(it->name, it->value);
 88		sStringTemplates[xml_template.mName] = xml_template;
 89		
 90		std::set<std::string>::const_iterator iter = default_args.find(xml_template.mName);
 91		if (iter != default_args.end())
 92		{
 93			std::string name = *iter;
 94			if (name[0] != '[')
 95				name = llformat("[%s]",name.c_str());
 96			sDefaultArgs[name] = xml_template.mText;
 97		}
 98	}
 99
100	return true;
101}
102
103
104//static
105bool LLTrans::parseLanguageStrings(LLXMLNodePtr &root)
106{
107	std::string xml_filename = "(language strings file)";
108	if (!root->hasName("strings"))
109	{
110		llerrs << "Invalid root node name in " << xml_filename 
111		<< ": was " << root->getName() << ", expected \"strings\"" << llendl;
112	}
113	
114	StringTable string_table;
115	LLXUIParser parser;
116	parser.readXUI(root, string_table, xml_filename);
117	
118	if (!string_table.validateBlock())
119	{
120		llerrs << "Problem reading strings: " << xml_filename << llendl;
121		return false;
122	}
123		
124	for(LLInitParam::ParamIterator<StringDef>::const_iterator it = string_table.strings.begin();
125		it != string_table.strings.end();
126		++it)
127	{
128		// share the same map with parseStrings() so we can search the strings using the same getString() function.- angela
129		LLTransTemplate xml_template(it->name, it->value);
130		sStringTemplates[xml_template.mName] = xml_template;
131	}
132	
133	return true;
134}
135
136
137
138static LLFastTimer::DeclareTimer FTM_GET_TRANS("Translate string");
139
140//static 
141std::string LLTrans::getString(const std::string &xml_desc, const LLStringUtil::format_map_t& msg_args)
142{
143	// Don't care about time as much as call count.  Make sure we're not
144	// calling LLTrans::getString() in an inner loop. JC
145	LLFastTimer timer(FTM_GET_TRANS);
146	
147	template_map_t::iterator iter = sStringTemplates.find(xml_desc);
148	if (iter != sStringTemplates.end())
149	{
150		std::string text = iter->second.mText;
151		LLStringUtil::format_map_t args = sDefaultArgs;
152		args.insert(msg_args.begin(), msg_args.end());
153		LLStringUtil::format(text, args);
154		
155		return text;
156	}
157	else
158	{
159		LL_WARNS_ONCE("configuration") << "Missing String in strings.xml: [" << xml_desc << "]" << LL_ENDL;
160		return "MissingString("+xml_desc+")";
161	}
162}
163
164//static
165std::string LLTrans::getString(const std::string &xml_desc, const LLSD& msg_args)
166{
167	// Don't care about time as much as call count.  Make sure we're not
168	// calling LLTrans::getString() in an inner loop. JC
169	LLFastTimer timer(FTM_GET_TRANS);
170
171	template_map_t::iterator iter = sStringTemplates.find(xml_desc);
172	if (iter != sStringTemplates.end())
173	{
174		std::string text = iter->second.mText;
175		LLStringUtil::format(text, msg_args);
176		return text;
177	}
178	else
179	{
180		LL_WARNS_ONCE("configuration") << "Missing String in strings.xml: [" << xml_desc << "]" << LL_ENDL;
181		return "MissingString("+xml_desc+")";
182	}
183}
184
185//static 
186bool LLTrans::findString(std::string &result, const std::string &xml_desc, const LLStringUtil::format_map_t& msg_args)
187{
188	LLFastTimer timer(FTM_GET_TRANS);
189	
190	template_map_t::iterator iter = sStringTemplates.find(xml_desc);
191	if (iter != sStringTemplates.end())
192	{
193		std::string text = iter->second.mText;
194		LLStringUtil::format_map_t args = sDefaultArgs;
195		args.insert(msg_args.begin(), msg_args.end());
196		LLStringUtil::format(text, args);
197		result = text;
198		return true;
199	}
200	else
201	{
202		LL_WARNS_ONCE("configuration") << "Missing String in strings.xml: [" << xml_desc << "]" << LL_ENDL;	
203		return false;
204	}
205}
206
207//static
208bool LLTrans::findString(std::string &result, const std::string &xml_desc, const LLSD& msg_args)
209{
210	LLFastTimer timer(FTM_GET_TRANS);
211
212	template_map_t::iterator iter = sStringTemplates.find(xml_desc);
213	if (iter != sStringTemplates.end())
214	{
215		std::string text = iter->second.mText;
216		LLStringUtil::format(text, msg_args);
217		result = text;
218		return true;
219	}
220	else
221	{
222		LL_WARNS_ONCE("configuration") << "Missing String in strings.xml: [" << xml_desc << "]" << LL_ENDL;	
223		return false;
224	}
225}
226
227//static
228std::string LLTrans::getCountString(const std::string& language, const std::string& xml_desc, S32 count)
229{
230	// Compute which string identifier to use
231	const char* form = "";
232	if (language == "ru") // Russian
233	{
234		// From GNU ngettext()
235		// Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;
236		if (count % 10 == 1
237			&& count % 100 != 11)
238		{
239			// singular, "1 item"
240			form = "A";
241		}
242		else if (count % 10 >= 2
243			&& count % 10 <= 4
244			&& (count % 100 < 10 || count % 100 >= 20) )
245		{
246			// special case "2 items", "23 items", but not "13 items"
247			form = "B";
248		}
249		else
250		{
251			// English-style plural, "5 items"
252			form = "C";
253		}
254	}
255	else if (language == "fr" || language == "pt") // French, Brazilian Portuguese
256	{
257		// French and Portuguese treat zero as a singular "0 item" not "0 items"
258		if (count == 0 || count == 1)
259		{
260			form = "A";
261		}
262		else
263		{
264			// English-style plural
265			form = "B";
266		}
267	}
268	else // default
269	{
270		// languages like English with 2 forms, singular and plural
271		if (count == 1)
272		{
273			// "1 item"
274			form = "A";
275		}
276		else
277		{
278			// "2 items", also use plural for "0 items"
279			form = "B";
280		}
281	}
282
283	// Translate that string
284	LLStringUtil::format_map_t args;
285	args["[COUNT]"] = llformat("%d", count);
286
287	// Look up "AgeYearsB" or "AgeWeeksC" including the "form"
288	std::string key = llformat("%s%s", xml_desc.c_str(), form);
289	return getString(key, args);
290}
291
292void LLTrans::setDefaultArg(const std::string& name, const std::string& value)
293{
294	sDefaultArgs[name] = value;
295}