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

Update and combine theme.json and block.json extractors #319

Merged
merged 6 commits into from
Jul 4, 2022
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
17 changes: 17 additions & 0 deletions assets/block-i18n.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"title": "block title",
"description": "block description",
"keywords": [ "block keyword" ],
"styles": [
{
"label": "block style label"
}
],
"variations": [
{
"title": "block variation title",
"description": "block variation description",
"keywords": [ "block variation keyword" ]
}
]
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"wp-cli/wp-cli-tests": "^3.1"
},
"suggest": {
"ext-json": "Used for reading and generating JSON translation files",
"ext-mbstring": "Used for calculating include/exclude matches in code extraction"
},
"config": {
Expand Down
43 changes: 41 additions & 2 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3128,7 +3128,14 @@ Feature: Generate a POT file of a WordPress project
"editorScript": "build/editor.js",
"script": "build/main.js",
"editorStyle": "build/editor.css",
"style": "build/style.css"
"style": "build/style.css",
"variations": [
{
"title": "Notice Variation A",
"description": "Just a variation",
"keywords": [ "msgvariation", "anotherkeyword" ]
}
]
}
"""

Expand Down Expand Up @@ -3187,6 +3194,38 @@ Feature: Generate a POT file of a WordPress project
"""
msgid "Other"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "block variation title"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "Notice Variation A"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "block variation description"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "Just a variation"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "block variation keyword"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "msgvariation"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "block variation keyword"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "anotherkeyword"
"""

Scenario: Ignores block.json files with other text domain
Given an empty foo-plugin directory
Expand Down Expand Up @@ -3646,7 +3685,7 @@ Feature: Generate a POT file of a WordPress project
}
}
"""

When I try `wp i18n make-pot foo-theme`
Then STDOUT should be:
"""
Expand Down
52 changes: 6 additions & 46 deletions src/BlockExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@

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;

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

$file_data = json_decode( $string, true );
$json = json_decode( $string, true );

if ( null === $file_data ) {
if ( null === $json ) {
WP_CLI::debug(
sprintf(
'Could not parse file %1$s: error code %2$s',
Expand All @@ -32,49 +28,13 @@ public static function fromString( $string, Translations $translations, array $o
return;
}

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

// Always allow missing domain or when --ignore-domain is used, but skip if domains don't match.
if ( null !== $translations->getDomain() && null !== $domain && $domain !== $translations->getDomain() ) {
return;
}

$add_reference = ! empty( $options['addReferences'] );

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

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

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

foreach ( $original as $msg ) {
$translation = $translations->insert( 'block style label', $msg['label'] );
if ( $add_reference ) {
$translation->addReference( $file );
}
}
}
}
parent::fromString( $string, $translations, $options );
}
}
12 changes: 6 additions & 6 deletions src/IterableCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

trait IterableCodeExtractor {

private static $dir = '';
protected static $dir = '';

/**
* Extract the translations from a file.
Expand Down Expand Up @@ -106,7 +106,7 @@ public static function fromFile( $file_or_files, Translations $translations, arr
* @type array $exclude A list of path to exclude. Default [].
* @type array $extensions A list of extensions to process. Default [].
* }
* @return null
* @return void
*/
public static function fromDirectory( $dir, Translations $translations, array $options = [] ) {
$dir = Utils\normalize_path( $dir );
Expand Down Expand Up @@ -193,7 +193,7 @@ protected static function containsMatchingChildren( SplFileInfo $dir, array $mat

/** @var string $root_relative_path */
$root_relative_path = str_replace( static::$dir, '', $dir->getPathname() );
$root_relative_path = self::trim_leading_slash( $root_relative_path );
$root_relative_path = static::trim_leading_slash( $root_relative_path );

foreach ( $matchers as $path_or_file ) {
// If the matcher contains no wildcards and the path matches the start of the matcher.
Expand Down Expand Up @@ -295,7 +295,7 @@ static function ( $file, $key, $iterator ) use ( $include, $exclude, $extensions
* @param array $extensions List of file extensions to match.
* @return bool Whether the file has a file extension that matches any of the ones in the list.
*/
private static function file_has_file_extension( $file, $extensions ) {
protected static function file_has_file_extension( $file, $extensions ) {
return in_array( $file->getExtension(), $extensions, true ) ||
in_array( static::file_get_extension_multi( $file ), $extensions, true );
}
Expand All @@ -306,7 +306,7 @@ private static function file_has_file_extension( $file, $extensions ) {
* @param SplFileInfo $file File or directory.
* @return string The single- or multi-file extension of the file.
*/
private static function file_get_extension_multi( $file ) {
protected static function file_get_extension_multi( $file ) {
$file_extension_separator = '.';

$filename = $file->getFilename();
Expand All @@ -324,7 +324,7 @@ private static function file_get_extension_multi( $file ) {
* @param string $path Path to trim.
* @return string Trimmed path.
*/
private static function trim_leading_slash( $path ) {
protected static function trim_leading_slash( $path ) {
return ltrim( $path, '/' );
}
}
Loading