Skip to content
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
6 changes: 6 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,8 @@ Feature: Generate a POT file of a WordPress project

Object(u.__)( 'minified.__', 'foo-plugin' );
Object(j._x)( 'minified._x', 'minified._x_context', 'foo-plugin' );

eval( "__( 'Hello Eval World', 'foo-plugin' );" );
"""

When I run `wp i18n make-pot foo-plugin`
Expand Down Expand Up @@ -1696,6 +1698,10 @@ Feature: Generate a POT file of a WordPress project
"""
msgctxt "minified._x_context"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "Hello Eval World"
"""
And the foo-plugin/foo-plugin.pot file should not contain:
"""
msgid "wrong-domain"
Expand Down
49 changes: 48 additions & 1 deletion src/JsFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,14 @@ function ( $node ) use ( &$translations, $options, &$all_comments ) {
return;
}

if ( isset( $options['line'] ) ) {
$line = $options['line'];
} else {
$line = $node->getLocation()->getStart()->getLine();
}

$translation = $translations->insert( $context, $original, $plural );
$translation->addReference( $file, $node->getLocation()->getStart()->getLine() );
$translation->addReference( $file, $line );

/** @var Node\Comment $comment */
foreach ( $all_comments as $comment ) {
Expand All @@ -170,6 +176,47 @@ function ( $node ) use ( &$translations, $options, &$all_comments ) {
}
);

/**
* Traverse through JS code contained within eval() to find and extract gettext functions.
*/
$scanner = $this;
$traverser->addFunction(
function ( $node ) use ( &$translations, $options, $scanner ) {
/** @var Node\CallExpression $node */
if ( 'CallExpression' !== $node->getType() ) {
return;
}

$callee = $this->resolveExpressionCallee( $node );

if ( ! $callee || 'eval' !== $callee['name'] ) {
return;
}

$eval_contents = '';
/** @var Node\Node $argument */
foreach ( $node->getArguments() as $argument ) {
if ( 'Literal' === $argument->getType() ) {
/** @var Node\Literal $argument */
$eval_contents = $argument->getValue();
break;
}
}

if ( ! $eval_contents ) {
return;
}

// Override the line location to be that of the eval().
$options['line'] = $node->getLocation()->getStart()->getLine();

$class = get_class( $scanner );
$evals = new $class( $eval_contents );
$evals->enableCommentsExtraction( $options['extractComments'] );
$evals->saveGettextFunctions( $translations, $options );
}
);

$traverser->traverse( $ast );
}

Expand Down