translation_loader_po.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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, const String &p_path) {
  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_EXPLAIN(p_path + ":" + itos(line) + " Unexpected EOF while reading 'msgid' at file: ");
  58. ERR_FAIL_V(RES());
  59. } else {
  60. break;
  61. }
  62. }
  63. if (l.begins_with("msgid")) {
  64. if (status == STATUS_READING_ID) {
  65. memdelete(f);
  66. ERR_EXPLAIN(p_path + ":" + itos(line) + " Unexpected 'msgid', was expecting 'msgstr' while parsing: ");
  67. ERR_FAIL_V(RES());
  68. }
  69. if (msg_id != "") {
  70. if (!skip_this)
  71. translation->add_message(msg_id, msg_str);
  72. } else if (config == "")
  73. config = msg_str;
  74. l = l.substr(5, l.length()).strip_edges();
  75. status = STATUS_READING_ID;
  76. msg_id = "";
  77. msg_str = "";
  78. skip_this = skip_next;
  79. skip_next = false;
  80. }
  81. if (l.begins_with("msgstr")) {
  82. if (status != STATUS_READING_ID) {
  83. memdelete(f);
  84. ERR_EXPLAIN(p_path + ":" + itos(line) + " Unexpected 'msgstr', was expecting 'msgid' while parsing: ");
  85. ERR_FAIL_V(RES());
  86. }
  87. l = l.substr(6, l.length()).strip_edges();
  88. status = STATUS_READING_STRING;
  89. }
  90. if (l == "" || l.begins_with("#")) {
  91. if (l.find("fuzzy") != -1) {
  92. skip_next = true;
  93. }
  94. line++;
  95. continue; //nothing to read or comment
  96. }
  97. if (!l.begins_with("\"") || status == STATUS_NONE) {
  98. //not a string? failure!
  99. ERR_EXPLAIN(p_path + ":" + itos(line) + " Invalid line '" + l + "' while parsing: ");
  100. ERR_FAIL_V(RES());
  101. }
  102. l = l.substr(1, l.length());
  103. //find final quote
  104. int end_pos = -1;
  105. for (int i = 0; i < l.length(); i++) {
  106. if (l[i] == '"' && (i == 0 || l[i - 1] != '\\')) {
  107. end_pos = i;
  108. break;
  109. }
  110. }
  111. if (end_pos == -1) {
  112. ERR_EXPLAIN(p_path + ":" + itos(line) + " Expected '\"' at end of message while parsing file: ");
  113. ERR_FAIL_V(RES());
  114. }
  115. l = l.substr(0, end_pos);
  116. l = l.c_unescape();
  117. if (status == STATUS_READING_ID)
  118. msg_id += l;
  119. else
  120. msg_str += l;
  121. line++;
  122. }
  123. f->close();
  124. memdelete(f);
  125. if (status == STATUS_READING_STRING) {
  126. if (msg_id != "") {
  127. if (!skip_this)
  128. translation->add_message(msg_id, msg_str);
  129. } else if (config == "")
  130. config = msg_str;
  131. }
  132. if (config == "") {
  133. ERR_EXPLAIN("No config found in file: " + p_path);
  134. ERR_FAIL_V(RES());
  135. }
  136. Vector<String> configs = config.split("\n");
  137. for (int i = 0; i < configs.size(); i++) {
  138. String c = configs[i].strip_edges();
  139. int p = c.find(":");
  140. if (p == -1)
  141. continue;
  142. String prop = c.substr(0, p).strip_edges();
  143. String value = c.substr(p + 1, c.length()).strip_edges();
  144. if (prop == "X-Language" || prop == "Language") {
  145. translation->set_locale(value);
  146. }
  147. }
  148. if (r_error)
  149. *r_error = OK;
  150. return translation;
  151. }
  152. RES TranslationLoaderPO::load(const String &p_path, const String &p_original_path, Error *r_error) {
  153. if (r_error)
  154. *r_error = ERR_CANT_OPEN;
  155. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  156. ERR_FAIL_COND_V(!f, RES());
  157. return load_translation(f, r_error);
  158. }
  159. void TranslationLoaderPO::get_recognized_extensions(List<String> *p_extensions) const {
  160. p_extensions->push_back("po");
  161. //p_extensions->push_back("mo"); //mo in the future...
  162. }
  163. bool TranslationLoaderPO::handles_type(const String &p_type) const {
  164. return (p_type == "Translation");
  165. }
  166. String TranslationLoaderPO::get_resource_type(const String &p_path) const {
  167. if (p_path.get_extension().to_lower() == "po")
  168. return "Translation";
  169. return "";
  170. }
  171. TranslationLoaderPO::TranslationLoaderPO() {
  172. }