From 7f7bc16bccc8bb0344cc3df7ca4b2504798396f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 5 Apr 2022 10:54:11 +0200 Subject: [PATCH 01/10] Add support for translating the Title from patterns in /patterns --- src/IterableCodeExtractor.php | 12 ++++++++++++ src/MakePotCommand.php | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index 5cdb2369..110439a2 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -24,6 +24,7 @@ trait IterableCodeExtractor { * Optional. An array of options passed down to static::fromString() * * @type bool $wpExtractTemplates Extract 'Template Name' headers in theme files. Default 'false'. + * @type bool $wpExtractPatterns Extract 'Title' headers in pattern files. Default 'false'. * @type array $restrictFileNames Skip all files which are not included in this array. * } * @return null @@ -65,6 +66,17 @@ public static function fromFile( $file_or_files, Translations $translations, arr } } + if ( ! empty( $options['wpExtractPatterns'] ) ) { + $headers = MakePotCommand::get_file_data_from_string( $string, [ 'Title' => 'Title' ] ); + + if ( ! empty( $headers['Title'] ) ) { + $translation = new Translation( '', $headers['Title'] ); + $translation->addExtractedComment( 'Title of the pattern' ); + + $translations[] = $translation; + } + } + static::fromString( $string, $translations, $options ); } } diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index e0dac0ad..b048c000 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -618,6 +618,17 @@ protected function extract_strings() { PhpCodeExtractor::fromDirectory( $this->source, $translations, $options ); } + if ( ! $this->skip_php ) { + $options = [ + // Extract 'Title' headers from pattern files. + 'wpExtractPatterns' => isset( $this->main_file_data['Theme Name'] ), + 'include' => array_merge( $this->include, array( 'patterns' ) ), + 'exclude' => $this->exclude, + 'extensions' => [ 'php' ], + ]; + PhpCodeExtractor::fromDirectory( $this->source, $translations, $options ); + } + if ( ! $this->skip_blade ) { $options = [ 'include' => $this->include, From cc4b02b4e3de8dde18e9f5f91d3c1e716c99fab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 5 Apr 2022 13:14:40 +0200 Subject: [PATCH 02/10] Extract description as well --- src/IterableCodeExtractor.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index 110439a2..b293d472 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -67,7 +67,7 @@ public static function fromFile( $file_or_files, Translations $translations, arr } if ( ! empty( $options['wpExtractPatterns'] ) ) { - $headers = MakePotCommand::get_file_data_from_string( $string, [ 'Title' => 'Title' ] ); + $headers = MakePotCommand::get_file_data_from_string( $string, [ 'Title' => 'Title', 'Description' => 'Description' ] ); if ( ! empty( $headers['Title'] ) ) { $translation = new Translation( '', $headers['Title'] ); @@ -75,6 +75,13 @@ public static function fromFile( $file_or_files, Translations $translations, arr $translations[] = $translation; } + + if ( ! empty( $headers['Description'] ) ) { + $translation = new Translation( '', $headers['Description'] ); + $translation->addExtractedComment( 'Description of the pattern' ); + + $translations[] = $translation; + } } static::fromString( $string, $translations, $options ); From 4cff589d7d669b8a6e9860722cb5d222e9ad1bfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 5 Apr 2022 13:19:51 +0200 Subject: [PATCH 03/10] Fix lint issues --- src/IterableCodeExtractor.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index b293d472..6767d458 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -67,7 +67,13 @@ public static function fromFile( $file_or_files, Translations $translations, arr } if ( ! empty( $options['wpExtractPatterns'] ) ) { - $headers = MakePotCommand::get_file_data_from_string( $string, [ 'Title' => 'Title', 'Description' => 'Description' ] ); + $headers = MakePotCommand::get_file_data_from_string( + $string, + [ + 'Title' => 'Title', + 'Description' => 'Description', + ] + ); if ( ! empty( $headers['Title'] ) ) { $translation = new Translation( '', $headers['Title'] ); From 2b14dfd9c310cba6708eafb6aa2c2796289ec234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 5 Apr 2022 17:31:23 +0200 Subject: [PATCH 04/10] Add context & file name as comment --- src/IterableCodeExtractor.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index 6767d458..570d6040 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -76,15 +76,15 @@ public static function fromFile( $file_or_files, Translations $translations, arr ); if ( ! empty( $headers['Title'] ) ) { - $translation = new Translation( '', $headers['Title'] ); - $translation->addExtractedComment( 'Title of the pattern' ); + $translation = new Translation( 'Title of the pattern', $headers['Title'] ); + $translation->addReference( $options[ 'file' ] ); $translations[] = $translation; } if ( ! empty( $headers['Description'] ) ) { - $translation = new Translation( '', $headers['Description'] ); - $translation->addExtractedComment( 'Description of the pattern' ); + $translation = new Translation( 'Description of the pattern', $headers['Description'] ); + $translation->addReference( $options[ 'file' ] ); $translations[] = $translation; } From ffa87027ac9ff724d74e454d3d193bfcd7b8903e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 5 Apr 2022 18:09:41 +0200 Subject: [PATCH 05/10] Update context for title & description --- src/IterableCodeExtractor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index 570d6040..1cd0e903 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -76,14 +76,14 @@ public static function fromFile( $file_or_files, Translations $translations, arr ); if ( ! empty( $headers['Title'] ) ) { - $translation = new Translation( 'Title of the pattern', $headers['Title'] ); + $translation = new Translation( 'Pattern title', $headers['Title'] ); $translation->addReference( $options[ 'file' ] ); $translations[] = $translation; } if ( ! empty( $headers['Description'] ) ) { - $translation = new Translation( 'Description of the pattern', $headers['Description'] ); + $translation = new Translation( 'Pattern description', $headers['Description'] ); $translation->addReference( $options[ 'file' ] ); $translations[] = $translation; From 3298a2a2581d77a5a53c3bad933395aa84ac2ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 5 Apr 2022 18:15:51 +0200 Subject: [PATCH 06/10] Fix lint issue --- src/IterableCodeExtractor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index 1cd0e903..717765a8 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -77,14 +77,14 @@ public static function fromFile( $file_or_files, Translations $translations, arr if ( ! empty( $headers['Title'] ) ) { $translation = new Translation( 'Pattern title', $headers['Title'] ); - $translation->addReference( $options[ 'file' ] ); + $translation->addReference( $options['file'] ); $translations[] = $translation; } if ( ! empty( $headers['Description'] ) ) { $translation = new Translation( 'Pattern description', $headers['Description'] ); - $translation->addReference( $options[ 'file' ] ); + $translation->addReference( $options['file'] ); $translations[] = $translation; } From 29c3b190fe4a677fb4689ccd2b73243f98660378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 5 Apr 2022 18:17:54 +0200 Subject: [PATCH 07/10] Update comment --- src/IterableCodeExtractor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index 717765a8..a0dbcb8d 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -24,7 +24,7 @@ trait IterableCodeExtractor { * Optional. An array of options passed down to static::fromString() * * @type bool $wpExtractTemplates Extract 'Template Name' headers in theme files. Default 'false'. - * @type bool $wpExtractPatterns Extract 'Title' headers in pattern files. Default 'false'. + * @type bool $wpExtractPatterns Extract 'Title' and 'Description' headers in pattern files. Default 'false'. * @type array $restrictFileNames Skip all files which are not included in this array. * } * @return null From fe3b26df7376d34416909d746c352946b2d36f0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Wed, 6 Apr 2022 16:50:52 +0200 Subject: [PATCH 08/10] Add behat test --- features/makepot.feature | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/features/makepot.feature b/features/makepot.feature index db0f9303..eba14b6f 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -3658,3 +3658,41 @@ Feature: Generate a POT file of a WordPress project msgctxt "Color name" msgid "Black" """ + + @patterns + Scenario: Extract strings from the patterns directory + Given an empty foo-theme/patterns directory + And a foo-theme/patterns/my-pattern.php file: + """ + Date: Wed, 6 Apr 2022 16:52:11 +0200 Subject: [PATCH 09/10] Remove tag used in testing --- features/makepot.feature | 1 - 1 file changed, 1 deletion(-) diff --git a/features/makepot.feature b/features/makepot.feature index eba14b6f..1c1913fc 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -3659,7 +3659,6 @@ Feature: Generate a POT file of a WordPress project msgid "Black" """ - @patterns Scenario: Extract strings from the patterns directory Given an empty foo-theme/patterns directory And a foo-theme/patterns/my-pattern.php file: From 4a4549bda203a68dc9abb02f0cf54f93c8466e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Wed, 6 Apr 2022 17:25:17 +0200 Subject: [PATCH 10/10] Update src/MakePotCommand.php Co-authored-by: Pascal Birchler --- src/MakePotCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index b048c000..bfe37c61 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -620,7 +620,7 @@ protected function extract_strings() { if ( ! $this->skip_php ) { $options = [ - // Extract 'Title' headers from pattern files. + // Extract 'Title' and 'Description' headers from pattern files. 'wpExtractPatterns' => isset( $this->main_file_data['Theme Name'] ), 'include' => array_merge( $this->include, array( 'patterns' ) ), 'exclude' => $this->exclude,