Skip to content
Closed
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
32 changes: 32 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3092,6 +3092,38 @@ Feature: Generate a POT file of a WordPress project
msgid "Page not found."
"""

@blade
Scenario: Extract strings from custom directives from a Blade-PHP file in a theme
Given an empty foo-theme directory
And a foo-theme/style.css file:
"""
/*
Theme Name: Foo Theme
Theme URI: https://example.com
Description:
Author:
Author URI:
Version: 0.1.0
License: GPL-2.0+
Text Domain: foo-theme
*/
"""
And a foo-theme/stuff.blade.php file:
"""
@t( 'This is a custom directive', 'foo-theme' )
"""

When I try `wp i18n make-pot foo-theme --blade-directives=t result.pot --debug`
Then STDOUT should be:
"""
Theme stylesheet detected.
Success: POT file successfully generated.
"""
And the result.pot file should contain:
"""
msgid "This is a custom directive"
"""

Scenario: Custom package name
Given an empty example-project directory
And a example-project/stuff.php file:
Expand Down
1 change: 1 addition & 0 deletions src/BladeCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ final class BladeCodeExtractor extends BladeGettextExtractor {
'__ngettext' => 'single_plural_number_domain',
'__ngettext_noop' => 'single_plural_domain',
],
'directives' => [],
];

protected static $functionsScannerClass = 'WP_CLI\I18n\PhpFunctionsScanner';
Expand Down
10 changes: 10 additions & 0 deletions src/BladeGettextExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
protected static function getBladeCompiler() {
$cache_path = empty( $options['cachePath'] ) ? sys_get_temp_dir() : $options['cachePath'];
$blade_compiler = new BladeOne( null, $cache_path );
$directives = empty( static::$options['directives'] ) ? [] : static::$options['directives'];

Check warning on line 24 in src/BladeGettextExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/BladeGettextExtractor.php#L24

Added line #L24 was not covered by tests

foreach ( $directives as $directive ) {
$blade_compiler->directive(
$directive,
function ( $expression ) {
return "<?php __($expression); ?>";
}
);

Check warning on line 32 in src/BladeGettextExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/BladeGettextExtractor.php#L26-L32

Added lines #L26 - L32 were not covered by tests
}

if ( method_exists( $blade_compiler, 'withoutComponentTags' ) ) {
$blade_compiler->withoutComponentTags();
Expand Down
19 changes: 19 additions & 0 deletions src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@
*/
protected $project_type = 'generic';

/**
* @var array
*/
protected $directives = [];

/**
* These Regexes copied from http://php.net/manual/en/function.sprintf.php#93552
* and adjusted for better precision and updated specs.
Expand Down Expand Up @@ -231,6 +236,9 @@
* Defaults to true, use `--no-location` to skip the removal.
* Note that disabling this option makes it harder for technically skilled translators to understand each message’s context.
*
* [--blade-directives=<blade-directives>]
* : Custom Blade directives that should be treated as translation functions. Comma separated list for multiple.
*
* [--skip-js]
* : Skips JavaScript string extraction. Useful when this is done in another build step, e.g. through Babel.
*
Expand Down Expand Up @@ -334,6 +342,15 @@
$this->file_comment = Utils\get_flag_value( $assoc_args, 'file-comment' );
$this->package_name = Utils\get_flag_value( $assoc_args, 'package-name' );
$this->location = Utils\get_flag_value( $assoc_args, 'location', true );
$this->directives = array_map(
function ( $directive ) {
return trim( ltrim( $directive, '@' ) );
},
explode(
',',
Utils\get_flag_value( $assoc_args, 'blade-directives', '' )
)
);

Check warning on line 353 in src/MakePotCommand.php

View check run for this annotation

Codecov / codecov/patch

src/MakePotCommand.php#L345-L353

Added lines #L345 - L353 were not covered by tests

$ignore_domain = Utils\get_flag_value( $assoc_args, 'ignore-domain', false );

Expand Down Expand Up @@ -659,6 +676,8 @@
'extensions' => [ 'blade.php' ],
'addReferences' => $this->location,
];

BladeCodeExtractor::$options['directives'] += $this->directives;

Check warning on line 680 in src/MakePotCommand.php

View check run for this annotation

Codecov / codecov/patch

src/MakePotCommand.php#L680

Added line #L680 was not covered by tests
BladeCodeExtractor::fromDirectory( $this->source, $translations, $options );
}

Expand Down