Skip to content

Commit

Permalink
Blocks: Add i18n support to register_block_type_from_metadata
Browse files Browse the repository at this point in the history
Related Gutenberg issue: WordPress/gutenberg#23636.
Related WP-CLI PR: wp-cli/i18n-command#210.
Related documentation proposal: https://github.com/WordPress/gutenberg/blob/master/docs/designers-developers/developers/block-api/block-metadata.md#internationalization-not-implemented

Adds programatic i18n support to `register_block_type_from_metadata` function for block settings registered from `block.json` file that provides `textdomain` field.


Props swissspidy, ocean90.
Fixes #52301.


Built from https://develop.svn.wordpress.org/trunk@49981


git-svn-id: https://core.svn.wordpress.org/trunk@49682 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
gziolo committed Jan 19, 2021
1 parent ce05b16 commit 3b6e1a3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
53 changes: 51 additions & 2 deletions wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,15 @@ function register_block_script_handle( $metadata, $field_name ) {
$script_asset['dependencies'],
$script_asset['version']
);
return $result ? $script_handle : false;
if ( ! $result ) {
return false;
}

if ( ! empty( $metadata['textdomain'] ) ) {
wp_set_script_translations( $script_handle, $metadata['textdomain'] );
}

return $script_handle;
}

/**
Expand Down Expand Up @@ -229,7 +237,48 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {

foreach ( $property_mappings as $key => $mapped_key ) {
if ( isset( $metadata[ $key ] ) ) {
$settings[ $mapped_key ] = $metadata[ $key ];
$value = $metadata[ $key ];
if ( empty( $metadata['textdomain'] ) ) {
$settings[ $mapped_key ] = $value;
continue;
}
$textdomain = $metadata['textdomain'];
switch ( $key ) {
case 'title':
case 'description':
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralContext,WordPress.WP.I18n.NonSingularStringLiteralDomain
$settings[ $mapped_key ] = translate_with_gettext_context( $value, sprintf( 'block %s', $key ), $textdomain );
break;
case 'keywords':
$settings[ $mapped_key ] = array();
if ( ! is_array( $value ) ) {
continue 2;
}

foreach ( $value as $keyword ) {
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain
$settings[ $mapped_key ][] = translate_with_gettext_context( $keyword, 'block keyword', $textdomain );
}

break;
case 'styles':
$settings[ $mapped_key ] = array();
if ( ! is_array( $value ) ) {
continue 2;
}

foreach ( $value as $style ) {
if ( ! empty( $style['label'] ) ) {
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain
$style['label'] = translate_with_gettext_context( $style['label'], 'block style label', $textdomain );
}
$settings[ $mapped_key ][] = $style;
}

break;
default:
$settings[ $mapped_key ] = $value;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.7-alpha-49980';
$wp_version = '5.7-alpha-49981';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down

0 comments on commit 3b6e1a3

Please sign in to comment.