Skip to content

Commit

Permalink
Merge pull request #210 from wp-cli/add/blockjson
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera authored Jun 4, 2020
2 parents 4a6c73f + 7ae45a3 commit 0218790
Show file tree
Hide file tree
Showing 7 changed files with 426 additions and 25 deletions.
294 changes: 294 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2458,3 +2458,297 @@ Feature: Generate a POT file of a WordPress project
Success: POT file successfully generated!
"""
And the contents of the result.pot file should match /^msgid/

Scenario: Extract strings from block.json files
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.php file:
"""
<?php
/**
* Plugin Name: Foo Plugin
*/
"""
And a foo-plugin/block.json file:
"""
{
"name": "my-plugin/notice",
"title": "Notice",
"category": "common",
"parent": [ "core/group" ],
"icon": "star",
"description": "Shows warning, error or success notices ...",
"keywords": [ "alert", "message" ],
"textdomain": "foo-plugin",
"attributes": {
"message": {
"type": "string",
"source": "html",
"selector": ".message"
}
},
"styles": [
{ "name": "default", "label": "Default", "isDefault": true },
{ "name": "other", "label": "Other" }
],
"editorScript": "build/editor.js",
"script": "build/main.js",
"editorStyle": "build/editor.css",
"style": "build/style.css"
}
"""

When I try `wp i18n make-pot foo-plugin`
Then STDOUT should be:
"""
Plugin file detected.
Success: POT file successfully generated!
"""
And the foo-plugin/foo-plugin.pot file should exist
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "Foo Plugin"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "block title"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "Notice"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "block description"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "Shows warning, error or success notices ..."
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "block keyword"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "alert"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "message"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "block style label"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "Default"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "block style label"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "Other"
"""

Scenario: Ignores block.json files with other text domain
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.php file:
"""
<?php
/**
* Plugin Name: Foo Plugin
*/
"""
And a foo-plugin/block.json file:
"""
{
"name": "my-plugin/notice",
"title": "Notice",
"category": "common",
"parent": [ "core/group" ],
"icon": "star",
"description": "Shows warning, error or success notices ...",
"keywords": [ "alert", "message" ],
"textdomain": "my-plugin",
"attributes": {
"message": {
"type": "string",
"source": "html",
"selector": ".message"
}
},
"styles": [
{ "name": "default", "label": "Default", "isDefault": true },
{ "name": "other", "label": "Other" }
],
"editorScript": "build/editor.js",
"script": "build/main.js",
"editorStyle": "build/editor.css",
"style": "build/style.css"
}
"""

When I try `wp i18n make-pot foo-plugin`
Then STDOUT should be:
"""
Plugin file detected.
Success: POT file successfully generated!
"""
And the foo-plugin/foo-plugin.pot file should exist
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "Foo Plugin"
"""
And the foo-plugin/foo-plugin.pot file should not contain:
"""
msgid "Notice"
"""
And the foo-plugin/foo-plugin.pot file should not contain:
"""
msgid "Shows warning, error or success notices ..."
"""
And the foo-plugin/foo-plugin.pot file should not contain:
"""
msgid "alert"
"""
And the foo-plugin/foo-plugin.pot file should not contain:
"""
msgid "message"
"""

Scenario: Extract strings from block.json files with no text domain specified
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.php file:
"""
<?php
/**
* Plugin Name: Foo Plugin
*/
"""
And a foo-plugin/block.json file:
"""
{
"name": "my-plugin/notice",
"title": "Notice",
"category": "common",
"parent": [ "core/group" ],
"icon": "star",
"description": "Shows warning, error or success notices ...",
"keywords": [ "alert", "message" ],
"attributes": {
"message": {
"type": "string",
"source": "html",
"selector": ".message"
}
},
"styles": [
{ "name": "default", "label": "Default", "isDefault": true },
{ "name": "other", "label": "Other" }
],
"editorScript": "build/editor.js",
"script": "build/main.js",
"editorStyle": "build/editor.css",
"style": "build/style.css"
}
"""

When I try `wp i18n make-pot foo-plugin`
Then STDOUT should be:
"""
Plugin file detected.
Success: POT file successfully generated!
"""
And the foo-plugin/foo-plugin.pot file should exist
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "Foo Plugin"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "block title"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "Notice"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "block description"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "Shows warning, error or success notices ..."
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "block keyword"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "alert"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "message"
"""

Scenario: Skips block.json file altogether
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.php file:
"""
<?php
/**
* Plugin Name: Foo Plugin
*/
"""
And a foo-plugin/block.json file:
"""
{
"name": "my-plugin/notice",
"title": "Notice",
"category": "common",
"parent": [ "core/group" ],
"icon": "star",
"description": "Shows warning, error or success notices ...",
"keywords": [ "alert", "message" ],
"attributes": {
"message": {
"type": "string",
"source": "html",
"selector": ".message"
}
},
"styles": [
{ "name": "default", "label": "Default", "isDefault": true },
{ "name": "other", "label": "Other" }
],
"editorScript": "build/editor.js",
"script": "build/main.js",
"editorStyle": "build/editor.css",
"style": "build/style.css"
}
"""

When I try `wp i18n make-pot foo-plugin --skip-block-json`
Then STDOUT should be:
"""
Plugin file detected.
Success: POT file successfully generated!
"""
And the foo-plugin/foo-plugin.pot file should exist
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "Foo Plugin"
"""
And the foo-plugin/foo-plugin.pot file should not contain:
"""
msgctxt "block title"
"""
And the foo-plugin/foo-plugin.pot file should not contain:
"""
msgid "Notice"
"""
72 changes: 72 additions & 0 deletions src/BlockExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace WP_CLI\I18n;

use Gettext\Extractors\Extractor;
use Gettext\Extractors\ExtractorInterface;
use Gettext\Translations;
use WP_CLI;

final class BlockExtractor extends Extractor implements ExtractorInterface {
use IterableCodeExtractor;

/**
* @inheritdoc
*/
public static function fromString( $string, Translations $translations, array $options = [] ) {
$file = $options['file'];
WP_CLI::debug( "Parsing file {$file}", 'make-pot' );

$file_data = json_decode( $string, true );

if ( null === $file_data ) {
WP_CLI::debug(
sprintf(
'Could not parse file %1$s: error code %2$s',
$file,
json_last_error()
),
'make-pot'
);

return;
}

$domain = isset( $file_data['textdomain'] ) ? $file_data['textdomain'] : null;

// Allow missing domain, but skip if they don't match.
if ( null !== $domain && $domain !== $translations->getDomain() ) {
return;
}

foreach ( $file_data as $key => $original ) {
switch ( $key ) {
case 'title':
case 'description':
$translation = $translations->insert( sprintf( 'block %s', $key ), $original );
$translation->addReference( $file );
break;
case 'keywords':
if ( ! is_array( $original ) ) {
continue 2;
}

foreach ( $original as $msg ) {
$translation = $translations->insert( 'block keyword', $msg );
$translation->addReference( $file );
}

break;
case 'styles':
if ( ! is_array( $original ) ) {
continue 2;
}

foreach ( $original as $msg ) {
$translation = $translations->insert( 'block style label', $msg['label'] );
$translation->addReference( $file );
}
}
}
}
}
Loading

0 comments on commit 0218790

Please sign in to comment.