Skip to content

Commit

Permalink
Added support to MoParser for message contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 23, 2014
1 parent f261192 commit b460901
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/I18n/Parser/MoFileParser.php
Expand Up @@ -87,6 +87,8 @@ public function parse($resource) {
for ($i = 0; $i < $count; $i++) {
$pluralId = null;
$translated = null;
$context = null;
$plurals = null;

fseek($stream, $offsetId + $i * 8);

Expand All @@ -100,6 +102,10 @@ public function parse($resource) {
fseek($stream, $offset);
$singularId = fread($stream, $length);

if (strpos($singularId, "\x04") !== false) {
list($context, $singularId) = explode("\x04", $singularId);
}

if (strpos($singularId, "\000") !== false) {
list($singularId, $pluralId) = explode("\000", $singularId);
}
Expand All @@ -110,15 +116,24 @@ public function parse($resource) {
fseek($stream, $offset);
$translated = fread($stream, $length);

if (strpos($translated, "\000") === false) {
$messages[$singularId] = stripcslashes($translated);
if (strpos($translated, "\000") !== false) {
$translated = explode("\000", $translated);
$plurals = $pluralId !== null ? array_map('stripcslashes', $translated) : null;
$translated = $translated[0];
}

$singular = stripcslashes($translated);
if ($context !== null) {
$messages[$singularId]['_context'][$context] = $singular;
if ($pluralId !== null) {
$messages[$pluralId]['_context'][$context] = $plurals;
}
continue;
}

$translated = explode("\000", $translated);
$messages[$singularId] = stripcslashes($translated[0]);
$messages[$singularId] = $singular;
if ($pluralId !== null) {
$messages[$pluralId] = array_map('stripcslashes', $translated);
$messages[$pluralId] = $plurals;
}
}

Expand All @@ -139,4 +154,5 @@ protected function _readLong($stream, $isBigEndian) {

return (int)substr($result, -8);
}

}
35 changes: 35 additions & 0 deletions tests/TestCase/I18n/Parser/MoFileParserTest.php
Expand Up @@ -66,4 +66,39 @@ public function testParse2() {
$this->assertEquals($expected, $messages);
}

/**
* Tests parsing a file with plurals and message context
*
* @return void
*/
public function testParseFull() {
$parser = new MoFileParser;
$file = APP . 'Locale' . DS . 'rule_0_mo' . DS . 'default.mo';
$messages = $parser->parse($file);
$this->assertCount(5, $messages);
$expected = [
'Plural Rule 1' => 'Plural Rule 1 (translated)',
'%d = 1' => [
'_context' => [
'This is the context' => 'First Context trasnlation',
'Another Context' => '%d = 1 (translated)'
]
],
'%d = 0 or > 1' => [
'_context' => [
'Another Context' => [
0 => '%d = 1 (translated)',
1 => '%d = 0 or > 1 (translated)'
]
]
],
'%-5d = 1' => '%-5d = 1 (translated)',
'%-5d = 0 or > 1' => [
'%-5d = 1 (translated)',
'%-5d = 0 or > 1 (translated)'
]
];
$this->assertEquals($expected, $messages);
}

}

0 comments on commit b460901

Please sign in to comment.