translation_loader_po.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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/translation.h"
  33. RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
  34. enum Status {
  35. STATUS_NONE,
  36. STATUS_READING_ID,
  37. STATUS_READING_STRING,
  38. };
  39. Status status = STATUS_NONE;
  40. String msg_id;
  41. String msg_str;
  42. String config;
  43. if (r_error)
  44. *r_error = ERR_FILE_CORRUPT;
  45. Ref<Translation> translation = Ref<Translation>(memnew(Translation));
  46. int line = 1;
  47. bool skip_this = false;
  48. bool skip_next = false;
  49. bool is_eof = false;
  50. while (!is_eof) {
  51. String l = f->get_line().strip_edges();
  52. is_eof = f->eof_reached();
  53. // If we reached last line and it's not a content line, break, otherwise let processing that last loop
  54. if (is_eof && l.empty()) {
  55. if (status == STATUS_READING_ID) {
  56. memdelete(f);
  57. ERR_FAIL_V_MSG(RES(), f->get_path() + ":" + itos(line) + " Unexpected EOF while reading 'msgid' at file: ");
  58. } else {
  59. break;
  60. }
  61. }
  62. if (l.begins_with("msgid")) {
  63. if (status == STATUS_READING_ID) {
  64. memdelete(f);
  65. ERR_FAIL_V_MSG(RES(), f->get_path() + ":" + itos(line) + " Unexpected 'msgid', was expecting 'msgstr' while parsing: ");
  66. }
  67. if (msg_id != "") {
  68. if (!skip_this)
  69. translation->add_message(msg_id, msg_str);
  70. } else if (config == "")
  71. config = msg_str;
  72. l = l.substr(5, l.length()).strip_edges();
  73. status = STATUS_READING_ID;
  74. msg_id = "";
  75. msg_str = "";
  76. skip_this = skip_next;
  77. skip_next = false;
  78. }
  79. if (l.begins_with("msgstr")) {
  80. if (status != STATUS_READING_ID) {
  81. memdelete(f);
  82. ERR_FAIL_V_MSG(RES(), f->get_path() + ":" + itos(line) + " Unexpected 'msgstr', was expecting 'msgid' while parsing: ");
  83. }
  84. l = l.substr(6, l.length()).strip_edges();
  85. status = STATUS_READING_STRING;
  86. }
  87. if (l == "" || l.begins_with("#")) {
  88. if (l.find("fuzzy") != -1) {
  89. skip_next = true;
  90. }
  91. line++;
  92. continue; //nothing to read or comment
  93. }
  94. ERR_FAIL_COND_V_MSG(!l.begins_with("\"") || status == STATUS_NONE, RES(), f->get_path() + ":" + itos(line) + " Invalid line '" + l + "' while parsing: ");
  95. l = l.substr(1, l.length());
  96. // Find final quote, ignoring escaped ones (\").
  97. // The escape_next logic is necessary to properly parse things like \\"
  98. // where the blackslash is the one being escaped, not the quote.
  99. int end_pos = -1;
  100. bool escape_next = false;
  101. for (int i = 0; i < l.length(); i++) {
  102. if (l[i] == '\\' && !escape_next) {
  103. escape_next = true;
  104. continue;
  105. }
  106. if (l[i] == '"' && !escape_next) {
  107. end_pos = i;
  108. break;
  109. }
  110. escape_next = false;
  111. }
  112. ERR_FAIL_COND_V_MSG(end_pos == -1, RES(), f->get_path() + ":" + itos(line) + ": Expected '\"' at end of message while parsing file.");
  113. l = l.substr(0, end_pos);
  114. l = l.c_unescape();
  115. if (status == STATUS_READING_ID)
  116. msg_id += l;
  117. else
  118. msg_str += l;
  119. line++;
  120. }
  121. f->close();
  122. memdelete(f);
  123. if (status == STATUS_READING_STRING) {
  124. if (msg_id != "") {
  125. if (!skip_this)
  126. translation->add_message(msg_id, msg_str);
  127. } else if (config == "")
  128. config = msg_str;
  129. }
  130. ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + f->get_path() + ".");
  131. Vector<String> configs = config.split("\n");
  132. for (int i = 0; i < configs.size(); i++) {
  133. String c = configs[i].strip_edges();
  134. int p = c.find(":");
  135. if (p == -1)
  136. continue;
  137. String prop = c.substr(0, p).strip_edges();
  138. String value = c.substr(p + 1, c.length()).strip_edges();
  139. if (prop == "X-Language" || prop == "Language") {
  140. translation->set_locale(value);
  141. }
  142. }
  143. if (r_error)
  144. *r_error = OK;
  145. return translation;
  146. }
  147. 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) {
  148. if (r_error)
  149. *r_error = ERR_CANT_OPEN;
  150. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  151. ERR_FAIL_COND_V_MSG(!f, RES(), "Cannot open file '" + p_path + "'.");
  152. return load_translation(f, r_error);
  153. }
  154. void TranslationLoaderPO::get_recognized_extensions(List<String> *p_extensions) const {
  155. p_extensions->push_back("po");
  156. //p_extensions->push_back("mo"); //mo in the future...
  157. }
  158. bool TranslationLoaderPO::handles_type(const String &p_type) const {
  159. return (p_type == "Translation");
  160. }
  161. String TranslationLoaderPO::get_resource_type(const String &p_path) const {
  162. if (p_path.get_extension().to_lower() == "po")
  163. return "Translation";
  164. return "";
  165. }
  166. TranslationLoaderPO::TranslationLoaderPO() {
  167. }