WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/19/2021 11:04:03 AM (3 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.