translation_loader_po.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*************************************************************************/
  2. /* translation_loader_po.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "translation_loader_po.h"
  30. #include "os/file_access.h"
  31. #include "translation.h"
  32. RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const String &p_path) {
  33. enum Status {
  34. STATUS_NONE,
  35. STATUS_READING_ID,
  36. STATUS_READING_STRING,
  37. };
  38. Status status = STATUS_NONE;
  39. String msg_id;
  40. String msg_str;
  41. String config;
  42. if (r_error)
  43. *r_error = ERR_FILE_CORRUPT;
  44. Ref<Translation> translation = Ref<Translation>(memnew(Translation));
  45. int line = 1;
  46. while (true) {
  47. String l = f->get_line();
  48. if (f->eof_reached()) {
  49. if (status == STATUS_READING_STRING) {
  50. if (msg_id != "")
  51. translation->add_message(msg_id, msg_str);
  52. else if (config == "")
  53. config = msg_str;
  54. break;
  55. } else if (status == STATUS_NONE)
  56. break;
  57. memdelete(f);
  58. ERR_EXPLAIN(p_path + ":" + itos(line) + " Unexpected EOF while reading 'msgid' at file: ");
  59. ERR_FAIL_V(RES());
  60. }
  61. l = l.strip_edges();
  62. if (l.begins_with("msgid")) {
  63. if (status == STATUS_READING_ID) {
  64. memdelete(f);
  65. ERR_EXPLAIN(p_path + ":" + itos(line) + " Unexpected 'msgid', was expecting 'msgstr' while parsing: ");
  66. ERR_FAIL_V(RES());
  67. }
  68. if (msg_id != "")
  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. }
  77. if (l.begins_with("msgstr")) {
  78. if (status != STATUS_READING_ID) {
  79. memdelete(f);
  80. ERR_EXPLAIN(p_path + ":" + itos(line) + " Unexpected 'msgstr', was expecting 'msgid' while parsing: ");
  81. ERR_FAIL_V(RES());
  82. }
  83. l = l.substr(6, l.length()).strip_edges();
  84. status = STATUS_READING_STRING;
  85. }
  86. if (l == "" || l.begins_with("#")) {
  87. line++;
  88. continue; //nothing to read or comment
  89. }
  90. if (!l.begins_with("\"") || status == STATUS_NONE) {
  91. //not a string? failure!
  92. ERR_EXPLAIN(p_path + ":" + itos(line) + " Invalid line '" + l + "' while parsing: ");
  93. ERR_FAIL_V(RES());
  94. }
  95. l = l.substr(1, l.length());
  96. //find final quote
  97. int end_pos = -1;
  98. for (int i = 0; i < l.length(); i++) {
  99. if (l[i] == '"' && (i == 0 || l[i - 1] != '\\')) {
  100. end_pos = i;
  101. break;
  102. }
  103. }
  104. if (end_pos == -1) {
  105. ERR_EXPLAIN(p_path + ":" + itos(line) + " Expected '\"' at end of message while parsing file: ");
  106. ERR_FAIL_V(RES());
  107. }
  108. l = l.substr(0, end_pos);
  109. l = l.c_unescape();
  110. if (status == STATUS_READING_ID)
  111. msg_id += l;
  112. else
  113. msg_str += l;
  114. line++;
  115. }
  116. f->close();
  117. memdelete(f);
  118. if (config == "") {
  119. ERR_EXPLAIN("No config found in file: " + p_path);
  120. ERR_FAIL_V(RES());
  121. }
  122. Vector<String> configs = config.split("\n");
  123. for (int i = 0; i < configs.size(); i++) {
  124. String c = configs[i].strip_edges();
  125. int p = c.find(":");
  126. if (p == -1)
  127. continue;
  128. String prop = c.substr(0, p).strip_edges();
  129. String value = c.substr(p + 1, c.length()).strip_edges();
  130. if (prop == "X-Language") {
  131. translation->set_locale(value);
  132. }
  133. }
  134. if (r_error)
  135. *r_error = OK;
  136. return translation;
  137. }
  138. RES TranslationLoaderPO::load(const String &p_path, const String &p_original_path, Error *r_error) {
  139. if (r_error)
  140. *r_error = ERR_CANT_OPEN;
  141. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  142. ERR_FAIL_COND_V(!f, RES());
  143. return load_translation(f, r_error);
  144. }
  145. void TranslationLoaderPO::get_recognized_extensions(List<String> *p_extensions) const {
  146. p_extensions->push_back("po");
  147. //p_extensions->push_back("mo"); //mo in the future...
  148. }
  149. bool TranslationLoaderPO::handles_type(const String &p_type) const {
  150. return (p_type == "Translation");
  151. }
  152. String TranslationLoaderPO::get_resource_type(const String &p_path) const {
  153. if (p_path.get_extension().to_lower() == "po")
  154. return "Translation";
  155. return "";
  156. }
  157. TranslationLoaderPO::TranslationLoaderPO() {
  158. }