Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

block.json string extraction #210

Merged
merged 17 commits into from
Jun 4, 2020
Merged
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
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
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
*/
"""
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
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
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