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 11 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
295 changes: 295 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2458,3 +2458,298 @@ 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",
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
"attributes": {
"message": {
"type": "string",
"source": "html",
"selector": ".message"
}
},
"styleVariations": [
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
{ "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 variation"
"""
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 variation"
"""
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"
}
},
"styleVariations": [
{ "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"
}
},
"styleVariations": [
{ "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"
}
},
"styleVariations": [
{ "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"
"""
71 changes: 71 additions & 0 deletions src/BlockExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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}" );
swissspidy marked this conversation as resolved.
Show resolved Hide resolved

$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()
)
);

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 'styleVariations':
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
if ( ! is_array( $original ) ) {
continue 2;
}

foreach ( $original as $msg ) {
$translation = $translations->insert( 'block style variation', $msg['label'] );
$translation->addReference( $file );
}
}
}
}
}
10 changes: 9 additions & 1 deletion src/IterableCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ trait IterableCodeExtractor {
* @param array $options {
* Optional. An array of options passed down to static::fromString()
*
* @type bool $wpExtractTemplates Extract 'Template Name' headers in theme files. Default 'false'.
* @type bool $wpExtractTemplates Extract 'Template Name' headers in theme files. Default 'false'.
* @type array $restrictFileNames Skip all files which are not included in this array.
* }
* @return null
*/
public static function fromFile( $file, Translations $translations, array $options = [] ) {
foreach ( static::getFiles( $file ) as $f ) {
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
if ( ! empty( $options['restrictFileNames'] ) ) {
$basename = Utils\basename( $f );
if ( ! in_array( $basename, $options['restrictFileNames'], true ) ) {
continue;
}
}

// Make sure a relative file path is added as a comment.
$options['file'] = ltrim( str_replace( static::$dir, '', Utils\normalize_path( $f ) ), '/' );

Expand Down
Loading