editor_translation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 "core/string/translation_server.h"
  35. #include "editor/doc_translations.gen.h"
  36. #include "editor/editor_translations.gen.h"
  37. #include "editor/extractable_translations.gen.h"
  38. #include "editor/property_translations.gen.h"
  39. Vector<String> get_editor_locales() {
  40. Vector<String> locales;
  41. EditorTranslationList *etl = _editor_translations;
  42. while (etl->data) {
  43. const String &locale = etl->lang;
  44. locales.push_back(locale);
  45. etl++;
  46. }
  47. return locales;
  48. }
  49. void load_editor_translations(const String &p_locale) {
  50. const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain("godot.editor");
  51. EditorTranslationList *etl = _editor_translations;
  52. while (etl->data) {
  53. if (etl->lang == p_locale) {
  54. Vector<uint8_t> data;
  55. data.resize(etl->uncomp_size);
  56. int ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
  57. ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
  58. Ref<FileAccessMemory> fa;
  59. fa.instantiate();
  60. fa->open_custom(data.ptr(), data.size());
  61. Ref<Translation> tr = TranslationLoaderPO::load_translation(fa);
  62. if (tr.is_valid()) {
  63. tr->set_locale(etl->lang);
  64. domain->add_translation(tr);
  65. break;
  66. }
  67. }
  68. etl++;
  69. }
  70. }
  71. void load_property_translations(const String &p_locale) {
  72. const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain("godot.properties");
  73. PropertyTranslationList *etl = _property_translations;
  74. while (etl->data) {
  75. if (etl->lang == p_locale) {
  76. Vector<uint8_t> data;
  77. data.resize(etl->uncomp_size);
  78. int ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
  79. ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
  80. Ref<FileAccessMemory> fa;
  81. fa.instantiate();
  82. fa->open_custom(data.ptr(), data.size());
  83. Ref<Translation> tr = TranslationLoaderPO::load_translation(fa);
  84. if (tr.is_valid()) {
  85. tr->set_locale(etl->lang);
  86. domain->add_translation(tr);
  87. break;
  88. }
  89. }
  90. etl++;
  91. }
  92. }
  93. void load_doc_translations(const String &p_locale) {
  94. const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain("godot.documentation");
  95. DocTranslationList *dtl = _doc_translations;
  96. while (dtl->data) {
  97. if (dtl->lang == p_locale) {
  98. Vector<uint8_t> data;
  99. data.resize(dtl->uncomp_size);
  100. int ret = Compression::decompress(data.ptrw(), dtl->uncomp_size, dtl->data, dtl->comp_size, Compression::MODE_DEFLATE);
  101. ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
  102. Ref<FileAccessMemory> fa;
  103. fa.instantiate();
  104. fa->open_custom(data.ptr(), data.size());
  105. Ref<Translation> tr = TranslationLoaderPO::load_translation(fa);
  106. if (tr.is_valid()) {
  107. tr->set_locale(dtl->lang);
  108. domain->add_translation(tr);
  109. break;
  110. }
  111. }
  112. dtl++;
  113. }
  114. }
  115. void load_extractable_translations(const String &p_locale) {
  116. const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain("godot.editor");
  117. ExtractableTranslationList *etl = _extractable_translations;
  118. while (etl->data) {
  119. if (etl->lang == p_locale) {
  120. Vector<uint8_t> data;
  121. data.resize(etl->uncomp_size);
  122. int ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
  123. ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");
  124. Ref<FileAccessMemory> fa;
  125. fa.instantiate();
  126. fa->open_custom(data.ptr(), data.size());
  127. Ref<Translation> tr = TranslationLoaderPO::load_translation(fa);
  128. if (tr.is_valid()) {
  129. tr->set_locale(etl->lang);
  130. domain->add_translation(tr);
  131. break;
  132. }
  133. }
  134. etl++;
  135. }
  136. }
  137. Vector<Vector<String>> get_extractable_message_list() {
  138. ExtractableTranslationList *etl = _extractable_translations;
  139. Vector<Vector<String>> list;
  140. while (etl->data) {
  141. if (strcmp(etl->lang, "source")) {
  142. etl++;
  143. continue;
  144. }
  145. Vector<uint8_t> data;
  146. data.resize(etl->uncomp_size);
  147. int ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
  148. ERR_FAIL_COND_V_MSG(ret == -1, list, "Compressed file is corrupt.");
  149. Ref<FileAccessMemory> fa;
  150. fa.instantiate();
  151. fa->open_custom(data.ptr(), data.size());
  152. // Taken from TranslationLoaderPO, modified to work specifically with POTs.
  153. {
  154. const String path = fa->get_path();
  155. fa->seek(0);
  156. enum Status {
  157. STATUS_NONE,
  158. STATUS_READING_ID,
  159. STATUS_READING_STRING,
  160. STATUS_READING_CONTEXT,
  161. STATUS_READING_PLURAL,
  162. };
  163. Status status = STATUS_NONE;
  164. String msg_id;
  165. String msg_id_plural;
  166. String msg_context;
  167. int line = 1;
  168. bool entered_context = false;
  169. bool is_eof = false;
  170. while (!is_eof) {
  171. String l = fa->get_line().strip_edges();
  172. is_eof = fa->eof_reached();
  173. // If we reached last line and it's not a content line, break, otherwise let processing that last loop.
  174. if (is_eof && l.is_empty()) {
  175. if (status == STATUS_READING_ID || status == STATUS_READING_CONTEXT || status == STATUS_READING_PLURAL) {
  176. ERR_FAIL_V_MSG(Vector<Vector<String>>(), "Unexpected EOF while reading POT file at: " + path + ":" + itos(line));
  177. } else {
  178. break;
  179. }
  180. }
  181. if (l.begins_with("msgctxt")) {
  182. ERR_FAIL_COND_V_MSG(status != STATUS_READING_STRING && status != STATUS_READING_PLURAL, Vector<Vector<String>>(),
  183. "Unexpected 'msgctxt', was expecting 'msgid_plural' or 'msgstr' before 'msgctxt' while parsing: " + path + ":" + itos(line));
  184. // In POT files, "msgctxt" appears before "msgid". If we encounter a "msgctxt", we add what we have read
  185. // and set "entered_context" to true to prevent adding twice.
  186. if (!msg_id.is_empty()) {
  187. Vector<String> msgs;
  188. msgs.push_back(msg_id);
  189. msgs.push_back(msg_context);
  190. msgs.push_back(msg_id_plural);
  191. list.push_back(msgs);
  192. }
  193. msg_context = "";
  194. l = l.substr(7, l.length()).strip_edges();
  195. status = STATUS_READING_CONTEXT;
  196. entered_context = true;
  197. }
  198. if (l.begins_with("msgid_plural")) {
  199. if (status != STATUS_READING_ID) {
  200. ERR_FAIL_V_MSG(Vector<Vector<String>>(), "Unexpected 'msgid_plural', was expecting 'msgid' before 'msgid_plural' while parsing: " + path + ":" + itos(line));
  201. }
  202. l = l.substr(12, l.length()).strip_edges();
  203. status = STATUS_READING_PLURAL;
  204. } else if (l.begins_with("msgid")) {
  205. ERR_FAIL_COND_V_MSG(status == STATUS_READING_ID, Vector<Vector<String>>(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line));
  206. if (!msg_id.is_empty() && !entered_context) {
  207. Vector<String> msgs;
  208. msgs.push_back(msg_id);
  209. msgs.push_back(msg_context);
  210. msgs.push_back(msg_id_plural);
  211. list.push_back(msgs);
  212. }
  213. l = l.substr(5, l.length()).strip_edges();
  214. status = STATUS_READING_ID;
  215. // If we did not encounter msgctxt, we reset context to empty to reset it.
  216. if (!entered_context) {
  217. msg_context = "";
  218. }
  219. msg_id = "";
  220. msg_id_plural = "";
  221. entered_context = false;
  222. }
  223. if (l.begins_with("msgstr[")) {
  224. ERR_FAIL_COND_V_MSG(status != STATUS_READING_PLURAL, Vector<Vector<String>>(),
  225. "Unexpected 'msgstr[]', was expecting 'msgid_plural' before 'msgstr[]' while parsing: " + path + ":" + itos(line));
  226. l = l.substr(9, l.length()).strip_edges();
  227. } else if (l.begins_with("msgstr")) {
  228. ERR_FAIL_COND_V_MSG(status != STATUS_READING_ID, Vector<Vector<String>>(),
  229. "Unexpected 'msgstr', was expecting 'msgid' before 'msgstr' while parsing: " + path + ":" + itos(line));
  230. l = l.substr(6, l.length()).strip_edges();
  231. status = STATUS_READING_STRING;
  232. }
  233. if (l.is_empty() || l.begins_with("#")) {
  234. line++;
  235. continue; // Nothing to read or comment.
  236. }
  237. ERR_FAIL_COND_V_MSG(!l.begins_with("\"") || status == STATUS_NONE, Vector<Vector<String>>(), "Invalid line '" + l + "' while parsing: " + path + ":" + itos(line));
  238. l = l.substr(1, l.length());
  239. // Find final quote, ignoring escaped ones (\").
  240. // The escape_next logic is necessary to properly parse things like \\"
  241. // where the backslash is the one being escaped, not the quote.
  242. int end_pos = -1;
  243. bool escape_next = false;
  244. for (int i = 0; i < l.length(); i++) {
  245. if (l[i] == '\\' && !escape_next) {
  246. escape_next = true;
  247. continue;
  248. }
  249. if (l[i] == '"' && !escape_next) {
  250. end_pos = i;
  251. break;
  252. }
  253. escape_next = false;
  254. }
  255. ERR_FAIL_COND_V_MSG(end_pos == -1, Vector<Vector<String>>(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line));
  256. l = l.substr(0, end_pos);
  257. l = l.c_unescape();
  258. if (status == STATUS_READING_ID) {
  259. msg_id += l;
  260. } else if (status == STATUS_READING_CONTEXT) {
  261. msg_context += l;
  262. } else if (status == STATUS_READING_PLURAL) {
  263. msg_id_plural += l;
  264. }
  265. line++;
  266. }
  267. }
  268. etl++;
  269. }
  270. return list;
  271. }