Skip to content

Commit cd19ea7

Browse files
committed
Attempt to fix the singular/plural issue in the po parser
1 parent f67679b commit cd19ea7

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

src/I18n/Parser/PoFileParser.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
class PoFileParser
2525
{
2626

27+
const PLURAL_PREFIX = 'p:';
28+
2729
/**
2830
* Parses portable object (PO) format.
2931
*
@@ -166,9 +168,9 @@ protected function _addMessage(array &$messages, array $item)
166168
$key = stripcslashes($item['ids']['plural']);
167169

168170
if ($context !== null) {
169-
$messages[$key]['_context'][$context] = $plurals;
171+
$messages[static::PLURAL_PREFIX . $key]['_context'][$context] = $plurals;
170172
} else {
171-
$messages[$key]['_context'][''] = $plurals;
173+
$messages[static::PLURAL_PREFIX . $key]['_context'][''] = $plurals;
172174
}
173175
}
174176
}

src/I18n/Translator.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
class Translator extends BaseTranslator
2323
{
2424

25+
const PLURAL_PREFIX = 'p:';
26+
2527
/**
2628
* Translates the message formatting any placeholders
2729
*
@@ -33,7 +35,18 @@ class Translator extends BaseTranslator
3335
*/
3436
public function translate($key, array $tokensValues = [])
3537
{
36-
$message = $this->getMessage($key);
38+
if (isset($tokensValues['_count'])) {
39+
$message = $this->getMessage(static::PLURAL_PREFIX . $key);
40+
if (!$message) {
41+
$message = $this->getMessage($key);
42+
}
43+
} else {
44+
$message = $this->getMessage($key);
45+
if (!$message) {
46+
$message = $this->getMessage(static::PLURAL_PREFIX . $key);
47+
}
48+
}
49+
3750
if (!$message) {
3851
// Fallback to the message key
3952
$message = $key;

tests/TestCase/I18n/Parser/PoFileParserTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,39 +65,39 @@ public function testParse()
6565
$expected = [
6666
'Plural Rule 1' => [
6767
'_context' => [
68-
'' => 'Plural Rule 1 (translated)',
69-
],
68+
'' => 'Plural Rule 1 (translated)'
69+
]
7070
],
7171
'%d = 1' => [
7272
'_context' => [
7373
'This is the context' => 'First Context trasnlation',
7474
'Another Context' => '%d = 1 (translated)'
7575
]
7676
],
77-
'%d = 0 or > 1' => [
77+
'p:%d = 0 or > 1' => [
7878
'_context' => [
7979
'Another Context' => [
80-
0 => '%d = 1 (translated)',
81-
1 => '%d = 0 or > 1 (translated)'
80+
(int)0 => '%d = 1 (translated)',
81+
(int)1 => '%d = 0 or > 1 (translated)'
8282
]
8383
]
8484
],
8585
'%-5d = 1' => [
8686
'_context' => [
87-
'' => '%-5d = 1 (translated)',
88-
],
87+
'' => '%-5d = 1 (translated)'
88+
]
8989
],
90-
'%-5d = 0 or > 1' => [
90+
'p:%-5d = 0 or > 1' => [
9191
'_context' => [
9292
'' => [
93-
0 => '%-5d = 1 (translated)',
94-
1 => '',
95-
2 => '',
96-
3 => '',
97-
4 => '%-5d = 0 or > 1 (translated)'
98-
],
99-
],
100-
],
93+
(int)0 => '%-5d = 1 (translated)',
94+
(int)1 => '',
95+
(int)2 => '',
96+
(int)3 => '',
97+
(int)4 => '%-5d = 0 or > 1 (translated)'
98+
]
99+
]
100+
]
101101
];
102102
$this->assertEquals($expected, $messages);
103103
}

0 commit comments

Comments
 (0)