Skip to content
Permalink
Browse files

Attempt to fix the singular/plural issue in the po parser

  • Loading branch information...
burzum committed Dec 5, 2017
1 parent f67679b commit cd19ea7f8b1f5cbf57f8ef0b539a03de924ea3e9
Showing with 34 additions and 19 deletions.
  1. +4 −2 src/I18n/Parser/PoFileParser.php
  2. +14 −1 src/I18n/Translator.php
  3. +16 −16 tests/TestCase/I18n/Parser/PoFileParserTest.php
@@ -24,6 +24,8 @@
class PoFileParser
{
const PLURAL_PREFIX = 'p:';
/**
* Parses portable object (PO) format.
*
@@ -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;
}
}
}
@@ -22,6 +22,8 @@
class Translator extends BaseTranslator
{
const PLURAL_PREFIX = 'p:';
/**
* Translates the message formatting any placeholders
*
@@ -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;
@@ -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);
}

0 comments on commit cd19ea7

Please sign in to comment.
You can’t perform that action at this time.