translation.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Minetest
  3. Copyright (C) 2017 Nore, Nathanaël Courant <nore@mesecons.net>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "translation.h"
  17. #include "log.h"
  18. #include "util/string.h"
  19. static Translations main_translations;
  20. Translations *g_translations = &main_translations;
  21. Translations::~Translations()
  22. {
  23. clear();
  24. }
  25. void Translations::clear()
  26. {
  27. m_translations.clear();
  28. }
  29. const std::wstring &Translations::getTranslation(
  30. const std::wstring &textdomain, const std::wstring &s)
  31. {
  32. std::wstring key = textdomain + L"|" + s;
  33. try {
  34. return m_translations.at(key);
  35. } catch (const std::out_of_range &) {
  36. warningstream << "Translations: can't find translation for string \""
  37. << wide_to_utf8(s) << "\" in textdomain \""
  38. << wide_to_utf8(textdomain) << "\"" << std::endl;
  39. // Silence that warning in the future
  40. m_translations[key] = s;
  41. return s;
  42. }
  43. }
  44. void Translations::loadTranslation(const std::string &data)
  45. {
  46. std::istringstream is(data);
  47. std::wstring textdomain;
  48. std::string line;
  49. while (is.good()) {
  50. std::getline(is, line);
  51. if (str_starts_with(line, "# textdomain:")) {
  52. textdomain = utf8_to_wide(trim(str_split(line, ':')[1]));
  53. }
  54. if (line.empty() || line[0] == '#')
  55. continue;
  56. std::wstring wline = utf8_to_wide(line);
  57. if (wline.empty())
  58. continue;
  59. // Read line
  60. // '=' marks the key-value pair, but may be escaped by an '@'.
  61. // '\n' may also be escaped by '@'.
  62. // All other escapes are preserved.
  63. size_t i = 0;
  64. std::wostringstream word1, word2;
  65. while (i < wline.length() && wline[i] != L'=') {
  66. if (wline[i] == L'@') {
  67. if (i + 1 < wline.length()) {
  68. if (wline[i + 1] == L'=') {
  69. word1.put(L'=');
  70. } else if (wline[i + 1] == L'n') {
  71. word1.put(L'\n');
  72. } else {
  73. word1.put(L'@');
  74. word1.put(wline[i + 1]);
  75. }
  76. i += 2;
  77. } else {
  78. // End of line, go to the next one.
  79. word1.put(L'\n');
  80. if (!is.good()) {
  81. break;
  82. }
  83. i = 0;
  84. std::getline(is, line);
  85. wline = utf8_to_wide(line);
  86. }
  87. } else {
  88. word1.put(wline[i]);
  89. i++;
  90. }
  91. }
  92. if (i == wline.length()) {
  93. errorstream << "Malformed translation line \"" << line << "\""
  94. << std::endl;
  95. continue;
  96. }
  97. i++;
  98. while (i < wline.length()) {
  99. if (wline[i] == L'@') {
  100. if (i + 1 < wline.length()) {
  101. if (wline[i + 1] == L'=') {
  102. word2.put(L'=');
  103. } else if (wline[i + 1] == L'n') {
  104. word2.put(L'\n');
  105. } else {
  106. word2.put(L'@');
  107. word2.put(wline[i + 1]);
  108. }
  109. i += 2;
  110. } else {
  111. // End of line, go to the next one.
  112. word2.put(L'\n');
  113. if (!is.good()) {
  114. break;
  115. }
  116. i = 0;
  117. std::getline(is, line);
  118. wline = utf8_to_wide(line);
  119. }
  120. } else {
  121. word2.put(wline[i]);
  122. i++;
  123. }
  124. }
  125. std::wstring oword1 = word1.str(), oword2 = word2.str();
  126. if (oword2.empty()) {
  127. oword2 = oword1;
  128. errorstream << "Ignoring empty translation for \""
  129. << wide_to_utf8(oword1) << "\"" << std::endl;
  130. }
  131. m_translations[textdomain + L"|" + oword1] = oword2;
  132. }
  133. }