lltrans.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @file lltrans.cpp
  3. * @brief LLTrans implementation
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, Linden Research, Inc.
  8. * Copyright (c) 2023, Henri Beauchamp.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #include "linden_common.h"
  34. #include "lltrans.h"
  35. #include "llnotifications.h"
  36. #include "lluictrlfactory.h"
  37. #include "llxmlnode.h"
  38. LLTrans::map_t LLTrans::sStringTemplates;
  39. LLTrans::map_t LLTrans::sUIStringTemplates;
  40. //static
  41. void LLTrans::parseStrings(const std::string& xml_filename,
  42. map_t& templates)
  43. {
  44. LLXMLNodePtr root;
  45. bool success = LLUICtrlFactory::getLayeredXMLNode(xml_filename, root);
  46. if (!success || root.isNull() || !root->hasName("strings"))
  47. {
  48. llerrs << "Problem reading strings: " << xml_filename << llendl;
  49. }
  50. for (LLXMLNode* string = root->getFirstChild(); string != NULL;
  51. string = string->getNextSibling())
  52. {
  53. if (!string->hasName("string"))
  54. {
  55. continue;
  56. }
  57. std::string string_name;
  58. if (!string->getAttributeString("name", string_name))
  59. {
  60. llwarns << "Unable to parse string with no name" << llendl;
  61. continue;
  62. }
  63. LLTransTemplate xml_template(string_name, string->getTextContents());
  64. templates[xml_template.mName] = xml_template;
  65. }
  66. }
  67. //static
  68. void LLTrans::init()
  69. {
  70. parseStrings("ui_strings.xml", sUIStringTemplates);
  71. parseStrings("strings.xml", sStringTemplates);
  72. }
  73. //static
  74. bool LLTrans::hasString(const std::string& xml_desc)
  75. {
  76. return sStringTemplates.find(xml_desc) != sStringTemplates.end();
  77. }
  78. //static
  79. bool LLTrans::findString(const std::string& xml_desc, map_t::const_iterator& it,
  80. bool ui_string)
  81. {
  82. const map_t& templates = ui_string ? sUIStringTemplates : sStringTemplates;
  83. it = templates.find(xml_desc);
  84. if (it == templates.end())
  85. {
  86. LLSD args;
  87. args["STRING_NAME"] = xml_desc;
  88. llwarns_once << "Missing String in "
  89. << (ui_string ? "ui_strings" : "strings") << ".xml: "
  90. << xml_desc << llendl;
  91. gNotifications.add("MissingString", args);
  92. return false;
  93. }
  94. return true;
  95. }
  96. //static
  97. std::string LLTrans::getString(const std::string& xml_desc,
  98. const LLStringUtil::format_map_t& args)
  99. {
  100. map_t::const_iterator iter;
  101. if (!findString(xml_desc, iter))
  102. {
  103. return xml_desc;
  104. }
  105. std::string text = iter->second.mText;
  106. LLStringUtil::format(text, args);
  107. LL_DEBUGS("GetStringTrans") << "Translating '" << xml_desc << "': " << text
  108. << LL_ENDL;
  109. return text;
  110. }
  111. //static
  112. std::string LLTrans::getString(const std::string& xml_desc,
  113. const LLSD& msg_args)
  114. {
  115. map_t::const_iterator iter;
  116. if (!findString(xml_desc, iter))
  117. {
  118. return xml_desc;
  119. }
  120. std::string text = iter->second.mText;
  121. LLStringUtil::format(text, msg_args);
  122. LL_DEBUGS("GetStringTrans") << "Translating '" << xml_desc << "': " << text
  123. << LL_ENDL;
  124. return text;
  125. }
  126. //static
  127. const std::string& LLTrans::getUIString(const std::string& xml_desc)
  128. {
  129. map_t::const_iterator iter;
  130. return findString(xml_desc, iter, true) ? iter->second.mText : xml_desc;
  131. }