translation_loader_po.cpp 6.2 KB

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