From aac4a3611daffa7059915901887a074f1fe27916 Mon Sep 17 00:00:00 2001 From: Danil Alexeev Date: Sun, 30 Jul 2023 17:21:54 +0300 Subject: [PATCH] Editor: Fix escaping issues with POT generator --- editor/pot_generator.cpp | 56 ++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/editor/pot_generator.cpp b/editor/pot_generator.cpp index 9428d290880..ec044ee06e6 100644 --- a/editor/pot_generator.cpp +++ b/editor/pot_generator.cpp @@ -99,25 +99,27 @@ void POTGenerator::_write_to_pot(const String &p_file) { return; } - String project_name = GLOBAL_GET("application/config/name"); + String project_name = GLOBAL_GET("application/config/name").operator String().replace("\n", "\\n"); Vector files = GLOBAL_GET("internationalization/locale/translations_pot_files"); String extracted_files = ""; for (int i = 0; i < files.size(); i++) { - extracted_files += "# " + files[i] + "\n"; + extracted_files += "# " + files[i].replace("\n", "\\n") + "\n"; } const String header = - "# LANGUAGE translation for " + project_name + " for the following files:\n" + extracted_files + + "# LANGUAGE translation for " + project_name + " for the following files:\n" + + extracted_files + "#\n" - "# FIRST AUTHOR < EMAIL @ADDRESS>, YEAR.\n" + "# FIRST AUTHOR , YEAR.\n" "#\n" "#, fuzzy\n" "msgid \"\"\n" "msgstr \"\"\n" "\"Project-Id-Version: " + - project_name + "\\n\"\n" - "\"MIME-Version: 1.0\\n\"\n" - "\"Content-Type: text/plain; charset=UTF-8\\n\"\n" - "\"Content-Transfer-Encoding: 8-bit\\n\"\n"; + project_name + + "\\n\"\n" + "\"MIME-Version: 1.0\\n\"\n" + "\"Content-Type: text/plain; charset=UTF-8\\n\"\n" + "\"Content-Transfer-Encoding: 8-bit\\n\"\n"; file->store_string(header); @@ -134,12 +136,12 @@ void POTGenerator::_write_to_pot(const String &p_file) { // Write file locations. for (const String &E : locations) { - file->store_line("#: " + E.trim_prefix("res://")); + file->store_line("#: " + E.trim_prefix("res://").replace("\n", "\\n")); } // Write context. if (!context.is_empty()) { - file->store_line("msgctxt \"" + context + "\""); + file->store_line("msgctxt " + context.c_escape().quote()); } // Write msgid. @@ -158,30 +160,34 @@ void POTGenerator::_write_to_pot(const String &p_file) { } void POTGenerator::_write_msgid(Ref r_file, const String &p_id, bool p_plural) { - // Split \\n and \n. - Vector msg_lines; - Vector temp = p_id.split("\\n"); - for (int i = 0; i < temp.size(); i++) { - msg_lines.append_array(temp[i].split("\n")); - } - - // Add \n. - for (int i = 0; i < msg_lines.size() - 1; i++) { - msg_lines.set(i, msg_lines[i] + "\\n"); - } - if (p_plural) { r_file->store_string("msgid_plural "); } else { r_file->store_string("msgid "); } - if (msg_lines.size() > 1) { + if (p_id.is_empty()) { + r_file->store_line("\"\""); + return; + } + + const Vector lines = p_id.split("\n"); + const String &last_line = lines[lines.size() - 1]; // `lines` cannot be empty. + int pot_line_count = lines.size(); + if (last_line.is_empty()) { + pot_line_count--; + } + + if (pot_line_count > 1) { r_file->store_line("\"\""); } - for (int i = 0; i < msg_lines.size(); i++) { - r_file->store_line("\"" + msg_lines[i] + "\""); + for (int i = 0; i < lines.size() - 1; i++) { + r_file->store_line((lines[i] + "\n").c_escape().quote()); + } + + if (!last_line.is_empty()) { + r_file->store_line(last_line.c_escape().quote()); } }