8000 [Translation] Fix InvalidArgumentException when using untranslated pl… · symfony/symfony@fea815b · GitHub
[go: up one dir, main page]

Skip to content
< 8000 header class="HeaderMktg header-logged-out js-details-container js-header Details f4 py-3" role="banner" data-is-top="true" data-color-mode=light data-light-theme=light data-dark-theme=dark>

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fea815b

Browse files
BjornTwachtmannfabpot
authored andcommitted
[Translation] Fix InvalidArgumentException when using untranslated plural forms from .po files
1 parent 7ac01bc commit fea815b

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/Symfony/Component/Translation/MessageSelector.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,16 @@ class MessageSelector
4949
*/
5050
public function choose($message, $number, $locale)
5151
{
52-
preg_match_all('/(?:\|\||[^\|])++/', $message, $parts);
52+
$parts = array();
53+
if (preg_match('/^\|++$/', $message)) {
54+
$parts = explode('|', $message);
55+
} elseif (preg_match_all('/(?:\|\||[^\|])++/', $message, $matches)) {
56+
$parts = $matches[0];
57+
}
58+
5359
$explicitRules = array();
5460
$standardRules = array();
55-
foreach ($parts[0] as $part) {
61+
foreach ($parts as $part) {
5662
$part = trim(str_replace('||', '|', $part));
5763

5864
if (preg_match('/^(?P<interval>'.Interval::getIntervalRegexp().')\s*(?P<message>.*?)$/xs', $part, $matches)) {
@@ -76,7 +82,7 @@ public function choose($message, $number, $locale)
7682
if (!isset($standardRules[$position])) {
7783
// when there's exactly one rule given, and that rule is a standard
7884
// rule, use this rule
79-
if (1 === count($parts[0]) && isset($standardRules[0])) {
85+
if (1 === count($parts) && isset($standardRules[0])) {
8086
return $standardRules[0];
8187
}
8288

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ public function getChooseTests()
128128
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),
129129
// esacape pipe
130130
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),
131+
// Empty plural set (2 plural forms) from a .PO file
132+
array('', '|', 1),
133+
// Empty plural set (3 plural forms) from a .PO file
134+
array('', '||', 1),
131135
);
132136
}
133137
}

0 commit comments

Comments
 (0)
0