Skip to content

Commit 7ecd7cb

Browse files
committed
Translate properties
Display localized properties whenever possible. Just like the Inspector does when "Localized" is set from the dropdown menu. For this the property name must be stored in the block definition. Once translated, it is added to the display template.
1 parent d3a1599 commit 7ecd7cb

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

addons/block_code/code_generation/block_definition.gd

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ const FORMAT_STRING_PATTERN = "\\[(?<out_parameter>[^\\]]+)\\]|\\{const (?<const
3232
## [codeblock]
3333
## say {salute: STRING} | {fancy: BOOL}
3434
## [/codeblock]
35+
## If [member property_name] is set, this template is assumed to be a format
36+
## string with a `%s` placeholder; in this case, any literal `%` signs must
37+
## be escaped as `%%`.
3538
@export var display_template: String
3639

3740
## Template for the generated GDScript code. This must be valid GDScript. The
@@ -67,6 +70,10 @@ const FORMAT_STRING_PATTERN = "\\[(?<out_parameter>[^\\]]+)\\]|\\{const (?<const
6770
## Empty except for blocks that have a defined scope.
6871
var scope: String
6972

73+
## Optional property name, for localizing it. Only relevant for property setters, changers and
74+
## getters.
75+
var property_name: String
76+
7077
static var _display_template_regex := RegEx.create_from_string(FORMAT_STRING_PATTERN)
7178

7279

@@ -198,42 +205,48 @@ static func has_category(block_definition, category: String) -> bool:
198205

199206
static func new_property_setter(_class_name: String, property: Dictionary, category: String, default_value: Variant) -> Resource:
200207
var type_string: String = Types.VARIANT_TYPE_TO_STRING[property.type]
201-
return new(
208+
var block_definition: Resource = new(
202209
&"%s_set_%s" % [_class_name, property.name],
203210
_class_name,
204211
"Set the %s property" % property.name,
205212
category,
206213
Types.BlockType.STATEMENT,
207214
TYPE_NIL,
208-
"set %s to {value: %s}" % [property.name.capitalize().to_lower(), type_string],
215+
"set %%s to {value: %s}" % type_string,
209216
"%s = {value}" % property.name,
210217
{"value": default_value},
211218
)
219+
block_definition.property_name = property.name
220+
return block_definition
212221

213222

214223
static func new_property_changer(_class_name: String, property: Dictionary, category: String, default_value: Variant) -> Resource:
215224
var type_string: String = Types.VARIANT_TYPE_TO_STRING[property.type]
216-
return new(
225+
var block_definition: Resource = new(
217226
&"%s_change_%s" % [_class_name, property.name],
218227
_class_name,
219228
"Change the %s property" % property.name,
220229
category,
221230
Types.BlockType.STATEMENT,
222231
TYPE_NIL,
223-
"change %s by {value: %s}" % [property.name.capitalize().to_lower(), type_string],
232+
"change %%s by {value: %s}" % type_string,
224233
"%s += {value}" % property.name,
225234
{"value": default_value},
226235
)
236+
block_definition.property_name = property.name
237+
return block_definition
227238

228239

229240
static func new_property_getter(_class_name: String, property: Dictionary, category: String) -> Resource:
230-
return new(
241+
var block_definition: Resource = new(
231242
&"%s_get_%s" % [_class_name, property.name],
232243
_class_name,
233244
"The %s property" % property.name,
234245
category,
235246
Types.BlockType.VALUE,
236247
property.type,
237-
"%s" % property.name.capitalize().to_lower(),
248+
"%s",
238249
"%s" % property.name,
239250
)
251+
block_definition.property_name = property.name
252+
return block_definition

addons/block_code/ui/blocks/block/block.gd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ func _get_format_string() -> String:
129129
if not definition:
130130
return ""
131131

132+
if definition.property_name:
133+
var domain: TranslationDomain = TranslationServer.get_or_add_domain("godot.properties")
134+
var translated_property: String = domain.translate(definition.property_name.capitalize())
135+
# TODO: Ideally we should be also passing the context. See:
136+
# https://github.com/godotengine/godot/blob/978b38797ba8e8757592f21101e32e364d60662d/editor/editor_property_name_processor.cpp#L90
137+
return tr(definition.display_template) % translated_property.to_lower()
138+
132139
return tr(definition.display_template)
133140

134141

0 commit comments

Comments
 (0)