Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix PO parsing around context.
  • Loading branch information
dereuromark authored and dereuromark committed May 22, 2017
1 parent 18f68aa commit 37b0c11
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
11 changes: 5 additions & 6 deletions src/I18n/Parser/PoFileParser.php
Expand Up @@ -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'])) {
Expand All @@ -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;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/I18n/MessagesFileLoaderTest.php
Expand Up @@ -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']['']);
}
}
36 changes: 24 additions & 12 deletions tests/TestCase/I18n/Parser/PoFileParserTest.php
Expand Up @@ -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',
Expand All @@ -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);
}
Expand All @@ -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']['']);
}

/**
Expand All @@ -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']['']);
}

/**
Expand All @@ -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']['']);
}
}

0 comments on commit 37b0c11

Please sign in to comment.