Skip to content

Commit edd77c0

Browse files
committed
Blocks: Add i18n support to register_block_type_from_metadata
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. git-svn-id: https://develop.svn.wordpress.org/trunk@49981 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 2651809 commit edd77c0

File tree

8 files changed

+154
-24
lines changed

8 files changed

+154
-24
lines changed

src/wp-includes/blocks.php

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,15 @@ function register_block_script_handle( $metadata, $field_name ) {
134134
$script_asset['dependencies'],
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

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

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
}
235284

File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/phpunit/tests/blocks/fixtures/block.json renamed to tests/phpunit/data/blocks/notice/block.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"apiVersion": 2,
3-
"name": "my-plugin/notice",
3+
"name": "tests/notice",
44
"title": "Notice",
55
"category": "common",
66
"parent": [
77
"core/group"
88
],
99
"providesContext": {
10-
"my-plugin/message": "message"
10+
"tests/message": "message"
1111
},
1212
"usesContext": [
1313
"groupId"
@@ -18,7 +18,7 @@
1818
"alert",
1919
"message"
2020
],
21-
"textDomain": "my-plugin",
21+
"textdomain": "notice",
2222
"attributes": {
2323
"message": {
2424
"type": "string",
@@ -46,8 +46,8 @@
4646
"message": "This is a notice!"
4747
}
4848
},
49-
"editorScript": "my-plugin-notice-editor-script",
50-
"script": "my-plugin-notice-script",
51-
"editorStyle": "my-plugin-notice-editor-style",
52-
"style": "my-plugin-notice-style"
49+
"editorScript": "tests-notice-editor-script",
50+
"script": "tests-notice-script",
51+
"editorStyle": "tests-notice-editor-style",
52+
"style": "tests-notice-style"
5353
}
986 Bytes
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
msgid ""
2+
msgstr ""
3+
"Project-Id-Version: \n"
4+
"POT-Creation-Date: 2015-12-31 16:31+0100\n"
5+
"PO-Revision-Date: 2021-01-14 18:26+0100\n"
6+
"Language: pl_PL\n"
7+
"MIME-Version: 1.0\n"
8+
"Content-Type: text/plain; charset=UTF-8\n"
9+
"Content-Transfer-Encoding: 8bit\n"
10+
"X-Generator: Poedit 2.4.2\n"
11+
"X-Poedit-Basepath: .\n"
12+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
13+
"X-Poedit-KeywordsList: __;_e;_x:1,2c;_ex:1,2c;_n:1,2;_nx:1,2,4c;_n_noop:1,2;"
14+
"_nx_noop:1,2,3c;esc_attr__;esc_html__;esc_attr_e;esc_html_e;esc_attr_x:1,2c;"
15+
"esc_html_x:1,2c\n"
16+
"X-Textdomain-Support: yes\n"
17+
"Last-Translator: \n"
18+
"Language-Team: \n"
19+
"X-Poedit-SearchPath-0: .\n"
20+
21+
msgctxt "block title"
22+
msgid "Notice"
23+
msgstr "Powiadomienie"
24+
25+
msgctxt "block description"
26+
msgid "Shows warning, error or success notices…"
27+
msgstr "Wyświetla ostrzeżenie, błąd lub powiadomienie o sukcesie…"
28+
29+
msgctxt "block keyword"
30+
msgid "alert"
31+
msgstr "ostrzeżenie"
32+
33+
msgctxt "block keyword"
34+
msgid "message"
35+
msgstr "wiadomość"
36+
37+
msgctxt "block style label"
38+
msgid "Default"
39+
msgstr "Domyślny"
40+
41+
msgctxt "block style label"
42+
msgid "Other"
43+
msgstr "Inny"

tests/phpunit/tests/blocks/register.php

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function tearDown() {
6262

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 );
6868
}
@@ -193,7 +193,7 @@ function test_missing_asset_file_register_block_script_handle() {
193193
$metadata = array(
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' );
199199

@@ -217,9 +217,9 @@ function test_handle_passed_register_block_script_handle() {
217217
*/
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' );
225225

@@ -262,9 +262,9 @@ function test_handle_passed_register_block_style_handle() {
262262
*/
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' );
270270

@@ -303,12 +303,12 @@ function test_metadata_not_found_in_the_current_directory() {
303303
*/
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 );
314314
$this->assertSameSets( array( 'core/group' ), $result->parent );
@@ -327,7 +327,7 @@ function test_block_registers_with_metadata_fixture() {
327327
);
328328
$this->assertSame(
329329
array(
330-
'my-plugin/message' => 'message',
330+
'tests/message' => 'message',
331331
),
332332
$result->provides_context
333333
);
@@ -361,10 +361,48 @@ function test_block_registers_with_metadata_fixture() {
361361
),
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

370408
/**
@@ -433,7 +471,7 @@ public function test_filter_block_registration_metadata() {
433471

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 );
439477

@@ -451,7 +489,7 @@ public function test_filter_block_registration_metadata_settings() {
451489

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 );
457495

0 commit comments

Comments
 (0)