Skip to content

Commit

Permalink
Merge pull request #7 from cooperaj/internal_whitespace_removal
Browse files Browse the repository at this point in the history
Remove whitespace in translation keys of more than 2 characters.
  • Loading branch information
cooperaj authored Jul 7, 2023
2 parents d3fc27c + 4607807 commit 1db25d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/TokenParser/TransTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ public function parse(Token $token): Node
[$body, $plural] = $this->parsePluralisation($body);
}

// strip whitespace of more than 1 character
/** @var string $msg */
$msg = $body->getAttribute('data');
$body->setAttribute(
'data',
preg_replace('/\s{2,}/', ' ', $msg)
);

return new TransNode($body, $plural, $domain, $count, $vars, $notes, $context, $lineno, $this->getTag());
}

Expand All @@ -162,7 +170,7 @@ public function getTag(): string
* @throws UnhandledPluralisationRuleException
* @copyright Fabien Potencier <fabien@symfony.com>
*
* @see Symfony\Contracts\Translation\TranslatorInterface for more information on the format.
* @see \Symfony\Contracts\Translation\TranslatorInterface for more information on the format.
*
* Copied from Symfony\Component\Translation\Dumper\PoFileDumper
*/
Expand Down Expand Up @@ -214,4 +222,4 @@ private function parsePluralisation(TextNode $body): array

return [$body, $plural];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--TEST--
strip internal whitespace from keys
--TEMPLATE--
<p>{% trans %}If your translation key contains new lines due to syntax indentation we want
to be sure that they're all stripped out{% endtrans %}</p>
--DATA--
return []
--EXPECT--
<p>If your translation key contains new lines due to syntax indentation we want to be sure that they're all stripped out</p>

0 comments on commit 1db25d3

Please sign in to comment.