translation_loader_po.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #ifdef DEBUG_ENABLED
  47. int line = 1;
  48. #endif
  49. bool skip_this = false;
  50. bool skip_next = false;
  51. bool is_eof = false;
  52. const String path = f->get_path();
  53. while (!is_eof) {
  54. String l = f->get_line().strip_edges();
  55. is_eof = f->eof_reached();
  56. // If we reached last line and it's not a content line, break, otherwise let processing that last loop
  57. if (is_eof && l.empty()) {
  58. if (status == STATUS_READING_ID) {
  59. memdelete(f);
  60. ERR_FAIL_V_MSG(RES(), "Unexpected EOF while reading 'msgid' at: " + path + ":" + itos(line));
  61. } else {
  62. break;
  63. }
  64. }
  65. if (l.begins_with("msgid")) {
  66. if (status == STATUS_READING_ID) {
  67. memdelete(f);
  68. ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line));
  69. }
  70. if (msg_id != "") {
  71. if (!skip_this)
  72. translation->add_message(msg_id, msg_str);
  73. } else if (config == "")
  74. config = msg_str;
  75. l = l.substr(5, l.length()).strip_edges();
  76. status = STATUS_READING_ID;
  77. msg_id = "";
  78. msg_str = "";
  79. skip_this = skip_next;
  80. skip_next = false;
  81. }
  82. if (l.begins_with("msgstr")) {
  83. if (status != STATUS_READING_ID) {
  84. memdelete(f);
  85. ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr', was expecting 'msgid' while parsing: " + path + ":" + itos(line));
  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. #ifdef DEBUG_ENABLED
  95. line++;
  96. #endif
  97. continue; //nothing to read or comment
  98. }
  99. if (!l.begins_with("\"") || status == STATUS_NONE) {
  100. memdelete(f);
  101. ERR_FAIL_V_MSG(RES(), "Invalid line '" + l + "' while parsing: " + path + ":" + itos(line));
  102. }
  103. l = l.substr(1, l.length());
  104. //find final quote
  105. int end_pos = -1;
  106. for (int i = 0; i < l.length(); i++) {
  107. if (l[i] == '"' && (i == 0 || l[i - 1] != '\\')) {
  108. end_pos = i;
  109. break;
  110. }
  111. }
  112. if (end_pos == -1) {
  113. memdelete(f);
  114. ERR_FAIL_V_MSG(RES(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line));
  115. }
  116. l = l.substr(0, end_pos);
  117. l = l.c_unescape();
  118. if (status == STATUS_READING_ID)
  119. msg_id += l;
  120. else
  121. msg_str += l;
  122. #ifdef DEBUG_ENABLED
  123. line++;
  124. #endif
  125. }
  126. memdelete(f);
  127. if (status == STATUS_READING_STRING) {
  128. if (msg_id != "") {
  129. if (!skip_this)
  130. translation->add_message(msg_id, msg_str);
  131. } else if (config == "")
  132. config = msg_str;
  133. }
  134. ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + path + ".");
  135. Vector<String> configs = config.split("\n");
  136. for (int i = 0; i < configs.size(); i++) {
  137. String c = configs[i].strip_edges();
  138. int p = c.find(":");
  139. if (p == -1)
  140. continue;
  141. String prop = c.substr(0, p).strip_edges();
  142. String value = c.substr(p + 1, c.length()).strip_edges();
  143. if (prop == "X-Language" || prop == "Language") {
  144. translation->set_locale(value);
  145. }
  146. }
  147. if (r_error)
  148. *r_error = OK;
  149. return translation;
  150. }
  151. RES TranslationLoaderPO::load(const String &p_path, const String &p_original_path, Error *r_error) {
  152. if (r_error)
  153. *r_error = ERR_CANT_OPEN;
  154. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  155. ERR_FAIL_COND_V_MSG(!f, RES(), "Cannot open file '" + p_path + "'.");
  156. return load_translation(f, r_error);
  157. }
  158. void TranslationLoaderPO::get_recognized_extensions(List<String> *p_extensions) const {
  159. p_extensions->push_back("po");
  160. }
  161. bool TranslationLoaderPO::handles_type(const String &p_type) const {
  162. return (p_type == "Translation");
  163. }
  164. String TranslationLoaderPO::get_resource_type(const String &p_path) const {
  165. if (p_path.get_extension().to_lower() == "po")
  166. return "Translation";
  167. return "";
  168. }
  169. TranslationLoaderPO::TranslationLoaderPO() {
  170. }