Skip to content

Strings from block.json are not translated in Gutenberg #305

@code-flow

Description

@code-flow

Hey there,
I guess I've found an issue regarding the translation of block.json content. I'm not quite sure where the exact problem is: WP-CLI or the Gutenberg project?

Summary:
After generating PO- and JSON-Files with the according CLI-commands Gutenberg does not translate strings from block.json.

Steps to reproduce:

  1. Install a Test block
    cd /wp-content/plugins
    npx @wordpress/create-block my-test-block

  2. Edit /src/index.js to use block-meta:

import metadata from './block.json';
registerBlockType(metadata, {
	edit: Edit,
	save,
});
  1. Fix editorScript path in block.json (otherwise MD5-hashes are not correct)
{
	"editorScript": "file:index.js",
	...
}
  1. Create POT
    cd /my-test-block
    wp i18n make-pot . languages/my-test-block.pot --skip-php --exclude=build,node_modules

  2. Create PO-File and translate (at least the strings from the block.json)

  3. Create JSON-File:
    cd languages
    wp i18n make-json my-test-block-de_DE.po ./ --no-purge '--use-map={ "src/index.js": "build/index.js", "src/save.js": "build/index.js", "src/edit.js": "build/index.js", "src/block.json": "build/index.js" }'

  4. Run build
    cd wp-content/plugins/my-test-block
    npm run build

  5. Fix path in my-test.block.php

add_filter(
	'load_script_translation_file',
	function ( string $file, string $handle, string $domain ) {
		if ( 'my-test-block' !== $domain ) {
			return (string) $file;
		}

		return plugin_dir_path( __FILE__ ) . 'languages/' . basename( $file );
	},
	10,
	3
);
  1. Activate plugin and add the block into a post. Set language accordingly.
    The title is not translated.

Environment
wp cli info

OS:     Darwin 21.3.0 Darwin Kernel Version 21.3.0: Wed Jan  5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_ARM64_T8101 arm64
Shell:  /bin/zsh
PHP binary:     /opt/homebrew/Cellar/php@7.4/7.4.28.reinstall/bin/php
PHP version:    7.4.28
php.ini used:   /opt/homebrew/etc/php/7.4/php.ini
MySQL binary:   
MySQL version:  
SQL modes:      
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /Users/fs/Local Sites/meinwp/app/public/wp-content/plugins/my-test-block/languages
WP-CLI packages dir:    
WP-CLI global config:   /Users/fs/.wp-cli/config.yml
WP-CLI project config:  
WP-CLI version: 2.6.0

Expected Result
I would expect that the block title and the description gets translated.

Actual Results
Title and description gets not translated.

Any help is much appreciated.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions