Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions src/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'] ) ) {

@gziolo gziolo Jan 14, 2021

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ocean90, is it good to assume that if there is no textdomain defined in block.json that this block/plugin isn't meant to be translated?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say so, yes. Would core blocks set it to default? But it actually doesn't seem like it will ever be empty due to the default being default?

$textdomain = isset( $metadata['textdomain'] ) ? $metadata['textdomain'] : 'default';

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I thought I removed this line 😅
Yes, I believe we either explicitly set it to default for core blocks or auto-detect them through core namespace in the block name and auto-apply default this way.

@gziolo gziolo Jan 19, 2021

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this fallback for now as it's way more complicated for blocks than for plugins where we have:

    // If no text domain is defined fall back to the plugin slug.
    if ( ! $plugin_data['TextDomain'] ) {
        $plugin_slug = dirname( plugin_basename( $plugin_file ) );
        if ( '.' !== $plugin_slug && false === strpos( $plugin_slug, '/' ) ) {
            $plugin_data['TextDomain'] = $plugin_slug;
        }
    }

Plugins fall back to the plugin slug, so we could do the same. What do you think?

The challenges I see:

  • block.json can be located anywhere, so the parent folder might be not the best way to pick
  • block.json might be inside the plugin so it would have the textdomain define but the challenge is how to read it
  • block.json might be inside the theme so it would have the textdomain define but the challenge is how to read it
  • name field is constructed from namespace/slug so we could use it in a quite reliable way

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
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -18,7 +18,7 @@
"alert",
"message"
],
"textDomain": "my-plugin",
"textdomain": "notice",
"attributes": {
"message": {
"type": "string",
Expand Down Expand Up @@ -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"
}
Binary file added tests/phpunit/data/languages/plugins/notice-pl_PL.mo
Binary file not shown.
43 changes: 43 additions & 0 deletions tests/phpunit/data/languages/plugins/notice-pl_PL.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2015-12-31 16:31+0100\n"
"PO-Revision-Date: 2021-01-14 18:26+0100\n"
"Language: pl_PL\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.4.2\n"
"X-Poedit-Basepath: .\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-KeywordsList: __;_e;_x:1,2c;_ex:1,2c;_n:1,2;_nx:1,2,4c;_n_noop:1,2;"
"_nx_noop:1,2,3c;esc_attr__;esc_html__;esc_attr_e;esc_html_e;esc_attr_x:1,2c;"
"esc_html_x:1,2c\n"
"X-Textdomain-Support: yes\n"
"Last-Translator: \n"
"Language-Team: \n"
"X-Poedit-SearchPath-0: .\n"

msgctxt "block title"
msgid "Notice"
msgstr "Powiadomienie"

msgctxt "block description"
msgid "Shows warning, error or success notices…"
msgstr "Wyświetla ostrzeżenie, błąd lub powiadomienie o sukcesie…"

msgctxt "block keyword"
msgid "alert"
msgstr "ostrzeżenie"

msgctxt "block keyword"
msgid "message"
msgstr "wiadomość"

msgctxt "block style label"
msgid "Default"
msgstr "Domyślny"

msgctxt "block style label"
msgid "Other"
msgstr "Inny"
68 changes: 53 additions & 15 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function tearDown() {

$registry = WP_Block_Type_Registry::get_instance();

foreach ( array( 'core/test-static', 'core/test-dynamic', 'my-plugin/notice' ) as $block_name ) {
foreach ( array( 'core/test-static', 'core/test-dynamic', 'tests/notice' ) as $block_name ) {
if ( $registry->is_registered( $block_name ) ) {
$registry->unregister( $block_name );
}
Expand Down Expand Up @@ -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' );

Expand All @@ -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' );

Expand Down Expand Up @@ -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' );

Expand Down Expand Up @@ -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 );
Expand All @@ -327,7 +327,7 @@ function test_block_registers_with_metadata_fixture() {
);
$this->assertSame(
array(
'my-plugin/message' => 'message',
'tests/message' => 'message',
),
$result->provides_context
);
Expand Down Expand Up @@ -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() {
Comment thread
gziolo marked this conversation as resolved.
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
);
}

/**
Expand Down Expand Up @@ -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 );

Expand All @@ -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 );

Expand Down