Skip to content

Commit

Permalink
[Translation] Added unescaping of ids in PoFileLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
JustBlackBird authored and fabpot committed Jul 4, 2014
1 parent 3b2bff8 commit 816a4a9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Symfony/Component/Translation/Loader/PoFileLoader.php
Expand Up @@ -157,7 +157,7 @@ private function parse($resource)
private function addMessage(array &$messages, array $item)
{
if (is_array($item['translated'])) {
$messages[$item['ids']['singular']] = stripcslashes($item['translated'][0]);
$messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated'][0]);
if (isset($item['ids']['plural'])) {
$plurals = $item['translated'];
// PO are by definition indexed so sort by index.
Expand All @@ -169,10 +169,10 @@ private function addMessage(array &$messages, array $item)
$empties = array_fill(0, $count+1, '-');
$plurals += $empties;
ksort($plurals);
$messages[$item['ids']['plural']] = stripcslashes(implode('|', $plurals));
$messages[stripcslashes($item['ids']['plural'])] = stripcslashes(implode('|', $plurals));
}
} elseif (!empty($item['ids']['singular'])) {
$messages[$item['ids']['singular']] = stripcslashes($item['translated']);
$messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated']);
}
}
}
Expand Up @@ -76,4 +76,28 @@ public function testLoadEmptyTranslation()
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}

public function testEscapedId()
{
$loader = new PoFileLoader();
$resource = __DIR__.'/../fixtures/escaped-id.po';
$catalogue = $loader->load($resource, 'en', 'domain1');

$messages = $catalogue->all('domain1');
$this->assertArrayHasKey('escaped "foo"', $messages);
$this->assertEquals('escaped "bar"', $messages['escaped "foo"']);
}

public function testEscapedIdPlurals()
{
$loader = new PoFileLoader();
$resource = __DIR__.'/../fixtures/escaped-id-plurals.po';
$catalogue = $loader->load($resource, 'en', 'domain1');

$messages = $catalogue->all('domain1');
$this->assertArrayHasKey('escaped "foo"', $messages);
$this->assertArrayHasKey('escaped "foos"', $messages);
$this->assertEquals('escaped "bar"', $messages['escaped "foo"']);
$this->assertEquals('escaped "bar"|escaped "bars"', $messages['escaped "foos"']);
}
}
@@ -0,0 +1,10 @@
msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: en\n"

msgid "escaped \"foo\""
msgid_plural "escaped \"foos\""
msgstr[0] "escaped \"bar\""
msgstr[1] "escaped \"bars\""
@@ -0,0 +1,8 @@
msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: en\n"

msgid "escaped \"foo\""
msgstr "escaped \"bar\""

0 comments on commit 816a4a9

Please sign in to comment.