Make WordPress Core

Changeset 49981


Ignore:
Timestamp:
01/19/2021 11:04:03 AM (6 years ago)
Author:
gziolo
Message:

Blocks: Add i18n support to register_block_type_from_metadata

Related Gutenberg issue: https://github.com/WordPress/gutenberg/issues/23636.
Related WP-CLI PR: https://github.com/wp-cli/i18n-command/pull/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.

Location:
trunk
Files:
7 added
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r49948 r49981  
    135135                $script_asset['version']
    136136        );
    137         return $result ? $script_handle : false;
     137        if ( ! $result ) {
     138                return false;
     139        }
     140
     141        if ( ! empty( $metadata['textdomain'] ) ) {
     142                wp_set_script_translations( $script_handle, $metadata['textdomain'] );
     143        }
     144
     145        return $script_handle;
    138146}
    139147
     
    230238        foreach ( $property_mappings as $key => $mapped_key ) {
    231239                if ( isset( $metadata[ $key ] ) ) {
    232                         $settings[ $mapped_key ] = $metadata[ $key ];
     240                        $value = $metadata[ $key ];
     241                        if ( empty( $metadata['textdomain'] ) ) {
     242                                $settings[ $mapped_key ] = $value;
     243                                continue;
     244                        }
     245                        $textdomain = $metadata['textdomain'];
     246                        switch ( $key ) {
     247                                case 'title':
     248                                case 'description':
     249                                        // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralContext,WordPress.WP.I18n.NonSingularStringLiteralDomain
     250                                        $settings[ $mapped_key ] = translate_with_gettext_context( $value, sprintf( 'block %s', $key ), $textdomain );
     251                                        break;
     252                                case 'keywords':
     253                                        $settings[ $mapped_key ] = array();
     254                                        if ( ! is_array( $value ) ) {
     255                                                continue 2;
     256                                        }
     257
     258                                        foreach ( $value as $keyword ) {
     259                                                // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain
     260                                                $settings[ $mapped_key ][] = translate_with_gettext_context( $keyword, 'block keyword', $textdomain );
     261                                        }
     262
     263                                        break;
     264                                case 'styles':
     265                                        $settings[ $mapped_key ] = array();
     266                                        if ( ! is_array( $value ) ) {
     267                                                continue 2;
     268                                        }
     269
     270                                        foreach ( $value as $style ) {
     271                                                if ( ! empty( $style['label'] ) ) {
     272                                                        // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain
     273                                                        $style['label'] = translate_with_gettext_context( $style['label'], 'block style label', $textdomain );
     274                                                }
     275                                                $settings[ $mapped_key ][] = $style;
     276                                        }
     277
     278                                        break;
     279                                default:
     280                                        $settings[ $mapped_key ] = $value;
     281                        }
    233282                }
    234283        }
  • trunk/tests/phpunit/tests/blocks/register.php

    r49948 r49981  
    6363                $registry = WP_Block_Type_Registry::get_instance();
    6464
    65                 foreach ( array( 'core/test-static', 'core/test-dynamic', 'my-plugin/notice' ) as $block_name ) {
     65                foreach ( array( 'core/test-static', 'core/test-dynamic', 'tests/notice' ) as $block_name ) {
    6666                        if ( $registry->is_registered( $block_name ) ) {
    6767                                $registry->unregister( $block_name );
     
    194194                        'file'   => __FILE__,
    195195                        'name'   => 'unit-tests/test-block',
    196                         'script' => 'file:./fixtures/missing-asset.js',
     196                        'script' => 'file:./blocks/notice/missing-asset.js',
    197197                );
    198198                $result   = register_block_script_handle( $metadata, 'script' );
     
    218218        function test_success_register_block_script_handle() {
    219219                $metadata = array(
    220                         'file'   => __FILE__,
     220                        'file'   => DIR_TESTDATA . '/blocks/notice/block.json',
    221221                        'name'   => 'unit-tests/test-block',
    222                         'script' => 'file:./fixtures/block.js',
     222                        'script' => 'file:./block.js',
    223223                );
    224224                $result   = register_block_script_handle( $metadata, 'script' );
     
    263263        function test_success_register_block_style_handle() {
    264264                $metadata = array(
    265                         'file'  => __FILE__,
     265                        'file'  => DIR_TESTDATA . '/blocks/notice/block.json',
    266266                        'name'  => 'unit-tests/test-block',
    267                         'style' => 'file:./fixtures/block.css',
     267                        'style' => 'file:./block.css',
    268268                );
    269269                $result   = register_block_style_handle( $metadata, 'style' );
     
    304304        function test_block_registers_with_metadata_fixture() {
    305305                $result = register_block_type_from_metadata(
    306                         __DIR__ . '/fixtures'
     306                        DIR_TESTDATA . '/blocks/notice'
    307307                );
    308308
    309309                $this->assertInstanceOf( 'WP_Block_Type', $result );
    310310                $this->assertSame( 2, $result->api_version );
    311                 $this->assertSame( 'my-plugin/notice', $result->name );
     311                $this->assertSame( 'tests/notice', $result->name );
    312312                $this->assertSame( 'Notice', $result->title );
    313313                $this->assertSame( 'common', $result->category );
     
    328328                $this->assertSame(
    329329                        array(
    330                                 'my-plugin/message' => 'message',
     330                                'tests/message' => 'message',
    331331                        ),
    332332                        $result->provides_context
     
    362362                        $result->example
    363363                );
    364                 $this->assertSame( 'my-plugin-notice-editor-script', $result->editor_script );
    365                 $this->assertSame( 'my-plugin-notice-script', $result->script );
    366                 $this->assertSame( 'my-plugin-notice-editor-style', $result->editor_style );
    367                 $this->assertSame( 'my-plugin-notice-style', $result->style );
     364                $this->assertSame( 'tests-notice-editor-script', $result->editor_script );
     365                $this->assertSame( 'tests-notice-script', $result->script );
     366                $this->assertSame( 'tests-notice-editor-style', $result->editor_style );
     367                $this->assertSame( 'tests-notice-style', $result->style );
     368        }
     369
     370        /**
     371         * @ticket 52301
     372         */
     373        function test_block_registers_with_metadata_i18n_support() {
     374                function filter_set_locale_to_polish() {
     375                        return 'pl_PL';
     376                }
     377                add_filter( 'locale', 'filter_set_locale_to_polish' );
     378                load_textdomain( 'notice', WP_LANG_DIR . '/plugins/notice-pl_PL.mo' );
     379
     380                $result = register_block_type_from_metadata(
     381                        DIR_TESTDATA . '/blocks/notice'
     382                );
     383
     384                unload_textdomain( 'notice' );
     385                remove_filter( 'locale', 'filter_set_locale_to_polish' );
     386
     387                $this->assertInstanceOf( 'WP_Block_Type', $result );
     388                $this->assertSame( 'tests/notice', $result->name );
     389                $this->assertSame( 'Powiadomienie', $result->title );
     390                $this->assertSame( 'Wyświetla ostrzeżenie, błąd lub powiadomienie o sukcesie…', $result->description );
     391                $this->assertSameSets( array( 'ostrzeżenie', 'wiadomość' ), $result->keywords );
     392                $this->assertSame(
     393                        array(
     394                                array(
     395                                        'name'      => 'default',
     396                                        'label'     => 'Domyślny',
     397                                        'isDefault' => true,
     398                                ),
     399                                array(
     400                                        'name'  => 'other',
     401                                        'label' => 'Inny',
     402                                ),
     403                        ),
     404                        $result->styles
     405                );
    368406        }
    369407
     
    434472                add_filter( 'block_type_metadata', $filter_metadata_registration, 10, 2 );
    435473                $result = register_block_type_from_metadata(
    436                         __DIR__ . '/fixtures'
     474                        DIR_TESTDATA . '/blocks/notice'
    437475                );
    438476                remove_filter( 'block_type_metadata', $filter_metadata_registration );
     
    452490                add_filter( 'block_type_metadata_settings', $filter_metadata_registration, 10, 2 );
    453491                $result = register_block_type_from_metadata(
    454                         __DIR__ . '/fixtures'
     492                        DIR_TESTDATA . '/blocks/notice'
    455493                );
    456494                remove_filter( 'block_type_metadata_settings', $filter_metadata_registration );
Note: See TracChangeset for help on using the changeset viewer.