Skip to content

Commit

Permalink
Merge pull request #312 from oandregal/add/translate-php-patterns-met…
Browse files Browse the repository at this point in the history
…adata
  • Loading branch information
swissspidy authored Apr 6, 2022
2 parents 6c0fcdb + 4a4549b commit bcb1a81
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
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'] ),
'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,
Expand Down

0 comments on commit bcb1a81

Please sign in to comment.