Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for concatenated strings in PHP files #36

Merged
merged 1 commit into from
Oct 11, 2021
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
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!');