Skip to content

Commit

Permalink
Adjust QtTranslations patch and include QtTranslationsDumper + test a…
Browse files Browse the repository at this point in the history
…swell
  • Loading branch information
beberlei committed Sep 4, 2011
1 parent 5712798 commit 4ac380e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 11 deletions.
Expand Up @@ -15,6 +15,7 @@
<parameter key="translation.dumper.php.class">Symfony\Component\Translation\Dumper\PhpDumper</parameter>
<parameter key="translation.dumper.xliff.class">Symfony\Component\Translation\Dumper\XliffDumper</parameter>
<parameter key="translation.dumper.yml.class">Symfony\Component\Translation\Dumper\YamlDumper</parameter>
<parameter key="translation.dumper.qt.class">Symfony\Component\Translation\Dumper\QtDumper</parameter>
</parameters>

<services>
Expand Down Expand Up @@ -47,10 +48,10 @@
<tag name="translation.loader" alias="xliff" />
</service>

<<<<<<< HEAD
<service id="translation.loader.qt" class="%translation.loader.qt.class%">
<tag name="translation.loader" alias="ts" />
=======
</service>

<service id="translation.dumper.php" class="%translation.dumper.php.class%">
<tag name="translation.dumper" alias="php" />
</service>
Expand All @@ -61,7 +62,10 @@

<service id="translation.dumper.yml" class="%translation.dumper.yml.class%">
<tag name="translation.dumper" alias="yml" />
>>>>>>> symfony/master
</service>

<service id="translation.dumper.qt" class="%translation.dumper.qt.class%">
<tag name="translation.dumper" alias="ts" />
</service>
</services>
</container>
39 changes: 39 additions & 0 deletions src/Symfony/Component/Translation/Dumper/QtTranslationsDumper.php
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Dumper;

use Symfony\Component\Translation\MessageCatalogue;

/**
* QtTranslationsDumper generates a TS/XML formated string representation of a message catalogue
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class QtTranslationsDumper implements DumperInterface
{
public function dump(MessageCatalogue $messages, $domain = 'messages')
{
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;
$ts = $dom->appendChild($dom->createElement('TS'));
$context = $ts->appendChild($dom->createElement('context'));
$context->appendChild($dom->createElement('name', $domain));

foreach ($messages->all($domain) as $source => $target) {
$message = $context->appendChild($dom->createElement('message'));
$message->appendChild($dom->createElement('source', $source));
$message->appendChild($dom->createElement('translation', $target));
}

return $dom->saveXML();
}
}
29 changes: 29 additions & 0 deletions tests/Symfony/Tests/Component/Translation/Dumper/QtDumperTest.php
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Component\Translation\Dumper;

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Dumper\QtTranslationsDumper;

class QtTranslationsDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDump()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'), 'resources');

$dumper = new QtTranslationsDumper();
$dumperString = $dumper->dump($catalogue, 'resources');

$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.ts'), $dumperString);
}
}
16 changes: 8 additions & 8 deletions tests/Symfony/Tests/Component/Translation/fixtures/resources.ts
@@ -1,10 +1,10 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<TS>
<context>
<name>resources</name>
<message>
<source>foo</source>
<translation>bar</translation>
</message>
</context>
<context>
<name>resources</name>
<message>
<source>foo</source>
<translation>bar</translation>
</message>
</context>
</TS>

0 comments on commit 4ac380e

Please sign in to comment.