Skip to content

Commit

Permalink
Attempt to fix the singular/plural issue in the po parser
Browse files Browse the repository at this point in the history
  • Loading branch information
burzum committed Dec 5, 2017
1 parent f67679b commit cd19ea7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
6 changes: 4 additions & 2 deletions src/I18n/Parser/PoFileParser.php
Expand Up @@ -24,6 +24,8 @@
class PoFileParser
{

const PLURAL_PREFIX = 'p:';

/**
* Parses portable object (PO) format.
*
Expand Down Expand Up @@ -166,9 +168,9 @@ protected function _addMessage(array &$messages, array $item)
$key = stripcslashes($item['ids']['plural']);

if ($context !== null) {
$messages[$key]['_context'][$context] = $plurals;
$messages[static::PLURAL_PREFIX . $key]['_context'][$context] = $plurals;
} else {
$messages[$key]['_context'][''] = $plurals;
$messages[static::PLURAL_PREFIX . $key]['_context'][''] = $plurals;
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/I18n/Translator.php
Expand Up @@ -22,6 +22,8 @@
class Translator extends BaseTranslator
{

const PLURAL_PREFIX = 'p:';

/**
* Translates the message formatting any placeholders
*
Expand All @@ -33,7 +35,18 @@ class Translator extends BaseTranslator
*/
public function translate($key, array $tokensValues = [])
{
$message = $this->getMessage($key);
if (isset($tokensValues['_count'])) {
$message = $this->getMessage(static::PLURAL_PREFIX . $key);
if (!$message) {
$message = $this->getMessage($key);
}
} else {
$message = $this->getMessage($key);
if (!$message) {
$message = $this->getMessage(static::PLURAL_PREFIX . $key);
}
}

if (!$message) {
// Fallback to the message key
$message = $key;
Expand Down
32 changes: 16 additions & 16 deletions tests/TestCase/I18n/Parser/PoFileParserTest.php
Expand Up @@ -65,39 +65,39 @@ public function testParse()
$expected = [
'Plural Rule 1' => [
'_context' => [
'' => 'Plural Rule 1 (translated)',
],
'' => 'Plural Rule 1 (translated)'
]
],
'%d = 1' => [
'_context' => [
'This is the context' => 'First Context trasnlation',
'Another Context' => '%d = 1 (translated)'
]
],
'%d = 0 or > 1' => [
'p:%d = 0 or > 1' => [
'_context' => [
'Another Context' => [
0 => '%d = 1 (translated)',
1 => '%d = 0 or > 1 (translated)'
(int)0 => '%d = 1 (translated)',
(int)1 => '%d = 0 or > 1 (translated)'
]
]
],
'%-5d = 1' => [
'_context' => [
'' => '%-5d = 1 (translated)',
],
'' => '%-5d = 1 (translated)'
]
],
'%-5d = 0 or > 1' => [
'p:%-5d = 0 or > 1' => [
'_context' => [
'' => [
0 => '%-5d = 1 (translated)',
1 => '',
2 => '',
3 => '',
4 => '%-5d = 0 or > 1 (translated)'
],
],
],
(int)0 => '%-5d = 1 (translated)',
(int)1 => '',
(int)2 => '',
(int)3 => '',
(int)4 => '%-5d = 0 or > 1 (translated)'
]
]
]
];
$this->assertEquals($expected, $messages);
}
Expand Down

0 comments on commit cd19ea7

Please sign in to comment.