From 37b0c11a215f4489208b0a738375ef5df6b1b8ea Mon Sep 17 00:00:00 2001 From: dereuromark Date: Sat, 29 Apr 2017 01:24:25 +0200 Subject: [PATCH] Fix PO parsing around context. --- src/I18n/Parser/PoFileParser.php | 11 +++--- .../TestCase/I18n/MessagesFileLoaderTest.php | 4 +-- .../TestCase/I18n/Parser/PoFileParserTest.php | 36 ++++++++++++------- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/I18n/Parser/PoFileParser.php b/src/I18n/Parser/PoFileParser.php index c85917cfe6a..ecc78630b77 100644 --- a/src/I18n/Parser/PoFileParser.php +++ b/src/I18n/Parser/PoFileParser.php @@ -142,11 +142,10 @@ protected function _addMessage(array &$messages, array $item) $translation = stripcslashes($translation); - if ($context && (!isset($messages[$singular]) || is_array($messages[$singular]))) { + if ($context !== null) { $messages[$singular]['_context'][$context] = $translation; - } - if ($context === null) { - $messages[$singular] = $translation; + } else { + $messages[$singular]['_context'][''] = $translation; } if (isset($item['ids']['plural'])) { @@ -166,10 +165,10 @@ protected function _addMessage(array &$messages, array $item) $plurals = array_map('stripcslashes', $plurals); $key = stripcslashes($item['ids']['plural']); - if ($context) { + if ($context !== null) { $messages[$key]['_context'][$context] = $plurals; } else { - $messages[$key] = $plurals; + $messages[$key]['_context'][''] = $plurals; } } } diff --git a/tests/TestCase/I18n/MessagesFileLoaderTest.php b/tests/TestCase/I18n/MessagesFileLoaderTest.php index 0440ab6bf5d..46a346e3c52 100644 --- a/tests/TestCase/I18n/MessagesFileLoaderTest.php +++ b/tests/TestCase/I18n/MessagesFileLoaderTest.php @@ -56,12 +56,12 @@ public function testCustomLocalePath() $loader = new MessagesFileLoader('default', 'en'); $package = $loader(); $messages = $package->getMessages(); - $this->assertEquals('Po (translated)', $messages['Plural Rule 1']); + $this->assertEquals('Po (translated)', $messages['Plural Rule 1']['_context']['']); Configure::write('App.paths.locales', [TEST_APP . 'custom_locale' . DS]); $loader = new MessagesFileLoader('default', 'en'); $package = $loader(); $messages = $package->getMessages(); - $this->assertEquals('Po (translated) from custom folder', $messages['Plural Rule 1']); + $this->assertEquals('Po (translated) from custom folder', $messages['Plural Rule 1']['_context']['']); } } diff --git a/tests/TestCase/I18n/Parser/PoFileParserTest.php b/tests/TestCase/I18n/Parser/PoFileParserTest.php index 76b7757771a..f979a068780 100644 --- a/tests/TestCase/I18n/Parser/PoFileParserTest.php +++ b/tests/TestCase/I18n/Parser/PoFileParserTest.php @@ -37,7 +37,11 @@ public function testParse() $messages = $parser->parse($file); $this->assertCount(5, $messages); $expected = [ - 'Plural Rule 1' => 'Plural Rule 1 (translated)', + 'Plural Rule 1' => [ + '_context' => [ + '' => 'Plural Rule 1 (translated)', + ], + ], '%d = 1' => [ '_context' => [ 'This is the context' => 'First Context trasnlation', @@ -52,14 +56,22 @@ public function testParse() ] ] ], - '%-5d = 1' => '%-5d = 1 (translated)', + '%-5d = 1' => [ + '_context' => [ + '' => '%-5d = 1 (translated)', + ], + ], '%-5d = 0 or > 1' => [ - 0 => '%-5d = 1 (translated)', - 1 => '', - 2 => '', - 3 => '', - 4 => '%-5d = 0 or > 1 (translated)' - ] + '_context' => [ + '' => [ + 0 => '%-5d = 1 (translated)', + 1 => '', + 2 => '', + 3 => '', + 4 => '%-5d = 0 or > 1 (translated)' + ], + ], + ], ]; $this->assertEquals($expected, $messages); } @@ -75,7 +87,7 @@ public function testParseMultiLine() $file = APP . 'Locale' . DS . 'en' . DS . 'default.po'; $messages = $parser->parse($file); $this->assertCount(12, $messages); - $this->assertTextEquals("v\nsecond line", $messages["valid\nsecond line"]); + $this->assertTextEquals("v\nsecond line", $messages["valid\nsecond line"]['_context']['']); } /** @@ -89,7 +101,7 @@ public function testQuotedString() $file = APP . 'Locale' . DS . 'en' . DS . 'default.po'; $messages = $parser->parse($file); - $this->assertTextEquals('this is a "quoted string" (translated)', $messages['this is a "quoted string"']); + $this->assertTextEquals('this is a "quoted string" (translated)', $messages['this is a "quoted string"']['_context']['']); } /** @@ -113,7 +125,7 @@ public function testParseContextOnSomeMessages() return $package; }); - $this->assertTextEquals('En cours', $messages['Pending']); - $this->assertTextEquals('En resolved', $messages['Resolved']); + $this->assertTextEquals('En cours', $messages['Pending']['_context']['']); + $this->assertTextEquals('En resolved', $messages['Resolved']['_context']['']); } }