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 5 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
222 changes: 222 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2458,3 +2458,225 @@ 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 keywords"
"""
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: 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 keywords"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "alert"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "message"
"""
61 changes: 61 additions & 0 deletions src/BlockExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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;

public static $options = [
'translatableProperties' => [
'title',
'description',
'keywords',
'styles',
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
],
];

/**
* @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 ) {
if ( ! array_key_exists( $key, $options['translatableProperties'] ) ) {
continue;
}

foreach ( (array) $original as $msg ) {
$translation = $translations->insert( sprintf( 'block %s', $key ), $msg );
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
$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
24 changes: 23 additions & 1 deletion src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Gettext\Merge;
use Gettext\Translation;
use Gettext\Translations;
use Symfony\Component\Finder\SplFileInfo;
use WP_CLI;
use WP_CLI_Command;
use WP_CLI\Utils;
Expand Down Expand Up @@ -64,6 +63,11 @@ class MakePotCommand extends WP_CLI_Command {
*/
protected $skip_php = false;

/**
* @var bool
*/
protected $skip_json = false;

/**
* @var bool
*/
Expand Down Expand Up @@ -198,6 +202,9 @@ class MakePotCommand extends WP_CLI_Command {
* [--skip-php]
* : Skips PHP string extraction.
*
* [--skip-json]
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
* : Skips string extraction from block.json files.
*
* [--skip-audit]
* : Skips string audit where it tries to find possible mistakes in translatable strings. Useful when running in an
* automated environment.
Expand Down Expand Up @@ -276,6 +283,7 @@ public function handle_arguments( $args, $assoc_args ) {
$this->slug = Utils\get_flag_value( $assoc_args, 'slug', Utils\basename( $this->source ) );
$this->skip_js = Utils\get_flag_value( $assoc_args, 'skip-js', $this->skip_js );
$this->skip_php = Utils\get_flag_value( $assoc_args, 'skip-php', $this->skip_php );
$this->skip_json = Utils\get_flag_value( $assoc_args, 'skip-json', $this->skip_json );
$this->skip_audit = Utils\get_flag_value( $assoc_args, 'skip-audit', $this->skip_audit );
$this->headers = Utils\get_flag_value( $assoc_args, 'headers', $this->headers );
$this->file_comment = Utils\get_flag_value( $assoc_args, 'file-comment' );
Expand Down Expand Up @@ -601,6 +609,20 @@ protected function extract_strings() {
]
);
}

if ( ! $this->skip_json ) {
BlockExtractor::fromDirectory(
$this->source,
$translations,
[
// Only look for block.json files, nothing else.
'restrictFileNames' => [ 'block.json' ],
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
'include' => $this->include,
'exclude' => $this->exclude,
'extensions' => [ 'json' ],
]
);
}
} catch ( \Exception $e ) {
WP_CLI::error( $e->getMessage() );
}
Expand Down