Skip to content

Commit 688e6ea

Browse files
committed
bug #24594 [Translation] Fix InvalidArgumentException when using untranslated plural forms from .po files (BjornTwachtmann)
This PR was squashed before being merged into the 3.3 branch (closes #24594). Discussion ---------- [Translation] Fix InvalidArgumentException when using untranslated plural forms from .po files | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #24593 | License | MIT This PR fixes the bug in #24593. It's not the absolutely ideal way to address the issue, but I don't see how else to handle it without either dropping support for pips in translation strings, or rearchitecting the code that feeds translations to the MessageSelector as pipe separated in the first place. Commits ------- fea815b2f5 [Translation] Fix InvalidArgumentException when using untranslated plural forms from .po files
2 parents 373e553 + 11881fa commit 688e6ea

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

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

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)