From edd77c0791b14a657caa395acfbf6c9a3584ce2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Tue, 19 Jan 2021 11:04:03 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@49981 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/blocks.php | 53 +++++++++++++- .../blocks/notice}/block.asset.php | 0 .../fixtures => data/blocks/notice}/block.css | 0 .../fixtures => data/blocks/notice}/block.js | 0 .../blocks/notice}/block.json | 14 ++-- .../data/languages/plugins/notice-pl_PL.mo | Bin 0 -> 986 bytes .../data/languages/plugins/notice-pl_PL.po | 43 +++++++++++ tests/phpunit/tests/blocks/register.php | 68 ++++++++++++++---- 8 files changed, 154 insertions(+), 24 deletions(-) rename tests/phpunit/{tests/blocks/fixtures => data/blocks/notice}/block.asset.php (100%) rename tests/phpunit/{tests/blocks/fixtures => data/blocks/notice}/block.css (100%) rename tests/phpunit/{tests/blocks/fixtures => data/blocks/notice}/block.js (100%) rename tests/phpunit/{tests/blocks/fixtures => data/blocks/notice}/block.json (71%) create mode 100644 tests/phpunit/data/languages/plugins/notice-pl_PL.mo create mode 100644 tests/phpunit/data/languages/plugins/notice-pl_PL.po diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 1b5427340f433..ace9811f32e1b 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -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; } /** @@ -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; + } } } diff --git a/tests/phpunit/tests/blocks/fixtures/block.asset.php b/tests/phpunit/data/blocks/notice/block.asset.php similarity index 100% rename from tests/phpunit/tests/blocks/fixtures/block.asset.php rename to tests/phpunit/data/blocks/notice/block.asset.php diff --git a/tests/phpunit/tests/blocks/fixtures/block.css b/tests/phpunit/data/blocks/notice/block.css similarity index 100% rename from tests/phpunit/tests/blocks/fixtures/block.css rename to tests/phpunit/data/blocks/notice/block.css diff --git a/tests/phpunit/tests/blocks/fixtures/block.js b/tests/phpunit/data/blocks/notice/block.js similarity index 100% rename from tests/phpunit/tests/blocks/fixtures/block.js rename to tests/phpunit/data/blocks/notice/block.js diff --git a/tests/phpunit/tests/blocks/fixtures/block.json b/tests/phpunit/data/blocks/notice/block.json similarity index 71% rename from tests/phpunit/tests/blocks/fixtures/block.json rename to tests/phpunit/data/blocks/notice/block.json index 4cd2b4cd6912b..9a0ecc018da64 100644 --- a/tests/phpunit/tests/blocks/fixtures/block.json +++ b/tests/phpunit/data/blocks/notice/block.json @@ -1,13 +1,13 @@ { "apiVersion": 2, - "name": "my-plugin/notice", + "name": "tests/notice", "title": "Notice", "category": "common", "parent": [ "core/group" ], "providesContext": { - "my-plugin/message": "message" + "tests/message": "message" }, "usesContext": [ "groupId" @@ -18,7 +18,7 @@ "alert", "message" ], - "textDomain": "my-plugin", + "textdomain": "notice", "attributes": { "message": { "type": "string", @@ -46,8 +46,8 @@ "message": "This is a notice!" } }, - "editorScript": "my-plugin-notice-editor-script", - "script": "my-plugin-notice-script", - "editorStyle": "my-plugin-notice-editor-style", - "style": "my-plugin-notice-style" + "editorScript": "tests-notice-editor-script", + "script": "tests-notice-script", + "editorStyle": "tests-notice-editor-style", + "style": "tests-notice-style" } diff --git a/tests/phpunit/data/languages/plugins/notice-pl_PL.mo b/tests/phpunit/data/languages/plugins/notice-pl_PL.mo new file mode 100644 index 0000000000000000000000000000000000000000..284872cf6f88ad9f36ae6ac347cd0372d56b5d04 GIT binary patch literal 986 zcmZ8fO=}ZD7+y6hM!i%-1P|j)F&nenB4yVgXj>_^c4_0sZJF%8ZN^S!mYIoZJc%G) zyn5@E$1F7vL`N z6}SZa0WJg86P|Y!%)nQ`5qJT-4gL!da1VZd%JVLOKZ47?pTXzA-@z5|5Aa3sPcTZn zlfY@9%uUxB7)Z}nIjh%}I@@PC+02N-39TwS9o@IA&y5mlug1t2Z5Te=%QCW-DeVM| z->*MSiuY;I*CwxUNyhyb>!O11(Ijf!KoXO@O|r5=ySyi-`;TX~T!+l0!U-p-@_2Nl zml*wmGUu=7{!=m*!$z!{Z28;tQk0&*!W~5{Y=nW|2>f7)19J{WWd6He#w6 zxwaX`7_`hZZzy)X$%32ly#CQxa=Rg{i&&b*DaGkQ6x6~D=G0JSAWqfDuPq^^Ql)h< z4bPm~Viw~Cq}(}!W~JG2T{$sFQJP1`<|sNjX;v`yF=q?v<+=;?{GDD=XoE2aWN`}? z0++(cu|BhVK?(2TK4%*35I0#Tfl7X()X94`7#{WoImwx}&b*@GH&TLXtUY{l^fqU* v*Jg$83yyis_registered( $block_name ) ) { $registry->unregister( $block_name ); } @@ -193,7 +193,7 @@ function test_missing_asset_file_register_block_script_handle() { $metadata = array( 'file' => __FILE__, 'name' => 'unit-tests/test-block', - 'script' => 'file:./fixtures/missing-asset.js', + 'script' => 'file:./blocks/notice/missing-asset.js', ); $result = register_block_script_handle( $metadata, 'script' ); @@ -217,9 +217,9 @@ function test_handle_passed_register_block_script_handle() { */ function test_success_register_block_script_handle() { $metadata = array( - 'file' => __FILE__, + 'file' => DIR_TESTDATA . '/blocks/notice/block.json', 'name' => 'unit-tests/test-block', - 'script' => 'file:./fixtures/block.js', + 'script' => 'file:./block.js', ); $result = register_block_script_handle( $metadata, 'script' ); @@ -262,9 +262,9 @@ function test_handle_passed_register_block_style_handle() { */ function test_success_register_block_style_handle() { $metadata = array( - 'file' => __FILE__, + 'file' => DIR_TESTDATA . '/blocks/notice/block.json', 'name' => 'unit-tests/test-block', - 'style' => 'file:./fixtures/block.css', + 'style' => 'file:./block.css', ); $result = register_block_style_handle( $metadata, 'style' ); @@ -303,12 +303,12 @@ function test_metadata_not_found_in_the_current_directory() { */ function test_block_registers_with_metadata_fixture() { $result = register_block_type_from_metadata( - __DIR__ . '/fixtures' + DIR_TESTDATA . '/blocks/notice' ); $this->assertInstanceOf( 'WP_Block_Type', $result ); $this->assertSame( 2, $result->api_version ); - $this->assertSame( 'my-plugin/notice', $result->name ); + $this->assertSame( 'tests/notice', $result->name ); $this->assertSame( 'Notice', $result->title ); $this->assertSame( 'common', $result->category ); $this->assertSameSets( array( 'core/group' ), $result->parent ); @@ -327,7 +327,7 @@ function test_block_registers_with_metadata_fixture() { ); $this->assertSame( array( - 'my-plugin/message' => 'message', + 'tests/message' => 'message', ), $result->provides_context ); @@ -361,10 +361,48 @@ function test_block_registers_with_metadata_fixture() { ), $result->example ); - $this->assertSame( 'my-plugin-notice-editor-script', $result->editor_script ); - $this->assertSame( 'my-plugin-notice-script', $result->script ); - $this->assertSame( 'my-plugin-notice-editor-style', $result->editor_style ); - $this->assertSame( 'my-plugin-notice-style', $result->style ); + $this->assertSame( 'tests-notice-editor-script', $result->editor_script ); + $this->assertSame( 'tests-notice-script', $result->script ); + $this->assertSame( 'tests-notice-editor-style', $result->editor_style ); + $this->assertSame( 'tests-notice-style', $result->style ); + } + + /** + * @ticket 52301 + */ + function test_block_registers_with_metadata_i18n_support() { + function filter_set_locale_to_polish() { + return 'pl_PL'; + } + add_filter( 'locale', 'filter_set_locale_to_polish' ); + load_textdomain( 'notice', WP_LANG_DIR . '/plugins/notice-pl_PL.mo' ); + + $result = register_block_type_from_metadata( + DIR_TESTDATA . '/blocks/notice' + ); + + unload_textdomain( 'notice' ); + remove_filter( 'locale', 'filter_set_locale_to_polish' ); + + $this->assertInstanceOf( 'WP_Block_Type', $result ); + $this->assertSame( 'tests/notice', $result->name ); + $this->assertSame( 'Powiadomienie', $result->title ); + $this->assertSame( 'Wyświetla ostrzeżenie, błąd lub powiadomienie o sukcesie…', $result->description ); + $this->assertSameSets( array( 'ostrzeżenie', 'wiadomość' ), $result->keywords ); + $this->assertSame( + array( + array( + 'name' => 'default', + 'label' => 'Domyślny', + 'isDefault' => true, + ), + array( + 'name' => 'other', + 'label' => 'Inny', + ), + ), + $result->styles + ); } /** @@ -433,7 +471,7 @@ public function test_filter_block_registration_metadata() { add_filter( 'block_type_metadata', $filter_metadata_registration, 10, 2 ); $result = register_block_type_from_metadata( - __DIR__ . '/fixtures' + DIR_TESTDATA . '/blocks/notice' ); remove_filter( 'block_type_metadata', $filter_metadata_registration ); @@ -451,7 +489,7 @@ public function test_filter_block_registration_metadata_settings() { add_filter( 'block_type_metadata_settings', $filter_metadata_registration, 10, 2 ); $result = register_block_type_from_metadata( - __DIR__ . '/fixtures' + DIR_TESTDATA . '/blocks/notice' ); remove_filter( 'block_type_metadata_settings', $filter_metadata_registration );