[Translation][transChoice] allows escaping the pipe character.

This commit is contained in:
Abdellatif Ait boudad 2016-06-14 14:08:09 +00:00
parent 5c91f6e671
commit a35a93bf49
3 changed files with 11 additions and 4 deletions

View File

@ -1,6 +1,11 @@
CHANGELOG CHANGELOG
========= =========
3.2.0
-----
* Added support for escaping `|` in plural translations with double pipe.
3.1.0 3.1.0
----- -----

View File

@ -47,11 +47,11 @@ class MessageSelector
*/ */
public function choose($message, $number, $locale) public function choose($message, $number, $locale)
{ {
$parts = explode('|', $message); preg_match_all('/(?:\|\||[^\|])++/', $message, $parts);
$explicitRules = array(); $explicitRules = array();
$standardRules = array(); $standardRules = array();
foreach ($parts as $part) { foreach ($parts[0] as $part) {
$part = trim($part); $part = trim(str_replace('||', '|', $part));
if (preg_match('/^(?P<interval>'.Interval::getIntervalRegexp().')\s*(?P<message>.*?)$/xs', $part, $matches)) { if (preg_match('/^(?P<interval>'.Interval::getIntervalRegexp().')\s*(?P<message>.*?)$/xs', $part, $matches)) {
$explicitRules[$matches['interval']] = $matches['message']; $explicitRules[$matches['interval']] = $matches['message'];
@ -74,7 +74,7 @@ class MessageSelector
if (!isset($standardRules[$position])) { if (!isset($standardRules[$position])) {
// when there's exactly one rule given, and that rule is a standard // when there's exactly one rule given, and that rule is a standard
// rule, use this rule // rule, use this rule
if (1 === count($parts) && isset($standardRules[0])) { if (1 === count($parts[0]) && isset($standardRules[0])) {
return $standardRules[0]; return $standardRules[0];
} }

View File

@ -125,6 +125,8 @@ class MessageSelectorTest extends \PHPUnit_Framework_TestCase
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), 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),
// with double-quotes and id split accros lines // with double-quotes and id split accros lines
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), 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),
// esacape pipe
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),
); );
} }
} }