diff --git a/features/makepot.feature b/features/makepot.feature index ff7a0b6c..92390399 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -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: + """ + 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 ); + } + } + } + } +} diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index 716e1677..a0ff25fd 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -18,28 +18,37 @@ 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() * - * @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 ) { + 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( $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 + ), + '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/MakePotCommand.php b/src/MakePotCommand.php index dff80a22..84aa284e 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_block_json = false; + /** * @var bool */ @@ -198,6 +202,9 @@ class MakePotCommand extends WP_CLI_Command { * [--skip-php] * : Skips PHP string extraction. * + * [--skip-block-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. @@ -272,14 +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_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 ); @@ -601,6 +609,20 @@ protected function extract_strings() { ] ); } + + if ( ! $this->skip_block_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() ); } 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' ); } }