From cb9ca4d63d4195afbf8b5989a6541053ebcf254e Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 17 Apr 2020 10:09:59 +0200 Subject: [PATCH 01/16] PoC for block.json string extraction --- features/makepot.feature | 78 +++++++++++++++++++++++++++++++++++ src/BlockExtractor.php | 61 +++++++++++++++++++++++++++ src/IterableCodeExtractor.php | 10 ++++- src/MakePotCommand.php | 24 ++++++++++- 4 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 src/BlockExtractor.php diff --git a/features/makepot.feature b/features/makepot.feature index ff7a0b6c..36392062 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -2458,3 +2458,81 @@ 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: + """ + [ + 'title', + 'description', + 'keywords', + 'styles' + ], + ]; + + /** + * @inheritdoc + */ + public static function fromString( $string, Translations $translations, array $options = [] ) { + WP_CLI::debug( "Parsing file {$options['file']}" ); + + $file_data = json_decode( $string, true ); + + if ( null === $file_data ) { + WP_CLI::debug( + sprintf( + 'Could not parse file %1$s: %2$s', + $options['file'], + json_last_error_msg() + ) + ); + + return; + } + + $domain = isset( $file_data['textDomain'] ) ? $file_data['textDomain'] : null; + + if ( $domain !== $translations->getDomain() ) { + return; + } + + foreach ( $file_data as $key => $original ) { + if ( ! array_key_exists( $key, $options['translatableProperties'] ) ) { + continue; + } + + foreach ( (array) $original as $msg ) { + $translations->insert( + sprintf('block %s', $key ), + $msg + ); + } + } + } +} diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index 716e1677..bb352e5a 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -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 ) { + 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 ) ), '/' ); diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index dff80a22..e58bb10d 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -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; @@ -64,6 +63,11 @@ class MakePotCommand extends WP_CLI_Command { */ protected $skip_php = false; + /** + * @var bool + */ + protected $skip_json = false; + /** * @var bool */ @@ -198,6 +202,9 @@ class MakePotCommand extends WP_CLI_Command { * [--skip-php] * : Skips PHP string extraction. * + * [--skip-json] + * : 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. @@ -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' ); @@ -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' ], + 'include' => $this->include, + 'exclude' => $this->exclude, + 'extensions' => [ 'json' ], + ] + ); + } } catch ( \Exception $e ) { WP_CLI::error( $e->getMessage() ); } From 657008f3c777a4db2032e353695a402daf05aab8 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 17 Apr 2020 10:15:55 +0200 Subject: [PATCH 02/16] Allow missing textDoamin field --- features/makepot.feature | 144 +++++++++++++++++++++++++++++++++++++++ src/BlockExtractor.php | 3 +- 2 files changed, 146 insertions(+), 1 deletion(-) diff --git a/features/makepot.feature b/features/makepot.feature index 36392062..0568d925 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -2460,6 +2460,84 @@ Feature: Generate a POT file of a WordPress project 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: + """ + getDomain() ) { + // Allow missing domain, but skip if they don't match. + if ( $domain !== null && $domain !== $translations->getDomain()) { return; } From 2a103976956051f22debb56d3bcfb5cb8826e298 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 17 Apr 2020 10:19:35 +0200 Subject: [PATCH 03/16] some lint fixes --- src/BlockExtractor.php | 11 ++++------- src/MakePotCommand.php | 8 ++++---- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/BlockExtractor.php b/src/BlockExtractor.php index e743be74..b18bf554 100644 --- a/src/BlockExtractor.php +++ b/src/BlockExtractor.php @@ -15,7 +15,7 @@ final class BlockExtractor extends Extractor implements ExtractorInterface { 'title', 'description', 'keywords', - 'styles' + 'styles', ], ]; @@ -30,9 +30,9 @@ public static function fromString( $string, Translations $translations, array $o if ( null === $file_data ) { WP_CLI::debug( sprintf( - 'Could not parse file %1$s: %2$s', + 'Could not parse file %1$s: error code %2$s', $options['file'], - json_last_error_msg() + json_last_error() ) ); @@ -52,10 +52,7 @@ public static function fromString( $string, Translations $translations, array $o } foreach ( (array) $original as $msg ) { - $translations->insert( - sprintf('block %s', $key ), - $msg - ); + $translations->insert( sprintf( 'block %s', $key ), $msg ); } } } diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index e58bb10d..c04257a8 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -616,10 +616,10 @@ protected function extract_strings() { $translations, [ // Only look for block.json files, nothing else. - 'restrictFileNames' => [ 'block.json' ], - 'include' => $this->include, - 'exclude' => $this->exclude, - 'extensions' => [ 'json' ], + 'restrictFileNames' => [ 'block.json' ], + 'include' => $this->include, + 'exclude' => $this->exclude, + 'extensions' => [ 'json' ], ] ); } From b39a5d18fcb1b195afacab138ffbdb5539556544 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 17 Apr 2020 10:22:05 +0200 Subject: [PATCH 04/16] some more lint fixes --- src/BlockExtractor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BlockExtractor.php b/src/BlockExtractor.php index b18bf554..1b11beec 100644 --- a/src/BlockExtractor.php +++ b/src/BlockExtractor.php @@ -42,7 +42,7 @@ public static function fromString( $string, Translations $translations, array $o $domain = isset( $file_data['textDomain'] ) ? $file_data['textDomain'] : null; // Allow missing domain, but skip if they don't match. - if ( $domain !== null && $domain !== $translations->getDomain()) { + if ( null !== $domain && $domain !== $translations->getDomain() ) { return; } From 8cd270e5dd905f98134ed29c9284ca40c0759bce Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 17 Apr 2020 10:30:02 +0200 Subject: [PATCH 05/16] Add file reference to extracted strings --- src/BlockExtractor.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/BlockExtractor.php b/src/BlockExtractor.php index 1b11beec..85e148bf 100644 --- a/src/BlockExtractor.php +++ b/src/BlockExtractor.php @@ -23,7 +23,8 @@ final class BlockExtractor extends Extractor implements ExtractorInterface { * @inheritdoc */ public static function fromString( $string, Translations $translations, array $options = [] ) { - WP_CLI::debug( "Parsing file {$options['file']}" ); + $file = $options['file']; + WP_CLI::debug( "Parsing file {$file}" ); $file_data = json_decode( $string, true ); @@ -31,7 +32,7 @@ public static function fromString( $string, Translations $translations, array $o WP_CLI::debug( sprintf( 'Could not parse file %1$s: error code %2$s', - $options['file'], + $file, json_last_error() ) ); @@ -52,7 +53,8 @@ public static function fromString( $string, Translations $translations, array $o } foreach ( (array) $original as $msg ) { - $translations->insert( sprintf( 'block %s', $key ), $msg ); + $translation = $translations->insert( sprintf( 'block %s', $key ), $msg ); + $translation->addReference( $file ); } } } From 8d32dd4b417d8e04537e6087c91faa1eeadc788d Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 17 Apr 2020 16:09:10 +0200 Subject: [PATCH 06/16] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Grzegorz (Greg) Ziółkowski --- src/BlockExtractor.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/BlockExtractor.php b/src/BlockExtractor.php index 85e148bf..241cf661 100644 --- a/src/BlockExtractor.php +++ b/src/BlockExtractor.php @@ -15,7 +15,7 @@ final class BlockExtractor extends Extractor implements ExtractorInterface { 'title', 'description', 'keywords', - 'styles', + 'styleVariations', ], ]; @@ -53,7 +53,11 @@ public static function fromString( $string, Translations $translations, array $o } foreach ( (array) $original as $msg ) { - $translation = $translations->insert( sprintf( 'block %s', $key ), $msg ); + if ( is_object( $msg ) ) { + $translation = $translations->insert( sprintf( 'block %s %s', $key, $msg->name ), $msg->label ); + } else { + $translation = $translations->insert( sprintf( 'block %s', $key ), $msg ); + } $translation->addReference( $file ); } } From b3c9bd711778daef514bd395746ba7f1349e80c1 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 20 Apr 2020 10:47:12 +0200 Subject: [PATCH 07/16] Rename skip-json to skip-block-json --- src/MakePotCommand.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index c04257a8..84aa284e 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -66,7 +66,7 @@ class MakePotCommand extends WP_CLI_Command { /** * @var bool */ - protected $skip_json = false; + protected $skip_block_json = false; /** * @var bool @@ -202,7 +202,7 @@ class MakePotCommand extends WP_CLI_Command { * [--skip-php] * : Skips PHP string extraction. * - * [--skip-json] + * [--skip-block-json] * : Skips string extraction from block.json files. * * [--skip-audit] @@ -279,15 +279,15 @@ public function handle_arguments( $args, $assoc_args ) { $array_arguments = array( 'headers' ); $assoc_args = Utils\parse_shell_arrays( $assoc_args, $array_arguments ); - $this->source = realpath( $args[0] ); - $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' ); - $this->package_name = Utils\get_flag_value( $assoc_args, 'package-name' ); + $this->source = realpath( $args[0] ); + $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_block_json = Utils\get_flag_value( $assoc_args, 'skip-block-json', $this->skip_block_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' ); + $this->package_name = Utils\get_flag_value( $assoc_args, 'package-name' ); $ignore_domain = Utils\get_flag_value( $assoc_args, 'ignore-domain', false ); @@ -610,7 +610,7 @@ protected function extract_strings() { ); } - if ( ! $this->skip_json ) { + if ( ! $this->skip_block_json ) { BlockExtractor::fromDirectory( $this->source, $translations, From 5552e997db154c8d952c65b95b122eb2c6026634 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 20 Apr 2020 10:47:19 +0200 Subject: [PATCH 08/16] More tests --- features/makepot.feature | 73 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/features/makepot.feature b/features/makepot.feature index 0568d925..cf27687d 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -2536,6 +2536,22 @@ Feature: Generate a POT file of a WordPress project """ msgid "message" """ + And the foo-plugin/foo-plugin.pot file should contain: + """ + msgctxt "block styleVariatons default" + """ + And the foo-plugin/foo-plugin.pot file should contain: + """ + msgid "Default" + """ + And the foo-plugin/foo-plugin.pot file should contain: + """ + msgctxt "block styleVariatons other" + """ + 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 @@ -2680,3 +2696,60 @@ Feature: Generate a POT file of a WordPress project """ msgid "message" """ + + Scenario: Skips block.json file altogether + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin.php file: + """ + Date: Mon, 11 May 2020 11:42:44 +0200 Subject: [PATCH 09/16] Add missing options merge --- src/BlockExtractor.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/BlockExtractor.php b/src/BlockExtractor.php index 241cf661..a96dd17b 100644 --- a/src/BlockExtractor.php +++ b/src/BlockExtractor.php @@ -23,6 +23,8 @@ final class BlockExtractor extends Extractor implements ExtractorInterface { * @inheritdoc */ public static function fromString( $string, Translations $translations, array $options = [] ) { + $options += static::$options; + $file = $options['file']; WP_CLI::debug( "Parsing file {$file}" ); From 589487f917f5ac4747451f014771a5830517420a Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 11 May 2020 11:58:04 +0200 Subject: [PATCH 10/16] Refactor code to work without options property Makes it more readable, and also improves message context --- features/makepot.feature | 8 +++---- src/BlockExtractor.php | 47 ++++++++++++++++++++++------------------ 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/features/makepot.feature b/features/makepot.feature index cf27687d..a6c52095 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -2526,7 +2526,7 @@ Feature: Generate a POT file of a WordPress project """ And the foo-plugin/foo-plugin.pot file should contain: """ - msgctxt "block keywords" + msgctxt "block keyword" """ And the foo-plugin/foo-plugin.pot file should contain: """ @@ -2538,7 +2538,7 @@ Feature: Generate a POT file of a WordPress project """ And the foo-plugin/foo-plugin.pot file should contain: """ - msgctxt "block styleVariatons default" + msgctxt "block style variation" """ And the foo-plugin/foo-plugin.pot file should contain: """ @@ -2546,7 +2546,7 @@ Feature: Generate a POT file of a WordPress project """ And the foo-plugin/foo-plugin.pot file should contain: """ - msgctxt "block styleVariatons other" + msgctxt "block style variation" """ And the foo-plugin/foo-plugin.pot file should contain: """ @@ -2686,7 +2686,7 @@ Feature: Generate a POT file of a WordPress project """ And the foo-plugin/foo-plugin.pot file should contain: """ - msgctxt "block keywords" + msgctxt "block keyword" """ And the foo-plugin/foo-plugin.pot file should contain: """ diff --git a/src/BlockExtractor.php b/src/BlockExtractor.php index a96dd17b..8e870278 100644 --- a/src/BlockExtractor.php +++ b/src/BlockExtractor.php @@ -10,21 +10,10 @@ final class BlockExtractor extends Extractor implements ExtractorInterface { use IterableCodeExtractor; - public static $options = [ - 'translatableProperties' => [ - 'title', - 'description', - 'keywords', - 'styleVariations', - ], - ]; - /** * @inheritdoc */ public static function fromString( $string, Translations $translations, array $options = [] ) { - $options += static::$options; - $file = $options['file']; WP_CLI::debug( "Parsing file {$file}" ); @@ -42,6 +31,7 @@ public static function fromString( $string, Translations $translations, array $o return; } + $domain = isset( $file_data['textDomain'] ) ? $file_data['textDomain'] : null; // Allow missing domain, but skip if they don't match. @@ -50,17 +40,32 @@ public static function fromString( $string, Translations $translations, array $o } foreach ( $file_data as $key => $original ) { - if ( ! array_key_exists( $key, $options['translatableProperties'] ) ) { - continue; - } + 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': + if ( ! is_array( $original ) ) { + continue 2; + } - foreach ( (array) $original as $msg ) { - if ( is_object( $msg ) ) { - $translation = $translations->insert( sprintf( 'block %s %s', $key, $msg->name ), $msg->label ); - } else { - $translation = $translations->insert( sprintf( 'block %s', $key ), $msg ); - } - $translation->addReference( $file ); + foreach( $original as $msg ) { + $translation = $translations->insert( 'block style variation', $msg['label'] ); + $translation->addReference( $file ); + } } } } From 52fc8520a411850e16ac282258be4525a536e33e Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 11 May 2020 12:03:18 +0200 Subject: [PATCH 11/16] lint fixes --- src/BlockExtractor.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/BlockExtractor.php b/src/BlockExtractor.php index 8e870278..c4a061b6 100644 --- a/src/BlockExtractor.php +++ b/src/BlockExtractor.php @@ -31,7 +31,6 @@ public static function fromString( $string, Translations $translations, array $o return; } - $domain = isset( $file_data['textDomain'] ) ? $file_data['textDomain'] : null; // Allow missing domain, but skip if they don't match. @@ -40,7 +39,7 @@ public static function fromString( $string, Translations $translations, array $o } foreach ( $file_data as $key => $original ) { - switch( $key ) { + switch ( $key ) { case 'title': case 'description': $translation = $translations->insert( sprintf( 'block %s', $key ), $original ); @@ -51,7 +50,7 @@ public static function fromString( $string, Translations $translations, array $o continue 2; } - foreach( $original as $msg ) { + foreach ( $original as $msg ) { $translation = $translations->insert( 'block keyword', $msg ); $translation->addReference( $file ); } @@ -62,7 +61,7 @@ public static function fromString( $string, Translations $translations, array $o continue 2; } - foreach( $original as $msg ) { + foreach ( $original as $msg ) { $translation = $translations->insert( 'block style variation', $msg['label'] ); $translation->addReference( $file ); } From 119fcf053e3e308532641ba43d82a420b4cae162 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 14 May 2020 11:36:49 +0200 Subject: [PATCH 12/16] Rename variable for clarity --- src/IterableCodeExtractor.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index bb352e5a..60e558e9 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -18,8 +18,8 @@ trait IterableCodeExtractor { /** * Extract the translations from a file. * - * @param array|string $file A path of a file or files - * @param Translations $translations The translations instance to append the new translations. + * @param array|string $file_or_files A path of a file or files + * @param Translations $translations The translations instance to append the new translations. * @param array $options { * Optional. An array of options passed down to static::fromString() * @@ -28,25 +28,25 @@ trait IterableCodeExtractor { * } * @return null */ - public static function fromFile( $file, Translations $translations, array $options = [] ) { - foreach ( static::getFiles( $file ) as $f ) { + public static function fromFile( $file_or_files, Translations $translations, array $options = [] ) { + foreach ( static::getFiles( $file_or_files ) as $file ) { if ( ! empty( $options['restrictFileNames'] ) ) { - $basename = Utils\basename( $f ); + $basename = Utils\basename( $file ); 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 ) ), '/' ); + $options['file'] = ltrim( str_replace( static::$dir, '', Utils\normalize_path( $file ) ), '/' ); - $string = file_get_contents( $f ); + $string = file_get_contents( $file ); if ( ! $string ) { WP_CLI::debug( sprintf( 'Could not load file %1s', - $f + $file ) ); From 0b2e92bf8d1dbdc43286a8b6e81fc826b8355875 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 14 May 2020 11:44:05 +0200 Subject: [PATCH 13/16] Use same debug group consistently for all extractors --- src/BlockExtractor.php | 5 +++-- src/IterableCodeExtractor.php | 3 ++- src/JsCodeExtractor.php | 8 +++++--- src/MapCodeExtractor.php | 5 +++-- src/PhpCodeExtractor.php | 5 +++-- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/BlockExtractor.php b/src/BlockExtractor.php index c4a061b6..4f3dd47e 100644 --- a/src/BlockExtractor.php +++ b/src/BlockExtractor.php @@ -15,7 +15,7 @@ final class BlockExtractor extends Extractor implements ExtractorInterface { */ public static function fromString( $string, Translations $translations, array $options = [] ) { $file = $options['file']; - WP_CLI::debug( "Parsing file {$file}" ); + WP_CLI::debug( "Parsing file {$file}", 'make-pot' ); $file_data = json_decode( $string, true ); @@ -25,7 +25,8 @@ public static function fromString( $string, Translations $translations, array $o 'Could not parse file %1$s: error code %2$s', $file, json_last_error() - ) + ), + 'make-pot' ); return; diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index 60e558e9..a0ff25fd 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -47,7 +47,8 @@ public static function fromFile( $file_or_files, Translations $translations, arr sprintf( 'Could not load file %1s', $file - ) + ), + 'make-pot' ); continue; diff --git a/src/JsCodeExtractor.php b/src/JsCodeExtractor.php index 5547e1ad..f751f3cf 100644 --- a/src/JsCodeExtractor.php +++ b/src/JsCodeExtractor.php @@ -28,7 +28,7 @@ final class JsCodeExtractor extends JsCode { * @inheritdoc */ public static function fromString( $string, Translations $translations, array $options = [] ) { - WP_CLI::debug( "Parsing file {$options['file']}" ); + WP_CLI::debug( "Parsing file {$options['file']}", 'make-pot' ); try { static::fromStringMultiple( $string, [ $translations ], $options ); @@ -40,7 +40,8 @@ public static function fromString( $string, Translations $translations, array $o $exception->getMessage(), $exception->getPosition()->getLine(), $exception->getPosition()->getColumn() - ) + ), + 'make-pot' ); } catch ( Exception $exception ) { WP_CLI::debug( @@ -48,7 +49,8 @@ public static function fromString( $string, Translations $translations, array $o 'Could not parse file %1$s: %2$s', $options['file'], $exception->getMessage() - ) + ), + 'make-pot' ); } } diff --git a/src/MapCodeExtractor.php b/src/MapCodeExtractor.php index 5a976da4..1d2c0a06 100644 --- a/src/MapCodeExtractor.php +++ b/src/MapCodeExtractor.php @@ -41,7 +41,7 @@ public static function fromString( $string, Translations $translations, array $o $string = implode( "\n", $map_object->sourcesContent ); - WP_CLI::debug( "Parsing file {$options['file']}" ); + WP_CLI::debug( "Parsing file {$options['file']}", 'make-pot' ); $functions = new JsFunctionsScanner( $string ); @@ -55,7 +55,8 @@ public static function fromString( $string, Translations $translations, array $o $e->getMessage(), $e->getPosition()->getLine(), $e->getPosition()->getColumn() - ) + ), + 'make-pot' ); } } diff --git a/src/PhpCodeExtractor.php b/src/PhpCodeExtractor.php index d423345d..478c5271 100644 --- a/src/PhpCodeExtractor.php +++ b/src/PhpCodeExtractor.php @@ -46,7 +46,7 @@ final class PhpCodeExtractor extends PhpCode { * {@inheritdoc} */ public static function fromString( $string, Translations $translations, array $options = [] ) { - WP_CLI::debug( "Parsing file {$options['file']}" ); + WP_CLI::debug( "Parsing file {$options['file']}", 'make-pot' ); try { static::fromStringMultiple( $string, [ $translations ], $options ); @@ -56,7 +56,8 @@ public static function fromString( $string, Translations $translations, array $o 'Could not parse file %1$s: %2$s', $options['file'], $exception->getMessage() - ) + ), + 'make-pot' ); } } From 2507e8db3459eb6fb3cd6e88b6ff7b8c3386b8b1 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 14 May 2020 11:46:49 +0200 Subject: [PATCH 14/16] Support styleVariations / styles alias --- features/makepot.feature | 95 +++++++++++++++++++++++++++++++++++++++- src/BlockExtractor.php | 1 + 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/features/makepot.feature b/features/makepot.feature index a6c52095..a93bc0f0 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -2553,6 +2553,100 @@ Feature: Generate a POT file of a WordPress project msgid "Other" """ + Scenario: Extract strings from block.json files with styleVariations alias + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin.php file: + """ + Date: Tue, 2 Jun 2020 14:20:47 +0200 Subject: [PATCH 15/16] Reflect changes to WordPress core --- features/makepot.feature | 108 +++------------------------------------ src/BlockExtractor.php | 5 +- 2 files changed, 9 insertions(+), 104 deletions(-) diff --git a/features/makepot.feature b/features/makepot.feature index a93bc0f0..0f1b17bc 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -2478,101 +2478,7 @@ Feature: Generate a POT file of a WordPress project "icon": "star", "description": "Shows warning, error or success notices ...", "keywords": [ "alert", "message" ], - "textDomain": "foo-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 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: Extract strings from block.json files with styleVariations alias - Given an empty foo-plugin directory - And a foo-plugin/foo-plugin.php file: - """ - getDomain() ) { @@ -58,13 +58,12 @@ public static function fromString( $string, Translations $translations, array $o break; case 'styles': - case 'styleVariations': if ( ! is_array( $original ) ) { continue 2; } foreach ( $original as $msg ) { - $translation = $translations->insert( 'block style variation', $msg['label'] ); + $translation = $translations->insert( 'block style style', $msg['label'] ); $translation->addReference( $file ); } } From 749e16fc3c967c45383dc2af1b74952130e21015 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 3 Jun 2020 11:41:27 +0200 Subject: [PATCH 16/16] Update msg context --- features/makepot.feature | 4 ++-- src/BlockExtractor.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/features/makepot.feature b/features/makepot.feature index 0f1b17bc..92390399 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -2538,7 +2538,7 @@ Feature: Generate a POT file of a WordPress project """ And the foo-plugin/foo-plugin.pot file should contain: """ - msgctxt "block style" + msgctxt "block style label" """ And the foo-plugin/foo-plugin.pot file should contain: """ @@ -2546,7 +2546,7 @@ Feature: Generate a POT file of a WordPress project """ And the foo-plugin/foo-plugin.pot file should contain: """ - msgctxt "block style" + msgctxt "block style label" """ And the foo-plugin/foo-plugin.pot file should contain: """ diff --git a/src/BlockExtractor.php b/src/BlockExtractor.php index 8a06f224..92b58d6b 100644 --- a/src/BlockExtractor.php +++ b/src/BlockExtractor.php @@ -63,7 +63,7 @@ public static function fromString( $string, Translations $translations, array $o } foreach ( $original as $msg ) { - $translation = $translations->insert( 'block style style', $msg['label'] ); + $translation = $translations->insert( 'block style label', $msg['label'] ); $translation->addReference( $file ); } }