translation_loader_po.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*************************************************************************/
  2. /* translation_loader_po.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 "translation_loader_po.h"
  31. #include "core/os/file_access.h"
  32. #include "core/string/translation.h"
  33. #include "core/string/translation_po.h"
  34. RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
  35. enum Status {
  36. STATUS_NONE,
  37. STATUS_READING_ID,
  38. STATUS_READING_STRING,
  39. STATUS_READING_CONTEXT,
  40. STATUS_READING_PLURAL,
  41. };
  42. Status status = STATUS_NONE;
  43. String msg_id;
  44. String msg_str;
  45. String msg_context;
  46. Vector<String> msgs_plural;
  47. String config;
  48. if (r_error) {
  49. *r_error = ERR_FILE_CORRUPT;
  50. }
  51. Ref<TranslationPO> translation = Ref<TranslationPO>(memnew(TranslationPO));
  52. int line = 1;
  53. int plural_forms = 0;
  54. int plural_index = -1;
  55. bool entered_context = false;
  56. bool skip_this = false;
  57. bool skip_next = false;
  58. bool is_eof = false;
  59. const String path = f->get_path();
  60. while (!is_eof) {
  61. String l = f->get_line().strip_edges();
  62. is_eof = f->eof_reached();
  63. // If we reached last line and it's not a content line, break, otherwise let processing that last loop
  64. if (is_eof && l.empty()) {
  65. if (status == STATUS_READING_ID || status == STATUS_READING_CONTEXT || (status == STATUS_READING_PLURAL && plural_index != plural_forms - 1)) {
  66. memdelete(f);
  67. ERR_FAIL_V_MSG(RES(), "Unexpected EOF while reading PO file at: " + path + ":" + itos(line));
  68. } else {
  69. break;
  70. }
  71. }
  72. if (l.begins_with("msgctxt")) {
  73. if (status != STATUS_READING_STRING && status != STATUS_READING_PLURAL) {
  74. memdelete(f);
  75. ERR_FAIL_V_MSG(RES(), "Unexpected 'msgctxt', was expecting 'msgid_plural' or 'msgstr' before 'msgctxt' while parsing: " + path + ":" + itos(line));
  76. }
  77. // In PO file, "msgctxt" appears before "msgid". If we encounter a "msgctxt", we add what we have read
  78. // and set "entered_context" to true to prevent adding twice.
  79. if (!skip_this && msg_id != "") {
  80. if (status == STATUS_READING_STRING) {
  81. translation->add_message(msg_id, msg_str, msg_context);
  82. } else if (status == STATUS_READING_PLURAL) {
  83. if (plural_index != plural_forms - 1) {
  84. memdelete(f);
  85. ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line));
  86. }
  87. translation->add_plural_message(msg_id, msgs_plural, msg_context);
  88. }
  89. }
  90. msg_context = "";
  91. l = l.substr(7, l.length()).strip_edges();
  92. status = STATUS_READING_CONTEXT;
  93. entered_context = true;
  94. }
  95. if (l.begins_with("msgid_plural")) {
  96. if (plural_forms == 0) {
  97. memdelete(f);
  98. ERR_FAIL_V_MSG(RES(), "PO file uses 'msgid_plural' but 'Plural-Forms' is invalid or missing in header: " + path + ":" + itos(line));
  99. } else if (status != STATUS_READING_ID) {
  100. memdelete(f);
  101. ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid_plural', was expecting 'msgid' before 'msgid_plural' while parsing: " + path + ":" + itos(line));
  102. }
  103. // We don't record the message in "msgid_plural" itself as tr_n(), TTRN(), RTRN() interfaces provide the plural string already.
  104. // We just have to reset variables related to plurals for "msgstr[]" later on.
  105. l = l.substr(12, l.length()).strip_edges();
  106. plural_index = -1;
  107. msgs_plural.clear();
  108. msgs_plural.resize(plural_forms);
  109. status = STATUS_READING_PLURAL;
  110. } else if (l.begins_with("msgid")) {
  111. if (status == STATUS_READING_ID) {
  112. memdelete(f);
  113. ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line));
  114. }
  115. if (msg_id != "") {
  116. if (!skip_this && !entered_context) {
  117. if (status == STATUS_READING_STRING) {
  118. translation->add_message(msg_id, msg_str, msg_context);
  119. } else if (status == STATUS_READING_PLURAL) {
  120. if (plural_index != plural_forms - 1) {
  121. memdelete(f);
  122. ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line));
  123. }
  124. translation->add_plural_message(msg_id, msgs_plural, msg_context);
  125. }
  126. }
  127. } else if (config == "") {
  128. config = msg_str;
  129. // Record plural rule.
  130. int p_start = config.find("Plural-Forms");
  131. if (p_start != -1) {
  132. int p_end = config.find("\n", p_start);
  133. translation->set_plural_rule(config.substr(p_start, p_end - p_start));
  134. plural_forms = translation->get_plural_forms();
  135. }
  136. }
  137. l = l.substr(5, l.length()).strip_edges();
  138. status = STATUS_READING_ID;
  139. // If we did not encounter msgctxt, we reset context to empty to reset it.
  140. if (!entered_context) {
  141. msg_context = "";
  142. }
  143. msg_id = "";
  144. msg_str = "";
  145. skip_this = skip_next;
  146. skip_next = false;
  147. entered_context = false;
  148. }
  149. if (l.begins_with("msgstr[")) {
  150. if (status != STATUS_READING_PLURAL) {
  151. memdelete(f);
  152. ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr[]', was expecting 'msgid_plural' before 'msgstr[]' while parsing: " + path + ":" + itos(line));
  153. }
  154. plural_index++; // Increment to add to the next slot in vector msgs_plural.
  155. l = l.substr(9, l.length()).strip_edges();
  156. } else if (l.begins_with("msgstr")) {
  157. if (status != STATUS_READING_ID) {
  158. memdelete(f);
  159. ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr', was expecting 'msgid' before 'msgstr' while parsing: " + path + ":" + itos(line));
  160. }
  161. l = l.substr(6, l.length()).strip_edges();
  162. status = STATUS_READING_STRING;
  163. }
  164. if (l == "" || l.begins_with("#")) {
  165. if (l.find("fuzzy") != -1) {
  166. skip_next = true;
  167. }
  168. line++;
  169. continue; // Nothing to read or comment.
  170. }
  171. if (!l.begins_with("\"") || status == STATUS_NONE) {
  172. memdelete(f);
  173. ERR_FAIL_V_MSG(RES(), "Invalid line '" + l + "' while parsing: " + path + ":" + itos(line));
  174. }
  175. l = l.substr(1, l.length());
  176. // Find final quote, ignoring escaped ones (\").
  177. // The escape_next logic is necessary to properly parse things like \\"
  178. // where the blackslash is the one being escaped, not the quote.
  179. int end_pos = -1;
  180. bool escape_next = false;
  181. for (int i = 0; i < l.length(); i++) {
  182. if (l[i] == '\\' && !escape_next) {
  183. escape_next = true;
  184. continue;
  185. }
  186. if (l[i] == '"' && !escape_next) {
  187. end_pos = i;
  188. break;
  189. }
  190. escape_next = false;
  191. }
  192. if (end_pos == -1) {
  193. memdelete(f);
  194. ERR_FAIL_V_MSG(RES(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line));
  195. }
  196. l = l.substr(0, end_pos);
  197. l = l.c_unescape();
  198. if (status == STATUS_READING_ID) {
  199. msg_id += l;
  200. } else if (status == STATUS_READING_STRING) {
  201. msg_str += l;
  202. } else if (status == STATUS_READING_CONTEXT) {
  203. msg_context += l;
  204. } else if (status == STATUS_READING_PLURAL && plural_index >= 0) {
  205. msgs_plural.write[plural_index] = msgs_plural[plural_index] + l;
  206. }
  207. line++;
  208. }
  209. memdelete(f);
  210. // Add the last set of data from last iteration.
  211. if (status == STATUS_READING_STRING) {
  212. if (msg_id != "") {
  213. if (!skip_this) {
  214. translation->add_message(msg_id, msg_str, msg_context);
  215. }
  216. } else if (config == "") {
  217. config = msg_str;
  218. }
  219. } else if (status == STATUS_READING_PLURAL) {
  220. if (!skip_this && msg_id != "") {
  221. if (plural_index != plural_forms - 1) {
  222. memdelete(f);
  223. ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line));
  224. }
  225. translation->add_plural_message(msg_id, msgs_plural, msg_context);
  226. }
  227. }
  228. ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + path + ".");
  229. Vector<String> configs = config.split("\n");
  230. for (int i = 0; i < configs.size(); i++) {
  231. String c = configs[i].strip_edges();
  232. int p = c.find(":");
  233. if (p == -1) {
  234. continue;
  235. }
  236. String prop = c.substr(0, p).strip_edges();
  237. String value = c.substr(p + 1, c.length()).strip_edges();
  238. if (prop == "X-Language" || prop == "Language") {
  239. translation->set_locale(value);
  240. }
  241. }
  242. if (r_error) {
  243. *r_error = OK;
  244. }
  245. return translation;
  246. }
  247. RES TranslationLoaderPO::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, bool p_no_cache) {
  248. if (r_error) {
  249. *r_error = ERR_CANT_OPEN;
  250. }
  251. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  252. ERR_FAIL_COND_V_MSG(!f, RES(), "Cannot open file '" + p_path + "'.");
  253. return load_translation(f, r_error);
  254. }
  255. void TranslationLoaderPO::get_recognized_extensions(List<String> *p_extensions) const {
  256. p_extensions->push_back("po");
  257. }
  258. bool TranslationLoaderPO::handles_type(const String &p_type) const {
  259. return (p_type == "Translation");
  260. }
  261. String TranslationLoaderPO::get_resource_type(const String &p_path) const {
  262. if (p_path.get_extension().to_lower() == "po") {
  263. return "Translation";
  264. }
  265. return "";
  266. }