pot_generator.cpp 9.3 KB

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