pot_generator.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*************************************************************************/
  2. /* pot_generator.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 "pot_generator.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/error/error_macros.h"
  33. #include "editor_translation_parser.h"
  34. #include "plugins/packed_scene_translation_parser_plugin.h"
  35. POTGenerator *POTGenerator::singleton = nullptr;
  36. #ifdef DEBUG_POT
  37. void POTGenerator::_print_all_translation_strings() {
  38. for (auto 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 (Set<String>::Element *E = v_md[i].locations.front(); E; E = E->next()) {
  46. print_line("location: " + E->get());
  47. }
  48. }
  49. }
  50. }
  51. #endif
  52. void POTGenerator::generate_pot(const String &p_file) {
  53. if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translations_pot_files")) {
  54. WARN_PRINT("No files selected for POT generation.");
  55. return;
  56. }
  57. // Clear all_translation_strings of the previous round.
  58. all_translation_strings.clear();
  59. Vector<String> files = ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files");
  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. String file_path = files[i];
  65. String file_extension = file_path.get_extension();
  66. if (EditorTranslationParser::get_singleton()->can_parse(file_extension)) {
  67. EditorTranslationParser::get_singleton()->get_parser(file_extension)->parse_file(file_path, &msgids, &msgids_context_plural);
  68. } else {
  69. ERR_PRINT("Unrecognized file extension " + file_extension + " in generate_pot()");
  70. return;
  71. }
  72. for (int j = 0; j < msgids_context_plural.size(); j++) {
  73. Vector<String> entry = msgids_context_plural[j];
  74. _add_new_msgid(entry[0], entry[1], entry[2], file_path);
  75. }
  76. for (int j = 0; j < msgids.size(); j++) {
  77. _add_new_msgid(msgids[j], "", "", file_path);
  78. }
  79. }
  80. _write_to_pot(p_file);
  81. }
  82. void POTGenerator::_write_to_pot(const String &p_file) {
  83. Error err;
  84. FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  85. if (err != OK) {
  86. ERR_PRINT("Failed to open " + p_file);
  87. return;
  88. }
  89. String project_name = ProjectSettings::get_singleton()->get("application/config/name");
  90. Vector<String> files = ProjectSettings::get_singleton()->get("internationalization/locale/translations_pot_files");
  91. String extracted_files = "";
  92. for (int i = 0; i < files.size(); i++) {
  93. extracted_files += "# " + files[i] + "\n";
  94. }
  95. const String header =
  96. "# LANGUAGE translation for " + project_name + " for the following files:\n" + extracted_files +
  97. "#\n"
  98. "#\n"
  99. "# FIRST AUTHOR < EMAIL @ADDRESS>, YEAR.\n"
  100. "#\n"
  101. "#, fuzzy\n"
  102. "msgid \"\"\n"
  103. "msgstr \"\"\n"
  104. "\"Project-Id-Version: " +
  105. project_name + "\\n\"\n"
  106. "\"Content-Type: text/plain; charset=UTF-8\\n\"\n"
  107. "\"Content-Transfer-Encoding: 8-bit\\n\"\n\n";
  108. file->store_string(header);
  109. for (OrderedHashMap<String, Vector<MsgidData>>::Element E_pair = all_translation_strings.front(); E_pair; E_pair = E_pair.next()) {
  110. String msgid = E_pair.key();
  111. Vector<MsgidData> v_msgid_data = E_pair.value();
  112. for (int i = 0; i < v_msgid_data.size(); i++) {
  113. String context = v_msgid_data[i].ctx;
  114. String plural = v_msgid_data[i].plural;
  115. const Set<String> &locations = v_msgid_data[i].locations;
  116. // Write file locations.
  117. for (Set<String>::Element *E = locations.front(); E; E = E->next()) {
  118. file->store_line("#: " + E->get().trim_prefix("res://"));
  119. }
  120. // Write context.
  121. if (!context.is_empty()) {
  122. file->store_line("msgctxt \"" + context + "\"");
  123. }
  124. // Write msgid.
  125. _write_msgid(file, msgid, false);
  126. // Write msgid_plural
  127. if (!plural.is_empty()) {
  128. _write_msgid(file, plural, true);
  129. file->store_line("msgstr[0] \"\"");
  130. file->store_line("msgstr[1] \"\"\n");
  131. } else {
  132. file->store_line("msgstr \"\"\n");
  133. }
  134. }
  135. }
  136. file->close();
  137. }
  138. void POTGenerator::_write_msgid(FileAccess *r_file, const String &p_id, bool p_plural) {
  139. // Split \\n and \n.
  140. Vector<String> temp = p_id.split("\\n");
  141. Vector<String> msg_lines;
  142. for (int i = 0; i < temp.size(); i++) {
  143. msg_lines.append_array(temp[i].split("\n"));
  144. if (i < temp.size() - 1) {
  145. // Add \n.
  146. msg_lines.set(msg_lines.size() - 1, msg_lines[msg_lines.size() - 1] + "\\n");
  147. }
  148. }
  149. if (p_plural) {
  150. r_file->store_string("msgid_plural ");
  151. } else {
  152. r_file->store_string("msgid ");
  153. }
  154. for (int i = 0; i < msg_lines.size(); i++) {
  155. r_file->store_line("\"" + msg_lines[i] + "\"");
  156. }
  157. }
  158. void POTGenerator::_add_new_msgid(const String &p_msgid, const String &p_context, const String &p_plural, const String &p_location) {
  159. // Insert new location if msgid under same context exists already.
  160. if (all_translation_strings.has(p_msgid)) {
  161. Vector<MsgidData> &v_mdata = all_translation_strings[p_msgid];
  162. for (int i = 0; i < v_mdata.size(); i++) {
  163. if (v_mdata[i].ctx == p_context) {
  164. if (!v_mdata[i].plural.is_empty() && !p_plural.is_empty() && v_mdata[i].plural != p_plural) {
  165. WARN_PRINT("Redefinition of plural message (msgid_plural), under the same message (msgid) and context (msgctxt)");
  166. }
  167. v_mdata.write[i].locations.insert(p_location);
  168. return;
  169. }
  170. }
  171. }
  172. // Add a new entry of msgid, context, plural and location - context and plural might be empty if the inserted msgid doesn't associated
  173. // context or plurals.
  174. MsgidData mdata;
  175. mdata.ctx = p_context;
  176. mdata.plural = p_plural;
  177. mdata.locations.insert(p_location);
  178. all_translation_strings[p_msgid].push_back(mdata);
  179. }
  180. POTGenerator *POTGenerator::get_singleton() {
  181. if (!singleton) {
  182. singleton = memnew(POTGenerator);
  183. }
  184. return singleton;
  185. }
  186. POTGenerator::POTGenerator() {
  187. }
  188. POTGenerator::~POTGenerator() {
  189. memdelete(singleton);
  190. singleton = nullptr;
  191. }