Skip to content

Commit

Permalink
Merge pull request #36 from adamzelycz/master
Browse files Browse the repository at this point in the history
Support for concatenated strings in PHP files
  • Loading branch information
voda authored Oct 11, 2021
2 parents 011a984 + 05e48d0 commit b4c81b9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Filters/PHPFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ private function processFunction(array $definition, Node $node, array $args): vo
if (count($message) === 1) { // line only
return;
}
} elseif ($arg instanceof Node\Expr\BinaryOp\Concat) {
$message[$type] = $this->processConcatenatedString($arg);
} else {
return;
}
Expand All @@ -112,6 +114,23 @@ private function processFunction(array $definition, Node $node, array $args): vo
}
}

private function processConcatenatedString(Node\Expr\BinaryOp\Concat $arg): string
{
$result = '';

if ($arg->left instanceof Node\Expr\BinaryOp\Concat) {
$result .= $this->processConcatenatedString($arg->left);
} elseif ($arg->left instanceof String_) {
$result .= $arg->left->value;
}

if ($arg->right instanceof String_) {
$result .= $arg->right->value;
}

return $result;
}

/* PhpParser\NodeVisitor: dont need these *******************************/

public function afterTraverse(array $nodes) {
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/GettextExtractor/Filters/PHPFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ public function testMessageWithNewlines(): void {
), $messages);
}

/** @dataProvider provideConcatenatedMessageFiles */
public function testConcatenatedMessage(string $phpFile): void {
$messages = $this->object->extract($phpFile);
self::assertContains(array(
GE\Extractor::LINE => 2,
GE\Extractor::SINGULAR => "A message!"
), $messages);
}

/** @return array<string, string[]> */
public function provideConcatenatedMessageFiles(): array {
return [
'String on one lines' => [__DIR__ . '/../../data/php/concatenatedMessageOneLine.php'],
'String on two lines' => [__DIR__ . '/../../data/php/concatenatedMessageTwoLines.php'],
'String on multiple lines with empty parts' => [__DIR__ . '/../../data/php/concatenatedMessageMultipleLines.php'],
];
}

public function testArrayAsParameter(): void {
$this->object->addFunction('addConfirmer', 3);
$messages = $this->object->extract(__DIR__ . '/../../data/php/arrayAsParameter.php');
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/data/php/concatenatedMessageMultipleLines.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
$translator->gettext('A ' .

'mess'

. 'age' . '' . '!' . '');
2 changes: 2 additions & 0 deletions tests/unit/data/php/concatenatedMessageOneLine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$translator->gettext('A ' . 'message!');
3 changes: 3 additions & 0 deletions tests/unit/data/php/concatenatedMessageTwoLines.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
$translator->gettext('A '
. 'message!');

0 comments on commit b4c81b9

Please sign in to comment.