8000 [Translation][transChoice] allows escaping the pipe character. · symfony/symfony@94fd3b7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 94fd3b7

Browse files
committed
[Translation][transChoice] allows escaping the pipe character.
1 parent 5c91f6e commit 94fd3b7

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
3.2.0
5+
-----
6+
7+
* Added support for escaping `|` in plural translations with double pipe.
8+
49
3.1.0
510
-----
611

src/Symfony/Component/Translation/MessageSelector.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ class MessageSelector
4747
*/
4848
public function choose($message, $number, $locale)
4949
{
50-
$parts = explode('|', $message);
50+
preg_match_all('/(?:\|\|.|[^\|])+/', $message, $parts);
5151
$explicitRules = array();
5252
$standardRules = array();
53-
foreach ($parts as $part) {
54-
$part = trim($part);
53+
foreach ($parts[0] as $part) {
54+
$part = trim(str_replace('||', '|', $part));
5555

5656
if (preg_match('/^(?P<interval>'.Interval::getIntervalRegexp().')\s*(?P<message>.*?)$/xs', $part, $matches)) {
5757
$explicitRules[$matches['interval']] = $matches['message'];
@@ -74,7 +74,7 @@ public function choose($message, $number, $locale)
7474
if (!isset($standardRules[$position])) {
7575
// when there's exactly one rule given, and that rule is a standard
7676
// rule, use this rule
77-
if (1 === count($parts) && isset($standardRules[0])) {
77+
if (1 === count($parts[0]) && isset($standardRules[0])) {
7878
return $standardRules[0];
7979
}
8080

src/Symfony/Component/Translation/Tests/MessageSelectorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ public function getChooseTests()
125125
array('This is a text with a\nnew-line in it. Selector = 0.', '{0}This is a text with a\nnew-line in it. Selector = 0.|{1}This is a text with a\nnew-line in it. Selector = 1.|[1,Inf]This is a text with a\nnew-line in it. Selector > 1.', 0),
126126
// with double-quotes and id split accros lines
127127
array("This is a text with a\nnew-line in it. Selector = 1.", "{0}This is a text with a\nnew-line in it. Selector = 0.|{1}This is a text with a\nnew-line in it. Selector = 1.|[1,Inf]This is a text with a\nnew-line in it. Selector > 1.", 1),
128+
// esacape pipe
129+
array('This is a text with | in it. Selector = 0.', "{0}This is a text with || in it. Selector = 0.|{1}This is a text with | in it. Selector = 1.", 0),
128130
);
129131
}
130132
}

0 commit comments

Comments
 (0)
0