Skip to content

Commit bcc9717

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 44dda20 commit bcc9717

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

addons/block_code/code_generation/block_definition.gd

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ const FORMAT_STRING_PATTERN = "\\[(?<out_parameter>[^\\]]+)\\]|\\{const (?<const
6767
## Empty except for blocks that have a defined scope.
6868
var scope: String
6969

70+
## Optional property name, for localizing it. Only relevant for property setters, changers and
71+
## getters.
72+
var property_name: String
73+
7074
static var _display_template_regex := RegEx.create_from_string(FORMAT_STRING_PATTERN)
7175

7276

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

199203
static func new_property_setter(_class_name: String, property: Dictionary, category: String, default_value: Variant) -> Resource:
200204
var type_string: String = Types.VARIANT_TYPE_TO_STRING[property.type]
201-
return new(
205+
var block_definition: Resource = new(
202206
&"%s_set_%s" % [_class_name, property.name],
203207
_class_name,
204208
"Set the %s property" % property.name,
205209
category,
206210
Types.BlockType.STATEMENT,
207211
TYPE_NIL,
208-
"set %s to {value: %s}" % [property.name.capitalize().to_lower(), type_string],
212+
"set %%s to {value: %s}" % type_string,
209213
"%s = {value}" % property.name,
210214
{"value": default_value},
211215
)
216+
block_definition.property_name = property.name
217+
return block_definition
212218

213219

214220
static func new_property_changer(_class_name: String, property: Dictionary, category: String, default_value: Variant) -> Resource:
215221
var type_string: String = Types.VARIANT_TYPE_TO_STRING[property.type]
216-
return new(
222+
var block_definition: Resource = new(
217223
&"%s_change_%s" % [_class_name, property.name],
218224
_class_name,
219225
"Change the %s property" % property.name,
220226
category,
221227
Types.BlockType.STATEMENT,
222228
TYPE_NIL,
223-
"change %s by {value: %s}" % [property.name.capitalize().to_lower(), type_string],
229+
"change %%s by {value: %s}" % type_string,
224230
"%s += {value}" % property.name,
225231
{"value": default_value},
226232
)
233+
block_definition.property_name = property.name
234+
return block_definition
227235

228236

229237
static func new_property_getter(_class_name: String, property: Dictionary, category: String) -> Resource:
230-
return new(
238+
var block_definition: Resource = new(
231239
&"%s_get_%s" % [_class_name, property.name],
232240
_class_name,
233241
"The %s property" % property.name,
234242
category,
235243
Types.BlockType.VALUE,
236244
property.type,
237-
"%s" % property.name.capitalize().to_lower(),
245+
"%s",
238246
"%s" % property.name,
239247
)
248+
block_definition.property_name = property.name
249+
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)