pot_generator.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**************************************************************************/
  2. /* pot_generator.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 "pot_generator.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/error/error_macros.h"
  33. #include "editor/editor_translation.h"
  34. #include "editor/editor_translation_parser.h"
  35. POTGenerator *POTGenerator::singleton = nullptr;
  36. #ifdef DEBUG_POT
  37. void POTGenerator::_print_all_translation_strings() {
  38. for (HashMap<String, Vector<POTGenerator::MsgidData>>::Element E = all_translation_strings.front(); E; E = E.next()) {
  39. Vector<MsgidData> v_md = all_translation_strings[E.key()];
  40. for (int i = 0; i < v_md.size(); i++) {
  41. print_line("++++++");
  42. print_line("msgid: " + E.key());
  43. print_line("context: " + v_md[i].ctx);
  44. print_line("msgid_plural: " + v_md[i].plural);
  45. for (const String &F : v_md[i].locations) {
  46. print_line("location: " + F);
  47. }
  48. }
  49. }
  50. }
  51. #endif
  52. void POTGenerator::generate_pot(const String &p_file) {
  53. Vector<String> files = GLOBAL_GET("internationalization/locale/translations_pot_files");
  54. if (files.is_empty()) {
  55. WARN_PRINT("No files selected for POT generation.");
  56. return;
  57. }
  58. // Clear all_translation_strings of the previous round.
  59. all_translation_strings.clear();
  60. // Collect all translatable strings according to files order in "POT Generation" setting.
  61. for (int i = 0; i < files.size(); i++) {
  62. Vector<Vector<String>> translations;
  63. const String &file_path = files[i];
  64. String file_extension = file_path.get_extension();
  65. if (EditorTranslationParser::get_singleton()->can_parse(file_extension)) {
  66. EditorTranslationParser::get_singleton()->get_parser(file_extension)->parse_file(file_path, &translations);
  67. } else {
  68. ERR_PRINT("Unrecognized file extension " + file_extension + " in generate_pot()");
  69. return;
  70. }
  71. for (const Vector<String> &translation : translations) {
  72. ERR_CONTINUE(translation.is_empty());
  73. const String &msgctxt = (translation.size() > 1) ? translation[1] : String();
  74. const String &msgid_plural = (translation.size() > 2) ? translation[2] : String();
  75. const String &comment = (translation.size() > 3) ? translation[3] : String();
  76. _add_new_msgid(translation[0], msgctxt, msgid_plural, file_path, comment);
  77. }
  78. }
  79. if (GLOBAL_GET("internationalization/locale/translation_add_builtin_strings_to_pot")) {
  80. for (const Vector<String> &extractable_msgids : get_extractable_message_list()) {
  81. _add_new_msgid(extractable_msgids[0], extractable_msgids[1], extractable_msgids[2], "", "");
  82. }
  83. }
  84. _write_to_pot(p_file);
  85. }
  86. void POTGenerator::_write_to_pot(const String &p_file) {
  87. Error err;
  88. Ref<FileAccess> file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  89. if (err != OK) {
  90. ERR_PRINT("Failed to open " + p_file);
  91. return;
  92. }
  93. String project_name = GLOBAL_GET("application/config/name").operator String().replace("\n", "\\n");
  94. Vector<String> files = GLOBAL_GET("internationalization/locale/translations_pot_files");
  95. String extracted_files = "";
  96. for (int i = 0; i < files.size(); i++) {
  97. extracted_files += "# " + files[i].replace("\n", "\\n") + "\n";
  98. }
  99. const String header =
  100. "# LANGUAGE translation for " + project_name + " for the following files:\n" +
  101. extracted_files +
  102. "#\n"
  103. "# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n"
  104. "#\n"
  105. "#, fuzzy\n"
  106. "msgid \"\"\n"
  107. "msgstr \"\"\n"
  108. "\"Project-Id-Version: " +
  109. project_name +
  110. "\\n\"\n"
  111. "\"MIME-Version: 1.0\\n\"\n"
  112. "\"Content-Type: text/plain; charset=UTF-8\\n\"\n"
  113. "\"Content-Transfer-Encoding: 8-bit\\n\"\n";
  114. file->store_string(header);
  115. for (const KeyValue<String, Vector<MsgidData>> &E_pair : all_translation_strings) {
  116. String msgid = E_pair.key;
  117. const Vector<MsgidData> &v_msgid_data = E_pair.value;
  118. for (int i = 0; i < v_msgid_data.size(); i++) {
  119. String context = v_msgid_data[i].ctx;
  120. String plural = v_msgid_data[i].plural;
  121. const HashSet<String> &locations = v_msgid_data[i].locations;
  122. const HashSet<String> &comments = v_msgid_data[i].comments;
  123. // Put the blank line at the start, to avoid a double at the end when closing the file.
  124. file->store_line("");
  125. // Write comments.
  126. bool is_first_comment = true;
  127. for (const String &E : comments) {
  128. if (is_first_comment) {
  129. file->store_line("#. TRANSLATORS: " + E.replace("\n", "\n#. "));
  130. } else {
  131. file->store_line("#. " + E.replace("\n", "\n#. "));
  132. }
  133. is_first_comment = false;
  134. }
  135. // Write file locations.
  136. for (const String &E : locations) {
  137. file->store_line("#: " + E.trim_prefix("res://").replace("\n", "\\n"));
  138. }
  139. // Write context.
  140. if (!context.is_empty()) {
  141. file->store_line("msgctxt " + context.json_escape().quote());
  142. }
  143. // Write msgid.
  144. _write_msgid(file, msgid, false);
  145. // Write msgid_plural.
  146. if (!plural.is_empty()) {
  147. _write_msgid(file, plural, true);
  148. file->store_line("msgstr[0] \"\"");
  149. file->store_line("msgstr[1] \"\"");
  150. } else {
  151. file->store_line("msgstr \"\"");
  152. }
  153. }
  154. }
  155. }
  156. void POTGenerator::_write_msgid(Ref<FileAccess> r_file, const String &p_id, bool p_plural) {
  157. if (p_plural) {
  158. r_file->store_string("msgid_plural ");
  159. } else {
  160. r_file->store_string("msgid ");
  161. }
  162. if (p_id.is_empty()) {
  163. r_file->store_line("\"\"");
  164. return;
  165. }
  166. const Vector<String> lines = p_id.split("\n");
  167. const String &last_line = lines[lines.size() - 1]; // `lines` cannot be empty.
  168. int pot_line_count = lines.size();
  169. if (last_line.is_empty()) {
  170. pot_line_count--;
  171. }
  172. if (pot_line_count > 1) {
  173. r_file->store_line("\"\"");
  174. }
  175. for (int i = 0; i < lines.size() - 1; i++) {
  176. r_file->store_line((lines[i] + "\n").json_escape().quote());
  177. }
  178. if (!last_line.is_empty()) {
  179. r_file->store_line(last_line.json_escape().quote());
  180. }
  181. }
  182. void POTGenerator::_add_new_msgid(const String &p_msgid, const String &p_context, const String &p_plural, const String &p_location, const String &p_comment) {
  183. // Insert new location if msgid under same context exists already.
  184. if (all_translation_strings.has(p_msgid)) {
  185. Vector<MsgidData> &v_mdata = all_translation_strings[p_msgid];
  186. for (int i = 0; i < v_mdata.size(); i++) {
  187. if (v_mdata[i].ctx == p_context) {
  188. if (!v_mdata[i].plural.is_empty() && !p_plural.is_empty() && v_mdata[i].plural != p_plural) {
  189. WARN_PRINT("Redefinition of plural message (msgid_plural), under the same message (msgid) and context (msgctxt)");
  190. }
  191. if (!p_location.is_empty()) {
  192. v_mdata.write[i].locations.insert(p_location);
  193. }
  194. if (!p_comment.is_empty()) {
  195. v_mdata.write[i].comments.insert(p_comment);
  196. }
  197. return;
  198. }
  199. }
  200. }
  201. // Add a new entry.
  202. MsgidData mdata;
  203. mdata.ctx = p_context;
  204. mdata.plural = p_plural;
  205. if (!p_location.is_empty()) {
  206. mdata.locations.insert(p_location);
  207. }
  208. if (!p_comment.is_empty()) {
  209. mdata.comments.insert(p_comment);
  210. }
  211. all_translation_strings[p_msgid].push_back(mdata);
  212. }
  213. POTGenerator *POTGenerator::get_singleton() {
  214. if (!singleton) {
  215. singleton = memnew(POTGenerator);
  216. }
  217. return singleton;
  218. }
  219. POTGenerator::~POTGenerator() {
  220. memdelete(singleton);
  221. singleton = nullptr;
  222. }