Skip to content

Commit

Permalink
Add BlockDefinition translation parser plugin
Browse files Browse the repository at this point in the history
Since BlockDefinitions are generic Resources, Godot doesn't
automatically extract translatable strings from them. In order to do
that, we need to provide an EditorTranslationParserPlugin that extracts
the desired fields so they're included in a POT file.
  • Loading branch information
dbnicholson committed Oct 31, 2024
1 parent 7834706 commit c5b3feb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions addons/block_code/block_code_plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ static var block_code_button: Button
const BlockInspectorPlugin := preload("res://addons/block_code/inspector_plugin/block_script_inspector.gd")
var block_inspector_plugin: BlockInspectorPlugin

const BlockTranslationParserPlugin := preload("res://addons/block_code/translation/parser.gd")
var _tx_parser_plugin: BlockTranslationParserPlugin

var editor_inspector: EditorInspector

var _selected_block_code: BlockCode
Expand Down Expand Up @@ -41,6 +44,10 @@ func _enter_tree():
block_inspector_plugin = BlockInspectorPlugin.new()
add_inspector_plugin(block_inspector_plugin)

if not _tx_parser_plugin:
_tx_parser_plugin = BlockTranslationParserPlugin.new()
add_translation_parser_plugin(_tx_parser_plugin)

# Remove unwanted class nodes from create node
old_feature_profile = EditorInterface.get_current_feature_profile()

Expand Down Expand Up @@ -69,6 +76,7 @@ func script_window_requested(script: String):


func _exit_tree():
remove_translation_parser_plugin(_tx_parser_plugin)
remove_inspector_plugin(block_inspector_plugin)

if block_code_button:
Expand Down
40 changes: 40 additions & 0 deletions addons/block_code/translation/parser.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@tool
## BlockCode translation parser plugin.
##
## Extracts translatable strings from BlockCode resources. Currently only
## BlockDefinition resources are handled.
extends EditorTranslationParserPlugin

const BLOCK_DEFINITION_SCRIPT_PATH := "res://addons/block_code/code_generation/block_definition.gd"

# BlockDefinition properties for translation
const block_def_tx_properties: Array[String] = [
"category",
"description",
"display_template",
]


func _get_recognized_extensions() -> PackedStringArray:
# BlockDefinition resources currently use the generic tres extension.
return ["tres"]


func _resource_is_block_definition(resource: Resource) -> bool:
var script := resource.get_script()
if not script:
return false
return script.resource_path == BLOCK_DEFINITION_SCRIPT_PATH


func _parse_file(path: String, msgids: Array[String], msgids_context_plural: Array[Array]) -> void:
# Only BlockDefinition resources are supported.
var res = ResourceLoader.load(path, "Resource")
if not res or not _resource_is_block_definition(res):
return
for prop in block_def_tx_properties:
var value: String = res.get(prop)
if value:
# For now just the messages are used. It might be better to provide
# context with msgids_context_plural to avoid conflicts.
msgids.append(value)

0 comments on commit c5b3feb

Please sign in to comment.