translation_loader_po.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**************************************************************************/
  2. /* translation_loader_po.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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, bool p_use_context, Error *r_error) {
  34. if (r_error) {
  35. *r_error = ERR_FILE_CORRUPT;
  36. }
  37. const String path = f->get_path();
  38. Ref<Translation> translation;
  39. if (p_use_context) {
  40. translation = Ref<Translation>(memnew(ContextTranslation));
  41. } else {
  42. translation.instance();
  43. }
  44. String config;
  45. uint32_t magic = f->get_32();
  46. if (magic == 0x950412de) {
  47. // Load binary MO file.
  48. uint16_t version_maj = f->get_16();
  49. uint16_t version_min = f->get_16();
  50. if (version_maj > 1) {
  51. ERR_FAIL_V_MSG(RES(), vformat("Unsupported MO file %s, version %d.%d.", path, version_maj, version_min));
  52. }
  53. uint32_t num_strings = f->get_32();
  54. uint32_t id_table_offset = f->get_32();
  55. uint32_t trans_table_offset = f->get_32();
  56. // Read string tables.
  57. for (uint32_t i = 0; i < num_strings; i++) {
  58. String msg_id;
  59. String msg_context;
  60. // Read id strings and context.
  61. {
  62. Vector<uint8_t> data;
  63. f->seek(id_table_offset + i * 8);
  64. uint32_t str_start = 0;
  65. uint32_t str_len = f->get_32();
  66. uint32_t str_offset = f->get_32();
  67. data.resize(str_len + 1);
  68. f->seek(str_offset);
  69. f->get_buffer(data.ptrw(), str_len);
  70. data.write[str_len] = 0;
  71. for (uint32_t j = 0; j < str_len + 1; j++) {
  72. if (data[j] == 0x04) {
  73. msg_context.parse_utf8((const char *)data.ptr(), j);
  74. str_start = j + 1;
  75. }
  76. if (data[j] == 0x00) {
  77. msg_id.parse_utf8((const char *)(data.ptr() + str_start), j - str_start);
  78. break;
  79. }
  80. }
  81. }
  82. // Read translated strings.
  83. {
  84. Vector<uint8_t> data;
  85. f->seek(trans_table_offset + i * 8);
  86. uint32_t str_len = f->get_32();
  87. uint32_t str_offset = f->get_32();
  88. data.resize(str_len + 1);
  89. f->seek(str_offset);
  90. f->get_buffer(data.ptrw(), str_len);
  91. data.write[str_len] = 0;
  92. if (msg_id.empty()) {
  93. config = String::utf8((const char *)data.ptr(), str_len);
  94. } else {
  95. for (uint32_t j = 0; j < str_len + 1; j++) {
  96. if (data[j] == 0x00) {
  97. translation->add_context_message(msg_id, String::utf8((const char *)data.ptr(), j), msg_context);
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. memdelete(f);
  105. } else {
  106. // Try to load as text PO file.
  107. f->seek(0);
  108. enum Status {
  109. STATUS_NONE,
  110. STATUS_READING_ID,
  111. STATUS_READING_STRING,
  112. STATUS_READING_CONTEXT,
  113. };
  114. Status status = STATUS_NONE;
  115. String msg_id;
  116. String msg_str;
  117. String msg_context;
  118. if (r_error) {
  119. *r_error = ERR_FILE_CORRUPT;
  120. }
  121. int line = 1;
  122. bool entered_context = false;
  123. bool skip_this = false;
  124. bool skip_next = false;
  125. bool is_eof = false;
  126. while (!is_eof) {
  127. String l = f->get_line().strip_edges();
  128. is_eof = f->eof_reached();
  129. // If we reached last line and it's not a content line, break, otherwise let processing that last loop
  130. if (is_eof && l.empty()) {
  131. if (status == STATUS_READING_ID || status == STATUS_READING_CONTEXT) {
  132. memdelete(f);
  133. ERR_FAIL_V_MSG(RES(), "Unexpected EOF while reading PO file at: " + path + ":" + itos(line));
  134. } else {
  135. break;
  136. }
  137. }
  138. if (l.begins_with("msgctxt")) {
  139. if (status != STATUS_READING_STRING) {
  140. memdelete(f);
  141. ERR_FAIL_V_MSG(RES(), "Unexpected 'msgctxt', was expecting 'msgstr' before 'msgctxt' while parsing: " + path + ":" + itos(line));
  142. }
  143. // In PO file, "msgctxt" appears before "msgid". If we encounter a "msgctxt", we add what we have read
  144. // and set "entered_context" to true to prevent adding twice.
  145. if (!skip_this && msg_id != "") {
  146. translation->add_context_message(msg_id, msg_str, msg_context);
  147. }
  148. msg_context = "";
  149. l = l.substr(7, l.length()).strip_edges();
  150. status = STATUS_READING_CONTEXT;
  151. entered_context = true;
  152. }
  153. if (l.begins_with("msgid")) {
  154. if (status == STATUS_READING_ID) {
  155. memdelete(f);
  156. ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line));
  157. }
  158. if (msg_id != "") {
  159. if (!skip_this && !entered_context) {
  160. translation->add_context_message(msg_id, msg_str, msg_context);
  161. }
  162. } else if (config == "") {
  163. config = msg_str;
  164. }
  165. l = l.substr(5, l.length()).strip_edges();
  166. status = STATUS_READING_ID;
  167. // If we did not encounter msgctxt, we reset context to empty to reset it.
  168. if (!entered_context) {
  169. msg_context = "";
  170. }
  171. msg_id = "";
  172. msg_str = "";
  173. skip_this = skip_next;
  174. skip_next = false;
  175. entered_context = false;
  176. }
  177. if (l.begins_with("msgstr")) {
  178. if (status != STATUS_READING_ID) {
  179. memdelete(f);
  180. ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr', was expecting 'msgid' before 'msgstr' while parsing: " + path + ":" + itos(line));
  181. }
  182. l = l.substr(6, l.length()).strip_edges();
  183. status = STATUS_READING_STRING;
  184. }
  185. if (l == "" || l.begins_with("#")) {
  186. if (l.find("fuzzy") != -1) {
  187. skip_next = true;
  188. }
  189. line++;
  190. continue; // Nothing to read or comment.
  191. }
  192. if (!l.begins_with("\"") || status == STATUS_NONE) {
  193. memdelete(f);
  194. ERR_FAIL_V_MSG(RES(), "Invalid line '" + l + "' while parsing: " + path + ":" + itos(line));
  195. }
  196. l = l.substr(1, l.length());
  197. // Find final quote, ignoring escaped ones (\").
  198. // The escape_next logic is necessary to properly parse things like \\"
  199. // where the blackslash is the one being escaped, not the quote.
  200. int end_pos = -1;
  201. bool escape_next = false;
  202. for (int i = 0; i < l.length(); i++) {
  203. if (l[i] == '\\' && !escape_next) {
  204. escape_next = true;
  205. continue;
  206. }
  207. if (l[i] == '"' && !escape_next) {
  208. end_pos = i;
  209. break;
  210. }
  211. escape_next = false;
  212. }
  213. if (end_pos == -1) {
  214. memdelete(f);
  215. ERR_FAIL_V_MSG(RES(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line));
  216. }
  217. l = l.substr(0, end_pos);
  218. l = l.c_unescape();
  219. if (status == STATUS_READING_ID) {
  220. msg_id += l;
  221. } else if (status == STATUS_READING_STRING) {
  222. msg_str += l;
  223. } else if (status == STATUS_READING_CONTEXT) {
  224. msg_context += l;
  225. }
  226. line++;
  227. }
  228. memdelete(f);
  229. // Add the last set of data from last iteration.
  230. if (status == STATUS_READING_STRING) {
  231. if (msg_id != "") {
  232. if (!skip_this) {
  233. translation->add_context_message(msg_id, msg_str, msg_context);
  234. }
  235. } else if (config == "") {
  236. config = msg_str;
  237. }
  238. }
  239. }
  240. ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + path + ".");
  241. Vector<String> configs = config.split("\n");
  242. for (int i = 0; i < configs.size(); i++) {
  243. String c = configs[i].strip_edges();
  244. int p = c.find(":");
  245. if (p == -1) {
  246. continue;
  247. }
  248. String prop = c.substr(0, p).strip_edges();
  249. String value = c.substr(p + 1, c.length()).strip_edges();
  250. if (prop == "X-Language" || prop == "Language") {
  251. translation->set_locale(value);
  252. }
  253. }
  254. if (r_error) {
  255. *r_error = OK;
  256. }
  257. return translation;
  258. }
  259. RES TranslationLoaderPO::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_no_subresource_cache) {
  260. if (r_error) {
  261. *r_error = ERR_CANT_OPEN;
  262. }
  263. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  264. ERR_FAIL_COND_V_MSG(!f, RES(), "Cannot open file '" + p_path + "'.");
  265. return load_translation(f, false, r_error);
  266. }
  267. void TranslationLoaderPO::get_recognized_extensions(List<String> *p_extensions) const {
  268. p_extensions->push_back("po");
  269. p_extensions->push_back("mo");
  270. }
  271. bool TranslationLoaderPO::handles_type(const String &p_type) const {
  272. return (p_type == "Translation");
  273. }
  274. String TranslationLoaderPO::get_resource_type(const String &p_path) const {
  275. if (p_path.get_extension().to_lower() == "po" || p_path.get_extension().to_lower() == "mo") {
  276. return "Translation";
  277. }
  278. return "";
  279. }
  280. TranslationLoaderPO::TranslationLoaderPO() {
  281. }