pot_generator.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_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 (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. 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. Ref<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 = GLOBAL_GET("application/config/name");
  90. Vector<String> files = GLOBAL_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. "# FIRST AUTHOR < EMAIL @ADDRESS>, YEAR.\n"
  99. "#\n"
  100. "#, fuzzy\n"
  101. "msgid \"\"\n"
  102. "msgstr \"\"\n"
  103. "\"Project-Id-Version: " +
  104. project_name + "\\n\"\n"
  105. "\"MIME-Version: 1.0\\n\"\n"
  106. "\"Content-Type: text/plain; charset=UTF-8\\n\"\n"
  107. "\"Content-Transfer-Encoding: 8-bit\\n\"\n";
  108. file->store_string(header);
  109. for (const KeyValue<String, Vector<MsgidData>> &E_pair : all_translation_strings) {
  110. String msgid = E_pair.key;
  111. const 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 HashSet<String> &locations = v_msgid_data[i].locations;
  116. // Put the blank line at the start, to avoid a double at the end when closing the file.
  117. file->store_line("");
  118. // Write file locations.
  119. for (const String &E : locations) {
  120. file->store_line("#: " + E.trim_prefix("res://"));
  121. }
  122. // Write context.
  123. if (!context.is_empty()) {
  124. file->store_line("msgctxt \"" + context + "\"");
  125. }
  126. // Write msgid.
  127. _write_msgid(file, msgid, false);
  128. // Write msgid_plural.
  129. if (!plural.is_empty()) {
  130. _write_msgid(file, plural, true);
  131. file->store_line("msgstr[0] \"\"");
  132. file->store_line("msgstr[1] \"\"");
  133. } else {
  134. file->store_line("msgstr \"\"");
  135. }
  136. }
  137. }
  138. }
  139. void POTGenerator::_write_msgid(Ref<FileAccess> r_file, const String &p_id, bool p_plural) {
  140. // Split \\n and \n.
  141. Vector<String> msg_lines;
  142. Vector<String> temp = p_id.split("\\n");
  143. for (int i = 0; i < temp.size(); i++) {
  144. msg_lines.append_array(temp[i].split("\n"));
  145. }
  146. // Add \n.
  147. for (int i = 0; i < msg_lines.size() - 1; i++) {
  148. msg_lines.set(i, msg_lines[i] + "\\n");
  149. }
  150. if (p_plural) {
  151. r_file->store_string("msgid_plural ");
  152. } else {
  153. r_file->store_string("msgid ");
  154. }
  155. if (msg_lines.size() > 1) {
  156. r_file->store_line("\"\"");
  157. }
  158. for (int i = 0; i < msg_lines.size(); i++) {
  159. r_file->store_line("\"" + msg_lines[i] + "\"");
  160. }
  161. }
  162. void POTGenerator::_add_new_msgid(const String &p_msgid, const String &p_context, const String &p_plural, const String &p_location) {
  163. // Insert new location if msgid under same context exists already.
  164. if (all_translation_strings.has(p_msgid)) {
  165. Vector<MsgidData> &v_mdata = all_translation_strings[p_msgid];
  166. for (int i = 0; i < v_mdata.size(); i++) {
  167. if (v_mdata[i].ctx == p_context) {
  168. if (!v_mdata[i].plural.is_empty() && !p_plural.is_empty() && v_mdata[i].plural != p_plural) {
  169. WARN_PRINT("Redefinition of plural message (msgid_plural), under the same message (msgid) and context (msgctxt)");
  170. }
  171. v_mdata.write[i].locations.insert(p_location);
  172. return;
  173. }
  174. }
  175. }
  176. // Add a new entry of msgid, context, plural and location - context and plural might be empty if the inserted msgid doesn't associated
  177. // context or plurals.
  178. MsgidData mdata;
  179. mdata.ctx = p_context;
  180. mdata.plural = p_plural;
  181. mdata.locations.insert(p_location);
  182. all_translation_strings[p_msgid].push_back(mdata);
  183. }
  184. POTGenerator *POTGenerator::get_singleton() {
  185. if (!singleton) {
  186. singleton = memnew(POTGenerator);
  187. }
  188. return singleton;
  189. }
  190. POTGenerator::POTGenerator() {
  191. }
  192. POTGenerator::~POTGenerator() {
  193. memdelete(singleton);
  194. singleton = nullptr;
  195. }