-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #304 from strarsis/add-blade-extractor
- Loading branch information
Showing
8 changed files
with
341 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace WP_CLI\I18n; | ||
|
||
use Exception; | ||
use Gettext\Translations; | ||
use WP_CLI; | ||
|
||
final class BladeCodeExtractor extends BladeGettextExtractor { | ||
use IterableCodeExtractor; | ||
|
||
public static $options = [ | ||
'extractComments' => [ 'translators', 'Translators' ], | ||
'constants' => [], | ||
'functions' => [ | ||
'__' => 'text_domain', | ||
'esc_attr__' => 'text_domain', | ||
'esc_html__' => 'text_domain', | ||
'esc_xml__' => 'text_domain', | ||
'_e' => 'text_domain', | ||
'esc_attr_e' => 'text_domain', | ||
'esc_html_e' => 'text_domain', | ||
'esc_xml_e' => 'text_domain', | ||
'_x' => 'text_context_domain', | ||
'_ex' => 'text_context_domain', | ||
'esc_attr_x' => 'text_context_domain', | ||
'esc_html_x' => 'text_context_domain', | ||
'esc_xml_x' => 'text_context_domain', | ||
'_n' => 'single_plural_number_domain', | ||
'_nx' => 'single_plural_number_context_domain', | ||
'_n_noop' => 'single_plural_domain', | ||
'_nx_noop' => 'single_plural_context_domain', | ||
|
||
// Compat. | ||
'_' => 'gettext', // Same as 'text_domain'. | ||
|
||
// Deprecated. | ||
'_c' => 'text_domain', | ||
'_nc' => 'single_plural_number_domain', | ||
'__ngettext' => 'single_plural_number_domain', | ||
'__ngettext_noop' => 'single_plural_domain', | ||
], | ||
]; | ||
|
||
protected static $functionsScannerClass = 'WP_CLI\I18n\PhpFunctionsScanner'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function fromString( $string, Translations $translations, array $options = [] ) { | ||
WP_CLI::debug( "Parsing file {$options['file']}", 'make-pot' ); | ||
|
||
try { | ||
static::fromStringMultiple( $string, [ $translations ], $options ); | ||
} catch ( Exception $exception ) { | ||
WP_CLI::debug( | ||
sprintf( | ||
'Could not parse file %1$s: %2$s', | ||
$options['file'], | ||
$exception->getMessage() | ||
), | ||
'make-pot' | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace WP_CLI\I18n; | ||
|
||
use eftec\bladeone\BladeOne; | ||
|
||
// Modified Gettext Blade extractor that | ||
// uses the up-to-date BladeOne standalone Blade engine, | ||
// correctly supports fromStringMultiple. | ||
|
||
/** | ||
* Class to get gettext strings from blade.php files returning arrays. | ||
*/ | ||
class BladeGettextExtractor extends \Gettext\Extractors\PhpCode { | ||
|
||
/** | ||
* Prepares a Blade compiler/engine and returns it. | ||
* | ||
* @return BladeOne | ||
*/ | ||
protected static function getBladeCompiler() { | ||
$cache_path = empty( $options['cachePath'] ) ? sys_get_temp_dir() : $options['cachePath']; | ||
$blade_compiler = new BladeOne( null, $cache_path ); | ||
|
||
if ( method_exists( $blade_compiler, 'withoutComponentTags' ) ) { | ||
$blade_compiler->withoutComponentTags(); | ||
} | ||
|
||
return $blade_compiler; | ||
} | ||
|
||
/** | ||
* Compiles the Blade template string into a PHP string in one step. | ||
* | ||
* @param string $string Blade string to be compiled to a PHP string | ||
* @return string | ||
*/ | ||
protected static function compileBladeToPhp( $string ) { | ||
return static::getBladeCompiler()->compileString( $string ); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* Note: In the parent PhpCode class fromString() uses fromStringMultiple() (overriden here) | ||
*/ | ||
public static function fromStringMultiple( $string, array $translations, array $options = [] ) { | ||
$php_string = static::compileBladeToPhp( $string ); | ||
return parent::fromStringMultiple( $php_string, $translations, $options ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.