editor_translation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**************************************************************************/
  2. /* editor_translation.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "editor/editor_translation.h"
  31. #include "core/io/compression.h"
  32. #include "core/io/file_access_memory.h"
  33. #include "core/io/translation_loader_po.h"
  34. #include "editor/doc_translations.gen.h"
  35. #include "editor/editor_translations.gen.h"
  36. #include "editor/extractable_translations.gen.h"
  37. #include "editor/property_translations.gen.h"
  38. Vector<String> get_editor_locales() {
  39. Vector<String> locales;
  40. EditorTranslationList *etl = _editor_translations;
  41. while (etl->data) {
  42. const String &locale = etl->lang;
  43. locales.push_back(locale);
  44. etl++;
  45. }
  46. return locales;
  47. }
  48. void load_editor_translations(const String &p_locale) {
  49. EditorTranslationList *etl = _editor_translations;
  50. while (etl->data) {
  51. if (etl->lang == p_locale) {
  52. Vector<uint8_t> data;
  53. data.resize(etl->uncomp_size);
  54. int ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
  55. ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
  56. Ref<FileAccessMemory> fa;
  57. fa.instantiate();
  58. fa->open_custom(data.ptr(), data.size());
  59. Ref<Translation> tr = TranslationLoaderPO::load_translation(fa);
  60. if (tr.is_valid()) {
  61. tr->set_locale(etl->lang);
  62. TranslationServer::get_singleton()->set_tool_translation(tr);
  63. break;
  64. }
  65. }
  66. etl++;
  67. }
  68. }
  69. void load_property_translations(const String &p_locale) {
  70. PropertyTranslationList *etl = _property_translations;
  71. while (etl->data) {
  72. if (etl->lang == p_locale) {
  73. Vector<uint8_t> data;
  74. data.resize(etl->uncomp_size);
  75. int ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
  76. ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
  77. Ref<FileAccessMemory> fa;
  78. fa.instantiate();
  79. fa->open_custom(data.ptr(), data.size());
  80. Ref<Translation> tr = TranslationLoaderPO::load_translation(fa);
  81. if (tr.is_valid()) {
  82. tr->set_locale(etl->lang);
  83. TranslationServer::get_singleton()->set_property_translation(tr);
  84. break;
  85. }
  86. }
  87. etl++;
  88. }
  89. }
  90. void load_doc_translations(const String &p_locale) {
  91. DocTranslationList *dtl = _doc_translations;
  92. while (dtl->data) {
  93. if (dtl->lang == p_locale) {
  94. Vector<uint8_t> data;
  95. data.resize(dtl->uncomp_size);
  96. int ret = Compression::decompress(data.ptrw(), dtl->uncomp_size, dtl->data, dtl->comp_size, Compression::MODE_DEFLATE);
  97. ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
  98. Ref<FileAccessMemory> fa;
  99. fa.instantiate();
  100. fa->open_custom(data.ptr(), data.size());
  101. Ref<Translation> tr = TranslationLoaderPO::load_translation(fa);
  102. if (tr.is_valid()) {
  103. tr->set_locale(dtl->lang);
  104. TranslationServer::get_singleton()->set_doc_translation(tr);
  105. break;
  106. }
  107. }
  108. dtl++;
  109. }
  110. }
  111. void load_extractable_translations(const String &p_locale) {
  112. ExtractableTranslationList *etl = _extractable_translations;
  113. while (etl->data) {
  114. if (etl->lang == p_locale) {
  115. Vector<uint8_t> data;
  116. data.resize(etl->uncomp_size);
  117. int ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
  118. ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
  119. Ref<FileAccessMemory> fa;
  120. fa.instantiate();
  121. fa->open_custom(data.ptr(), data.size());
  122. Ref<Translation> tr = TranslationLoaderPO::load_translation(fa);
  123. if (tr.is_valid()) {
  124. tr->set_locale(etl->lang);
  125. TranslationServer::get_singleton()->set_extractable_translation(tr);
  126. break;
  127. }
  128. }
  129. etl++;
  130. }
  131. }
  132. Vector<Vector<String>> get_extractable_message_list() {
  133. ExtractableTranslationList *etl = _extractable_translations;
  134. Vector<Vector<String>> list;
  135. while (etl->data) {
  136. if (strcmp(etl->lang, "source")) {
  137. etl++;
  138. continue;
  139. }
  140. Vector<uint8_t> data;
  141. data.resize(etl->uncomp_size);
  142. int ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
  143. ERR_FAIL_COND_V_MSG(ret == -1, list, "Compressed file is corrupt.");
  144. Ref<FileAccessMemory> fa;
  145. fa.instantiate();
  146. fa->open_custom(data.ptr(), data.size());
  147. // Taken from TranslationLoaderPO, modified to work specifically with POTs.
  148. {
  149. const String path = fa->get_path();
  150. fa->seek(0);
  151. enum Status {
  152. STATUS_NONE,
  153. STATUS_READING_ID,
  154. STATUS_READING_STRING,
  155. STATUS_READING_CONTEXT,
  156. STATUS_READING_PLURAL,
  157. };
  158. Status status = STATUS_NONE;
  159. String msg_id;
  160. String msg_id_plural;
  161. String msg_context;
  162. int line = 1;
  163. bool entered_context = false;
  164. bool is_eof = false;
  165. while (!is_eof) {
  166. String l = fa->get_line().strip_edges();
  167. is_eof = fa->eof_reached();
  168. // If we reached last line and it's not a content line, break, otherwise let processing that last loop.
  169. if (is_eof && l.is_empty()) {
  170. if (status == STATUS_READING_ID || status == STATUS_READING_CONTEXT || status == STATUS_READING_PLURAL) {
  171. ERR_FAIL_V_MSG(Vector<Vector<String>>(), "Unexpected EOF while reading POT file at: " + path + ":" + itos(line));
  172. } else {
  173. break;
  174. }
  175. }
  176. if (l.begins_with("msgctxt")) {
  177. ERR_FAIL_COND_V_MSG(status != STATUS_READING_STRING && status != STATUS_READING_PLURAL, Vector<Vector<String>>(),
  178. "Unexpected 'msgctxt', was expecting 'msgid_plural' or 'msgstr' before 'msgctxt' while parsing: " + path + ":" + itos(line));
  179. // In POT files, "msgctxt" appears before "msgid". If we encounter a "msgctxt", we add what we have read
  180. // and set "entered_context" to true to prevent adding twice.
  181. if (!msg_id.is_empty()) {
  182. Vector<String> msgs;
  183. msgs.push_back(msg_id);
  184. msgs.push_back(msg_context);
  185. msgs.push_back(msg_id_plural);
  186. list.push_back(msgs);
  187. }
  188. msg_context = "";
  189. l = l.substr(7, l.length()).strip_edges();
  190. status = STATUS_READING_CONTEXT;
  191. entered_context = true;
  192. }
  193. if (l.begins_with("msgid_plural")) {
  194. if (status != STATUS_READING_ID) {
  195. ERR_FAIL_V_MSG(Vector<Vector<String>>(), "Unexpected 'msgid_plural', was expecting 'msgid' before 'msgid_plural' while parsing: " + path + ":" + itos(line));
  196. }
  197. l = l.substr(12, l.length()).strip_edges();
  198. status = STATUS_READING_PLURAL;
  199. } else if (l.begins_with("msgid")) {
  200. ERR_FAIL_COND_V_MSG(status == STATUS_READING_ID, Vector<Vector<String>>(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line));
  201. if (!msg_id.is_empty() && !entered_context) {
  202. Vector<String> msgs;
  203. msgs.push_back(msg_id);
  204. msgs.push_back(msg_context);
  205. msgs.push_back(msg_id_plural);
  206. list.push_back(msgs);
  207. }
  208. l = l.substr(5, l.length()).strip_edges();
  209. status = STATUS_READING_ID;
  210. // If we did not encounter msgctxt, we reset context to empty to reset it.
  211. if (!entered_context) {
  212. msg_context = "";
  213. }
  214. msg_id = "";
  215. msg_id_plural = "";
  216. entered_context = false;
  217. }
  218. if (l.begins_with("msgstr[")) {
  219. ERR_FAIL_COND_V_MSG(status != STATUS_READING_PLURAL, Vector<Vector<String>>(),
  220. "Unexpected 'msgstr[]', was expecting 'msgid_plural' before 'msgstr[]' while parsing: " + path + ":" + itos(line));
  221. l = l.substr(9, l.length()).strip_edges();
  222. } else if (l.begins_with("msgstr")) {
  223. ERR_FAIL_COND_V_MSG(status != STATUS_READING_ID, Vector<Vector<String>>(),
  224. "Unexpected 'msgstr', was expecting 'msgid' before 'msgstr' while parsing: " + path + ":" + itos(line));
  225. l = l.substr(6, l.length()).strip_edges();
  226. status = STATUS_READING_STRING;
  227. }
  228. if (l.is_empty() || l.begins_with("#")) {
  229. line++;
  230. continue; // Nothing to read or comment.
  231. }
  232. ERR_FAIL_COND_V_MSG(!l.begins_with("\"") || status == STATUS_NONE, Vector<Vector<String>>(), "Invalid line '" + l + "' while parsing: " + path + ":" + itos(line));
  233. l = l.substr(1, l.length());
  234. // Find final quote, ignoring escaped ones (\").
  235. // The escape_next logic is necessary to properly parse things like \\"
  236. // where the backslash is the one being escaped, not the quote.
  237. int end_pos = -1;
  238. bool escape_next = false;
  239. for (int i = 0; i < l.length(); i++) {
  240. if (l[i] == '\\' && !escape_next) {
  241. escape_next = true;
  242. continue;
  243. }
  244. if (l[i] == '"' && !escape_next) {
  245. end_pos = i;
  246. break;
  247. }
  248. escape_next = false;
  249. }
  250. ERR_FAIL_COND_V_MSG(end_pos == -1, Vector<Vector<String>>(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line));
  251. l = l.substr(0, end_pos);
  252. l = l.c_unescape();
  253. if (status == STATUS_READING_ID) {
  254. msg_id += l;
  255. } else if (status == STATUS_READING_CONTEXT) {
  256. msg_context += l;
  257. } else if (status == STATUS_READING_PLURAL) {
  258. msg_id_plural += l;
  259. }
  260. line++;
  261. }
  262. }
  263. etl++;
  264. }
  265. return list;
  266. }