|
@@ -31,23 +31,25 @@
|
|
|
#include "pot_generator.h"
|
|
|
|
|
|
#include "core/error_macros.h"
|
|
|
-#include "core/os/file_access.h"
|
|
|
#include "core/project_settings.h"
|
|
|
#include "editor_translation_parser.h"
|
|
|
#include "plugins/packed_scene_translation_parser_plugin.h"
|
|
|
|
|
|
POTGenerator *POTGenerator::singleton = nullptr;
|
|
|
|
|
|
-//#define DEBUG_POT
|
|
|
-
|
|
|
#ifdef DEBUG_POT
|
|
|
-void _print_all_translation_strings(const OrderedHashMap<String, Set<String>> &p_all_translation_strings) {
|
|
|
- for (auto E_pair = p_all_translation_strings.front(); E_pair; E_pair = E_pair.next()) {
|
|
|
- String msg = static_cast<String>(E_pair.key()) + " : ";
|
|
|
- for (Set<String>::Element *E = E_pair.value().front(); E; E = E->next()) {
|
|
|
- msg += E->get() + " ";
|
|
|
+void POTGenerator::_print_all_translation_strings() {
|
|
|
+ for (auto E = all_translation_strings.front(); E; E = E.next()) {
|
|
|
+ Vector<MsgidData> v_md = all_translation_strings[E.key()];
|
|
|
+ for (int i = 0; i < v_md.size(); i++) {
|
|
|
+ print_line("++++++");
|
|
|
+ print_line("msgid: " + E.key());
|
|
|
+ print_line("context: " + v_md[i].ctx);
|
|
|
+ print_line("msgid_plural: " + v_md[i].plural);
|
|
|
+ for (Set<String>::Element *E = v_md[i].locations.front(); E; E = E->next()) {
|
|
|
+ print_line("location: " + E->get());
|
|
|
+ }
|
|
|
}
|
|
|
- print_line(msg);
|
|
|
}
|
|
|
}
|
|
|
#endif
|
|
@@ -65,27 +67,27 @@ void POTGenerator::generate_pot(const String &p_file) {
|
|
|
|
|
|
// Collect all translatable strings according to files order in "POT Generation" setting.
|
|
|
for (int i = 0; i < files.size(); i++) {
|
|
|
- Vector<String> translation_strings;
|
|
|
+ Vector<String> msgids;
|
|
|
+ Vector<Vector<String>> msgids_context_plural;
|
|
|
String file_path = files[i];
|
|
|
String file_extension = file_path.get_extension();
|
|
|
|
|
|
if (EditorTranslationParser::get_singleton()->can_parse(file_extension)) {
|
|
|
- EditorTranslationParser::get_singleton()->get_parser(file_extension)->parse_file(file_path, &translation_strings);
|
|
|
+ EditorTranslationParser::get_singleton()->get_parser(file_extension)->parse_file(file_path, &msgids, &msgids_context_plural);
|
|
|
} else {
|
|
|
ERR_PRINT("Unrecognized file extension " + file_extension + " in generate_pot()");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // Store translation strings parsed in this iteration along with their corresponding source file - to write into POT later on.
|
|
|
- for (int j = 0; j < translation_strings.size(); j++) {
|
|
|
- all_translation_strings[translation_strings[j]].insert(file_path);
|
|
|
+ for (int j = 0; j < msgids.size(); j++) {
|
|
|
+ _add_new_msgid(msgids[j], "", "", file_path);
|
|
|
+ }
|
|
|
+ for (int j = 0; j < msgids_context_plural.size(); j++) {
|
|
|
+ Vector<String> entry = msgids_context_plural[j];
|
|
|
+ _add_new_msgid(entry[0], entry[1], entry[2], file_path);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-#ifdef DEBUG_POT
|
|
|
- _print_all_translation_strings(all_translation_strings);
|
|
|
-#endif
|
|
|
-
|
|
|
_write_to_pot(p_file);
|
|
|
}
|
|
|
|
|
@@ -119,35 +121,86 @@ void POTGenerator::_write_to_pot(const String &p_file) {
|
|
|
|
|
|
file->store_string(header);
|
|
|
|
|
|
- for (OrderedHashMap<String, Set<String>>::Element E_pair = all_translation_strings.front(); E_pair; E_pair = E_pair.next()) {
|
|
|
- String msg = E_pair.key();
|
|
|
+ for (OrderedHashMap<String, Vector<MsgidData>>::Element E_pair = all_translation_strings.front(); E_pair; E_pair = E_pair.next()) {
|
|
|
+ String msgid = E_pair.key();
|
|
|
+ Vector<MsgidData> v_msgid_data = E_pair.value();
|
|
|
+ for (int i = 0; i < v_msgid_data.size(); i++) {
|
|
|
+ String context = v_msgid_data[i].ctx;
|
|
|
+ String plural = v_msgid_data[i].plural;
|
|
|
+ const Set<String> &locations = v_msgid_data[i].locations;
|
|
|
+
|
|
|
+ // Write file locations.
|
|
|
+ for (Set<String>::Element *E = locations.front(); E; E = E->next()) {
|
|
|
+ file->store_line("#: " + E->get().trim_prefix("res://"));
|
|
|
+ }
|
|
|
|
|
|
- // Write file locations.
|
|
|
- for (Set<String>::Element *E = E_pair.value().front(); E; E = E->next()) {
|
|
|
- file->store_line("#: " + E->get().trim_prefix("res://"));
|
|
|
- }
|
|
|
+ // Write context.
|
|
|
+ if (!context.empty()) {
|
|
|
+ file->store_line("msgctxt \"" + context + "\"");
|
|
|
+ }
|
|
|
|
|
|
- // Split \\n and \n.
|
|
|
- Vector<String> temp = msg.split("\\n");
|
|
|
- Vector<String> msg_lines;
|
|
|
- for (int i = 0; i < temp.size(); i++) {
|
|
|
- msg_lines.append_array(temp[i].split("\n"));
|
|
|
- if (i < temp.size() - 1) {
|
|
|
- // Add \n.
|
|
|
- msg_lines.set(msg_lines.size() - 1, msg_lines[msg_lines.size() - 1] + "\\n");
|
|
|
+ // Write msgid.
|
|
|
+ _write_msgid(file, msgid, false);
|
|
|
+
|
|
|
+ // Write msgid_plural
|
|
|
+ if (!plural.empty()) {
|
|
|
+ _write_msgid(file, plural, true);
|
|
|
+ file->store_line("msgstr[0] \"\"");
|
|
|
+ file->store_line("msgstr[1] \"\"\n");
|
|
|
+ } else {
|
|
|
+ file->store_line("msgstr \"\"\n");
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // Write msgid.
|
|
|
- file->store_string("msgid ");
|
|
|
- for (int i = 0; i < msg_lines.size(); i++) {
|
|
|
- file->store_line("\"" + msg_lines[i] + "\"");
|
|
|
+ file->close();
|
|
|
+}
|
|
|
+
|
|
|
+void POTGenerator::_write_msgid(FileAccess *r_file, const String &p_id, bool p_plural) {
|
|
|
+ // Split \\n and \n.
|
|
|
+ Vector<String> temp = p_id.split("\\n");
|
|
|
+ Vector<String> msg_lines;
|
|
|
+ for (int i = 0; i < temp.size(); i++) {
|
|
|
+ msg_lines.append_array(temp[i].split("\n"));
|
|
|
+ if (i < temp.size() - 1) {
|
|
|
+ // Add \n.
|
|
|
+ msg_lines.set(msg_lines.size() - 1, msg_lines[msg_lines.size() - 1] + "\\n");
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- file->store_line("msgstr \"\"\n");
|
|
|
+ if (p_plural) {
|
|
|
+ r_file->store_string("msgid_plural ");
|
|
|
+ } else {
|
|
|
+ r_file->store_string("msgid ");
|
|
|
}
|
|
|
|
|
|
- file->close();
|
|
|
+ for (int i = 0; i < msg_lines.size(); i++) {
|
|
|
+ r_file->store_line("\"" + msg_lines[i] + "\"");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void POTGenerator::_add_new_msgid(const String &p_msgid, const String &p_context, const String &p_plural, const String &p_location) {
|
|
|
+ // Insert new location if msgid under same context exists already.
|
|
|
+ if (all_translation_strings.has(p_msgid)) {
|
|
|
+ Vector<MsgidData> &v_mdata = all_translation_strings[p_msgid];
|
|
|
+ for (int i = 0; i < v_mdata.size(); i++) {
|
|
|
+ if (v_mdata[i].ctx == p_context) {
|
|
|
+ if (!v_mdata[i].plural.empty() && !p_plural.empty() && v_mdata[i].plural != p_plural) {
|
|
|
+ WARN_PRINT("Redefinition of plural message (msgid_plural), under the same message (msgid) and context (msgctxt)");
|
|
|
+ }
|
|
|
+ v_mdata.write[i].locations.insert(p_location);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Add a new entry of msgid, context, plural and location - context and plural might be empty if the inserted msgid doesn't associated
|
|
|
+ // context or plurals.
|
|
|
+ MsgidData mdata;
|
|
|
+ mdata.ctx = p_context;
|
|
|
+ mdata.plural = p_plural;
|
|
|
+ mdata.locations.insert(p_location);
|
|
|
+ all_translation_strings[p_msgid].push_back(mdata);
|
|
|
}
|
|
|
|
|
|
POTGenerator *POTGenerator::get_singleton() {
|