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

Extract pattern metadata (title & description) #312

Merged
merged 10 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 37 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3658,3 +3658,40 @@ Feature: Generate a POT file of a WordPress project
msgctxt "Color name"
msgid "Black"
"""

Scenario: Extract strings from the patterns directory
Given an empty foo-theme/patterns directory
And a foo-theme/patterns/my-pattern.php file:
"""
<?php
/**
* Title: My pattern title.
* Description: My pattern description.
*/
"""
And a foo-theme/style.css file:
"""
/*
Theme Name: foo theme
*/
"""

When I try `wp i18n make-pot foo-theme`
Then STDOUT should be:
"""
Theme stylesheet detected.
Success: POT file successfully generated!
"""
And the foo-theme/foo-theme.pot file should exist
And the foo-theme/foo-theme.pot file should contain:
"""
#: patterns/my-pattern.php
msgctxt "Pattern title"
msgid "My pattern title."
msgstr ""

#: patterns/my-pattern.php
msgctxt "Pattern description"
msgid "My pattern description."
msgstr ""
"""
25 changes: 25 additions & 0 deletions src/IterableCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' and 'Description' headers in pattern files. Default 'false'.
* @type array $restrictFileNames Skip all files which are not included in this array.
* }
* @return null
Expand Down Expand Up @@ -65,6 +66,30 @@ 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',
]
);

if ( ! empty( $headers['Title'] ) ) {
$translation = new Translation( 'Pattern title', $headers['Title'] );
$translation->addReference( $options['file'] );

$translations[] = $translation;
}

if ( ! empty( $headers['Description'] ) ) {
$translation = new Translation( 'Pattern description', $headers['Description'] );
$translation->addReference( $options['file'] );

$translations[] = $translation;
}
}

static::fromString( $string, $translations, $options );
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,17 @@ protected function extract_strings() {
PhpCodeExtractor::fromDirectory( $this->source, $translations, $options );
}

if ( ! $this->skip_php ) {
$options = [
// Extract 'Title' and 'Description' headers from pattern files.
'wpExtractPatterns' => isset( $this->main_file_data['Theme Name'] ),
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
'include' => array_merge( $this->include, array( 'patterns' ) ),
Copy link
Contributor

@ocean90 ocean90 May 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@swissspidy Do you know why this hardcodes patterns here? Since these are PHP files they should be included by default. Now you explicitly have to exclude the patterns when you for example are using --include=wp-admin/*. to only include wp-admin strings.

Related: https://meta.trac.wordpress.org/changeset/11809

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the idea was to make this more efficient and only scan these files when needed.

But you're right that this is already covered by the call above. I'll create a PHP to combine these.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'exclude' => $this->exclude,
'extensions' => [ 'php' ],
];
PhpCodeExtractor::fromDirectory( $this->source, $translations, $options );
}

if ( ! $this->skip_blade ) {
$options = [
'include' => $this->include,
Expand Down