Skip to content

Commit 7079c30

Browse files
committed
Add _customize_strings() method to EditorTranslationParserPlugin
1 parent 967e2d4 commit 7079c30

File tree

4 files changed

+72
-14
lines changed

4 files changed

+72
-14
lines changed

doc/classes/EditorTranslationParserPlugin.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,32 @@
9898
}
9999
[/csharp]
100100
[/codeblocks]
101+
Alternatively, the plugin can directly modify the final list of strings, by implementing [method _customize_strings].
101102
To use [EditorTranslationParserPlugin], register it using the [method EditorPlugin.add_translation_parser_plugin] method first.
102103
</description>
103104
<tutorials>
104105
</tutorials>
105106
<methods>
107+
<method name="_customize_strings" qualifiers="virtual const">
108+
<return type="PackedStringArray[]" />
109+
<param index="0" name="strings" type="PackedStringArray[]" />
110+
<description>
111+
Called after parsing all files. You can modify the [param strings] array to add or remove entries from the final list of strings, then return it after modifications. Each entry is a [PackedStringArray] like explained in the [EditorTranslationParserPlugin]'s description.
112+
[codeblock]
113+
@tool
114+
extends EditorTranslationParserPlugin
115+
116+
func _customize_strings(strings):
117+
# Add new string.
118+
strings.append(["Test 1", "context", "test 1 plurals", "test 1 comment"])
119+
120+
# Remove all strings that begin with $.
121+
strings = strings.filter(func(s): return not s[0].begins_with("$"))
122+
123+
return strings
124+
[/codeblock]
125+
</description>
126+
</method>
106127
<method name="_get_recognized_extensions" qualifiers="virtual const">
107128
<return type="PackedStringArray" />
108129
<description>

editor/translations/editor_translation_parser.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,39 @@ void EditorTranslationParserPlugin::get_recognized_extensions(List<String> *r_ex
7979
for (int i = 0; i < extensions.size(); i++) {
8080
r_extensions->push_back(extensions[i]);
8181
}
82-
} else {
82+
} else if (!GDVIRTUAL_IS_OVERRIDDEN(_customize_strings)) {
8383
ERR_PRINT("Custom translation parser plugin's \"func get_recognized_extensions()\" is undefined.");
8484
}
8585
}
8686

87+
void EditorTranslationParserPlugin::customize_strings(Vector<Vector<String>> &r_strings) const {
88+
if (!GDVIRTUAL_IS_OVERRIDDEN(_customize_strings)) {
89+
return;
90+
}
91+
92+
TypedArray<PackedStringArray> new_strings;
93+
new_strings.resize(r_strings.size());
94+
int i = 0;
95+
for (const PackedStringArray &translation : r_strings) {
96+
new_strings[i] = translation;
97+
i++;
98+
}
99+
100+
GDVIRTUAL_CALL(_customize_strings, new_strings, new_strings);
101+
102+
r_strings.resize(new_strings.size());
103+
PackedStringArray *translation_write = r_strings.ptrw();
104+
i = 0;
105+
for (const Variant &translation : new_strings) {
106+
translation_write[i] = translation;
107+
i++;
108+
}
109+
}
110+
87111
void EditorTranslationParserPlugin::_bind_methods() {
88112
GDVIRTUAL_BIND(_parse_file, "path");
89113
GDVIRTUAL_BIND(_get_recognized_extensions);
114+
GDVIRTUAL_BIND(_customize_strings, "strings");
90115

91116
#ifndef DISABLE_DEPRECATED
92117
GDVIRTUAL_BIND_COMPAT(_parse_file_bind_compat_99297, "path", "msgids", "msgids_context_plural");
@@ -151,6 +176,12 @@ Ref<EditorTranslationParserPlugin> EditorTranslationParser::get_parser(const Str
151176
return nullptr;
152177
}
153178

179+
void EditorTranslationParser::customize_strings(Vector<Vector<String>> &r_strings) const {
180+
for (const Ref<EditorTranslationParserPlugin> &parser : custom_parsers) {
181+
parser->customize_strings(r_strings);
182+
}
183+
}
184+
154185
void EditorTranslationParser::add_parser(const Ref<EditorTranslationParserPlugin> &p_parser, ParserType p_type) {
155186
if (p_type == ParserType::STANDARD) {
156187
standard_parsers.push_back(p_parser);

editor/translations/editor_translation_parser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class EditorTranslationParserPlugin : public RefCounted {
4242

4343
GDVIRTUAL1R(TypedArray<PackedStringArray>, _parse_file, String)
4444
GDVIRTUAL0RC(Vector<String>, _get_recognized_extensions)
45+
GDVIRTUAL1RC(TypedArray<PackedStringArray>, _customize_strings, TypedArray<PackedStringArray>)
4546

4647
#ifndef DISABLE_DEPRECATED
4748
GDVIRTUAL3_COMPAT(_parse_file_bind_compat_99297, _parse_file, String, TypedArray<String>, TypedArray<Array>)
@@ -50,6 +51,7 @@ class EditorTranslationParserPlugin : public RefCounted {
5051
public:
5152
virtual Error parse_file(const String &p_path, Vector<Vector<String>> *r_translations);
5253
virtual void get_recognized_extensions(List<String> *r_extensions) const;
54+
void customize_strings(Vector<Vector<String>> &r_strings) const;
5355
};
5456

5557
class EditorTranslationParser {
@@ -69,6 +71,7 @@ class EditorTranslationParser {
6971
void get_recognized_extensions(List<String> *r_extensions) const;
7072
bool can_parse(const String &p_extension) const;
7173
Ref<EditorTranslationParserPlugin> get_parser(const String &p_extension) const;
74+
void customize_strings(Vector<Vector<String>> &r_strings) const;
7275
void add_parser(const Ref<EditorTranslationParserPlugin> &p_parser, ParserType p_type);
7376
void remove_parser(const Ref<EditorTranslationParserPlugin> &p_parser, ParserType p_type);
7477
void clean_parsers();

editor/translations/pot_generator.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,29 @@ void POTGenerator::generate_pot(const String &p_file) {
6666
all_translation_strings.clear();
6767

6868
// Collect all translatable strings according to files order in "POT Generation" setting.
69-
for (int i = 0; i < files.size(); i++) {
70-
Vector<Vector<String>> translations;
71-
72-
const String &file_path = files[i];
73-
String file_extension = file_path.get_extension();
69+
Vector<Vector<String>> translations;
70+
for (const String &file_path : files) {
71+
const String file_extension = file_path.get_extension();
72+
Vector<Vector<String>> new_translations;
7473

7574
if (EditorTranslationParser::get_singleton()->can_parse(file_extension)) {
76-
EditorTranslationParser::get_singleton()->get_parser(file_extension)->parse_file(file_path, &translations);
75+
EditorTranslationParser::get_singleton()->get_parser(file_extension)->parse_file(file_path, &new_translations);
7776
} else {
78-
ERR_PRINT("Unrecognized file extension " + file_extension + " in generate_pot()");
77+
ERR_PRINT("Unrecognized file extension \"" + file_extension + "\" in generate_pot()");
7978
return;
8079
}
8180

82-
for (const Vector<String> &translation : translations) {
83-
ERR_CONTINUE(translation.is_empty());
84-
const String &msgctxt = (translation.size() > 1) ? translation[1] : String();
85-
const String &msgid_plural = (translation.size() > 2) ? translation[2] : String();
86-
const String &comment = (translation.size() > 3) ? translation[3] : String();
87-
_add_new_msgid(translation[0], msgctxt, msgid_plural, file_path, comment);
81+
for (Vector<String> &translation : new_translations) {
82+
translation.resize(5);
83+
translation.write[4] = file_path;
8884
}
85+
translations.append_array(new_translations);
86+
}
87+
EditorTranslationParser::get_singleton()->customize_strings(translations);
88+
89+
for (const Vector<String> &translation : translations) {
90+
ERR_CONTINUE(translation.is_empty());
91+
_add_new_msgid(translation[0], translation[1], translation[2], translation[4], translation[3]);
8992
}
9093

9194
if (GLOBAL_GET("internationalization/locale/translation_add_builtin_strings_to_pot")) {

0 commit comments

Comments
 (0)