[Editor] Fix return of EditorTranslationParserPlugin._parse_file

Merged `_get_comments` into `_parse_file` and changed to using a
returned `Array[PackedStringArray]` instead.
This commit is contained in:
A Thousand Ships
2024-11-15 20:19:00 +01:00
committed by AThousandShips
parent e567f49cbb
commit fec3d9e68c
9 changed files with 82 additions and 111 deletions

View File

@ -36,47 +36,41 @@
EditorTranslationParser *EditorTranslationParser::singleton = nullptr;
Error EditorTranslationParserPlugin::parse_file(const String &p_path, Vector<String> *r_ids, Vector<Vector<String>> *r_ids_ctx_plural) {
Error EditorTranslationParserPlugin::parse_file(const String &p_path, Vector<Vector<String>> *r_translations) {
TypedArray<PackedStringArray> ret;
if (GDVIRTUAL_CALL(_parse_file, p_path, ret)) {
// Copy over entries directly.
for (const PackedStringArray translation : ret) {
r_translations->push_back(translation);
}
return OK;
}
#ifndef DISABLE_DEPRECATED
TypedArray<String> ids;
TypedArray<Array> ids_ctx_plural;
if (GDVIRTUAL_CALL(_parse_file, p_path, ids, ids_ctx_plural)) {
if (GDVIRTUAL_CALL(_parse_file_bind_compat_99297, p_path, ids, ids_ctx_plural)) {
// Add user's extracted translatable messages.
for (int i = 0; i < ids.size(); i++) {
r_ids->append(ids[i]);
r_translations->push_back({ ids[i] });
}
// Add user's collected translatable messages with context or plurals.
for (int i = 0; i < ids_ctx_plural.size(); i++) {
Array arr = ids_ctx_plural[i];
ERR_FAIL_COND_V_MSG(arr.size() != 3, ERR_INVALID_DATA, "Array entries written into `msgids_context_plural` in `parse_file()` method should have the form [\"message\", \"context\", \"plural message\"]");
ERR_FAIL_COND_V_MSG(arr.size() != 3, ERR_INVALID_DATA, "Array entries written into `msgids_context_plural` in `_parse_file` method should have the form [\"message\", \"context\", \"plural message\"]");
Vector<String> id_ctx_plural;
id_ctx_plural.push_back(arr[0]);
id_ctx_plural.push_back(arr[1]);
id_ctx_plural.push_back(arr[2]);
r_ids_ctx_plural->append(id_ctx_plural);
r_translations->push_back({ arr[0], arr[1], arr[2] });
}
return OK;
} else {
ERR_PRINT("Custom translation parser plugin's \"func parse_file(path, extracted_strings)\" is undefined.");
return ERR_UNAVAILABLE;
}
}
#endif // DISABLE_DEPRECATED
void EditorTranslationParserPlugin::get_comments(Vector<String> *r_ids_comment, Vector<String> *r_ids_ctx_plural_comment) {
TypedArray<String> ids_comment;
TypedArray<String> ids_ctx_plural_comment;
if (GDVIRTUAL_CALL(_get_comments, ids_comment, ids_ctx_plural_comment)) {
for (int i = 0; i < ids_comment.size(); i++) {
r_ids_comment->append(ids_comment[i]);
}
for (int i = 0; i < ids_ctx_plural_comment.size(); i++) {
r_ids_ctx_plural_comment->append(ids_ctx_plural_comment[i]);
}
}
ERR_PRINT("Custom translation parser plugin's \"_parse_file\" is undefined.");
return ERR_UNAVAILABLE;
}
void EditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_extensions) const {
@ -91,9 +85,12 @@ void EditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_ex
}
void EditorTranslationParserPlugin::_bind_methods() {
GDVIRTUAL_BIND(_parse_file, "path", "msgids", "msgids_context_plural");
GDVIRTUAL_BIND(_get_comments, "msgids_comment", "msgids_context_plural_comment");
GDVIRTUAL_BIND(_parse_file, "path");
GDVIRTUAL_BIND(_get_recognized_extensions);
#ifndef DISABLE_DEPRECATED
GDVIRTUAL_BIND_COMPAT(_parse_file_bind_compat_99297, "path", "msgids", "msgids_context_plural");
#endif
}
/////////////////////////

View File

@ -42,13 +42,15 @@ class EditorTranslationParserPlugin : public RefCounted {
protected:
static void _bind_methods();
GDVIRTUAL3(_parse_file, String, TypedArray<String>, TypedArray<Array>)
GDVIRTUAL2(_get_comments, TypedArray<String>, TypedArray<String>)
GDVIRTUAL1R(TypedArray<PackedStringArray>, _parse_file, String)
GDVIRTUAL0RC(Vector<String>, _get_recognized_extensions)
#ifndef DISABLE_DEPRECATED
GDVIRTUAL3_COMPAT(_parse_file_bind_compat_99297, _parse_file, String, TypedArray<String>, TypedArray<Array>)
#endif
public:
virtual Error parse_file(const String &p_path, Vector<String> *r_ids, Vector<Vector<String>> *r_ids_ctx_plural);
virtual void get_comments(Vector<String> *r_ids_comment, Vector<String> *r_ids_ctx_plural_comment);
virtual Error parse_file(const String &p_path, Vector<Vector<String>> *r_translations);
virtual void get_recognized_extensions(List<String> *r_extensions) const;
};

View File

@ -38,7 +38,7 @@ void PackedSceneEditorTranslationParserPlugin::get_recognized_extensions(List<St
ResourceLoader::get_recognized_extensions_for_type("PackedScene", r_extensions);
}
Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path, Vector<String> *r_ids, Vector<Vector<String>> *r_ids_ctx_plural) {
Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path, Vector<Vector<String>> *r_translations) {
// Parse specific scene Node's properties (see in constructor) that are auto-translated by the engine when set. E.g Label's text property.
// These properties are translated with the tr() function in the C++ code when being set or updated.
@ -52,9 +52,9 @@ Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path,
ERR_FAIL_COND_V_MSG(packed_scene.is_null(), ERR_FILE_UNRECOGNIZED, vformat("'%s' is not a valid PackedScene resource.", p_path));
Ref<SceneState> state = packed_scene->get_state();
Vector<String> parsed_strings;
Vector<Pair<NodePath, bool>> atr_owners;
Vector<String> tabcontainer_paths;
for (int i = 0; i < state->get_node_count(); i++) {
String node_type = state->get_node_type(i);
String parent_path = state->get_node_path(i, true);
@ -122,10 +122,9 @@ Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path,
if (auto_translating && !tabcontainer_paths.is_empty() && ClassDB::is_parent_class(node_type, "Control") &&
parent_path == tabcontainer_paths[tabcontainer_paths.size() - 1]) {
parsed_strings.push_back(state->get_node_name(i));
r_translations->push_back({ state->get_node_name(i) });
}
}
if (!auto_translating) {
continue;
}
@ -151,11 +150,7 @@ Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path,
String extension = s->get_language()->get_extension();
if (EditorTranslationParser::get_singleton()->can_parse(extension)) {
Vector<String> temp;
Vector<Vector<String>> ids_context_plural;
EditorTranslationParser::get_singleton()->get_parser(extension)->parse_file(s->get_path(), &temp, &ids_context_plural);
parsed_strings.append_array(temp);
r_ids_ctx_plural->append_array(ids_context_plural);
EditorTranslationParser::get_singleton()->get_parser(extension)->parse_file(s->get_path(), r_translations);
}
} else if (node_type == "FileDialog" && property_name == "filters") {
// Extract FileDialog's filters property with values in format "*.png ; PNG Images","*.gd ; GDScript Files".
@ -163,21 +158,19 @@ Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path,
for (int k = 0; k < str_values.size(); k++) {
String desc = str_values[k].get_slice(";", 1).strip_edges();
if (!desc.is_empty()) {
parsed_strings.push_back(desc);
r_translations->push_back({ desc });
}
}
} else if (property_value.get_type() == Variant::STRING) {
String str_value = String(property_value);
// Prevent reading text containing only spaces.
if (!str_value.strip_edges().is_empty()) {
parsed_strings.push_back(str_value);
r_translations->push_back({ str_value });
}
}
}
}
r_ids->append_array(parsed_strings);
return OK;
}

View File

@ -42,7 +42,7 @@ class PackedSceneEditorTranslationParserPlugin : public EditorTranslationParserP
HashMap<String, Vector<String>> exception_list;
public:
virtual Error parse_file(const String &p_path, Vector<String> *r_ids, Vector<Vector<String>> *r_ids_ctx_plural) override;
virtual Error parse_file(const String &p_path, Vector<Vector<String>> *r_translations) override;
bool match_property(const String &p_property_name, const String &p_node_type);
virtual void get_recognized_extensions(List<String> *r_extensions) const override;

View File

@ -67,31 +67,24 @@ 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> msgids;
Vector<Vector<String>> msgids_context_plural;
Vector<String> msgids_comment;
Vector<String> msgids_context_plural_comment;
Vector<Vector<String>> translations;
const 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, &msgids, &msgids_context_plural);
EditorTranslationParser::get_singleton()->get_parser(file_extension)->get_comments(&msgids_comment, &msgids_context_plural_comment);
EditorTranslationParser::get_singleton()->get_parser(file_extension)->parse_file(file_path, &translations);
} else {
ERR_PRINT("Unrecognized file extension " + file_extension + " in generate_pot()");
return;
}
for (int j = 0; j < msgids_context_plural.size(); j++) {
const Vector<String> &entry = msgids_context_plural[j];
const String &comment = (j < msgids_context_plural_comment.size()) ? msgids_context_plural_comment[j] : String();
_add_new_msgid(entry[0], entry[1], entry[2], file_path, comment);
}
for (int j = 0; j < msgids.size(); j++) {
const String &comment = (j < msgids_comment.size()) ? msgids_comment[j] : String();
_add_new_msgid(msgids[j], "", "", file_path, comment);
for (const Vector<String> &translation : translations) {
ERR_CONTINUE(translation.is_empty());
const String &msgctxt = (translation.size() > 1) ? translation[1] : String();
const String &msgid_plural = (translation.size() > 2) ? translation[2] : String();
const String &comment = (translation.size() > 3) ? translation[3] : String();
_add_new_msgid(translation[0], msgctxt, msgid_plural, file_path, comment);
}
}