pot_generator.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. const 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. const 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. if (GLOBAL_GET("internationalization/locale/translation_add_builtin_strings_to_pot")) {
  81. for (const Vector<String> &extractable_msgids : get_extractable_message_list()) {
  82. _add_new_msgid(extractable_msgids[0], extractable_msgids[1], extractable_msgids[2], "");
  83. }
  84. }
  85. _write_to_pot(p_file);
  86. }
  87. void POTGenerator::_write_to_pot(const String &p_file) {
  88. Error err;
  89. Ref<FileAccess> file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  90. if (err != OK) {
  91. ERR_PRINT("Failed to open " + p_file);
  92. return;
  93. }
  94. String project_name = GLOBAL_GET("application/config/name").operator String().replace("\n", "\\n");
  95. Vector<String> files = GLOBAL_GET("internationalization/locale/translations_pot_files");
  96. String extracted_files = "";
  97. for (int i = 0; i < files.size(); i++) {
  98. extracted_files += "# " + files[i].replace("\n", "\\n") + "\n";
  99. }
  100. const String header =
  101. "# LANGUAGE translation for " + project_name + " for the following files:\n" +
  102. extracted_files +
  103. "#\n"
  104. "# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n"
  105. "#\n"
  106. "#, fuzzy\n"
  107. "msgid \"\"\n"
  108. "msgstr \"\"\n"
  109. "\"Project-Id-Version: " +
  110. project_name +
  111. "\\n\"\n"
  112. "\"MIME-Version: 1.0\\n\"\n"
  113. "\"Content-Type: text/plain; charset=UTF-8\\n\"\n"
  114. "\"Content-Transfer-Encoding: 8-bit\\n\"\n";
  115. file->store_string(header);
  116. for (const KeyValue<String, Vector<MsgidData>> &E_pair : all_translation_strings) {
  117. String msgid = E_pair.key;
  118. const Vector<MsgidData> &v_msgid_data = E_pair.value;
  119. for (int i = 0; i < v_msgid_data.size(); i++) {
  120. String context = v_msgid_data[i].ctx;
  121. String plural = v_msgid_data[i].plural;
  122. const HashSet<String> &locations = v_msgid_data[i].locations;
  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 file locations.
  126. for (const String &E : locations) {
  127. if (!E.is_empty()) {
  128. file->store_line("#: " + E.trim_prefix("res://").replace("\n", "\\n"));
  129. }
  130. }
  131. // Write context.
  132. if (!context.is_empty()) {
  133. file->store_line("msgctxt " + context.json_escape().quote());
  134. }
  135. // Write msgid.
  136. _write_msgid(file, msgid, false);
  137. // Write msgid_plural.
  138. if (!plural.is_empty()) {
  139. _write_msgid(file, plural, true);
  140. file->store_line("msgstr[0] \"\"");
  141. file->store_line("msgstr[1] \"\"");
  142. } else {
  143. file->store_line("msgstr \"\"");
  144. }
  145. }
  146. }
  147. }
  148. void POTGenerator::_write_msgid(Ref<FileAccess> r_file, const String &p_id, bool p_plural) {
  149. if (p_plural) {
  150. r_file->store_string("msgid_plural ");
  151. } else {
  152. r_file->store_string("msgid ");
  153. }
  154. if (p_id.is_empty()) {
  155. r_file->store_line("\"\"");
  156. return;
  157. }
  158. const Vector<String> lines = p_id.split("\n");
  159. const String &last_line = lines[lines.size() - 1]; // `lines` cannot be empty.
  160. int pot_line_count = lines.size();
  161. if (last_line.is_empty()) {
  162. pot_line_count--;
  163. }
  164. if (pot_line_count > 1) {
  165. r_file->store_line("\"\"");
  166. }
  167. for (int i = 0; i < lines.size() - 1; i++) {
  168. r_file->store_line((lines[i] + "\n").json_escape().quote());
  169. }
  170. if (!last_line.is_empty()) {
  171. r_file->store_line(last_line.json_escape().quote());
  172. }
  173. }
  174. void POTGenerator::_add_new_msgid(const String &p_msgid, const String &p_context, const String &p_plural, const String &p_location) {
  175. // Insert new location if msgid under same context exists already.
  176. if (all_translation_strings.has(p_msgid)) {
  177. Vector<MsgidData> &v_mdata = all_translation_strings[p_msgid];
  178. for (int i = 0; i < v_mdata.size(); i++) {
  179. if (v_mdata[i].ctx == p_context) {
  180. if (!v_mdata[i].plural.is_empty() && !p_plural.is_empty() && v_mdata[i].plural != p_plural) {
  181. WARN_PRINT("Redefinition of plural message (msgid_plural), under the same message (msgid) and context (msgctxt)");
  182. }
  183. v_mdata.write[i].locations.insert(p_location);
  184. return;
  185. }
  186. }
  187. }
  188. // Add a new entry of msgid, context, plural and location - context and plural might be empty if the inserted msgid doesn't associated
  189. // context or plurals.
  190. MsgidData mdata;
  191. mdata.ctx = p_context;
  192. mdata.plural = p_plural;
  193. mdata.locations.insert(p_location);
  194. all_translation_strings[p_msgid].push_back(mdata);
  195. }
  196. POTGenerator *POTGenerator::get_singleton() {
  197. if (!singleton) {
  198. singleton = memnew(POTGenerator);
  199. }
  200. return singleton;
  201. }
  202. POTGenerator::POTGenerator() {
  203. }
  204. POTGenerator::~POTGenerator() {
  205. memdelete(singleton);
  206. singleton = nullptr;
  207. }