translation_loader_po.cpp 6.1 KB

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