Skip to content

Commit

Permalink
-- add dumpers for translation component
Browse files Browse the repository at this point in the history
-- update license and phpDoc

-- fix CS

-- remove pot file loader

-- add unit tests
  • Loading branch information
Michel Salib committed Aug 29, 2011
1 parent a746055 commit 6278fcb
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 12 deletions.
Expand Up @@ -11,6 +11,9 @@
<parameter key="translation.loader.php.class">Symfony\Component\Translation\Loader\PhpFileLoader</parameter>
<parameter key="translation.loader.yml.class">Symfony\Component\Translation\Loader\YamlFileLoader</parameter>
<parameter key="translation.loader.xliff.class">Symfony\Component\Translation\Loader\XliffFileLoader</parameter>
<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>
</parameters>

<services>
Expand Down Expand Up @@ -42,5 +45,17 @@
<service id="translation.loader.xliff" class="%translation.loader.xliff.class%">
<tag name="translation.loader" alias="xliff" />
</service>

<service id="translation.dumper.php" class="%translation.dumper.php.class%">
<tag name="translation.dumper" alias="php" />
</service>

<service id="tranlsation.dumper.xliff" class="%translation.dumper.xliff.class%">

This comment has been minimized.

Copy link
@docteurklein

docteurklein Sep 5, 2011

Contributor

There is a typo here. ( tranlsation instead of translation )

<tag name="translation.dumper" alias="xliff" />
</service>

<service id="translation.dumper.yml" class="%translation.dumper.yml.class%">
<tag name="translation.dumper" alias="yml" />
</service>
</services>
</container>
32 changes: 32 additions & 0 deletions src/Symfony/Component/Translation/Dumper/DumperInterface.php
@@ -0,0 +1,32 @@
<?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;

/**
* DumperInterface is the interface implemented by all translation dumpers.
*
* @author Michel Salib <michelsalib@hotmail.com>
*/
interface DumperInterface
{
/**
* Generates a string representation of the message catalogue
*
* @param MessageCatalogue $messages The message catalogue
* @param string $domain The domain to dump
*
* @return string The string representation
*/
function dump(MessageCatalogue $messages, $domain = 'messages');
}
32 changes: 32 additions & 0 deletions src/Symfony/Component/Translation/Dumper/PhpDumper.php
@@ -0,0 +1,32 @@
<?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;

/**
* PhpDumper generates a php formated string representation of a message catalogue
*
* @author Michel Salib <michelsalib@hotmail.com>
*/
class PhpDumper implements DumperInterface
{
/**
* {@inheritDoc}
*/
public function dump(MessageCatalogue $messages, $domain = 'messages')
{
$output = "<?php\n\nreturn ".var_export($messages->all($domain), true).";\n";

return $output;
}
}
52 changes: 52 additions & 0 deletions src/Symfony/Component/Translation/Dumper/XliffDumper.php
@@ -0,0 +1,52 @@
<?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;

/**
* XliffDumper generates a xliff formated string representation of a message catalogue
*
* @author Michel Salib <michelsalib@hotmail.com>
*/
class XliffDumper implements DumperInterface
{
/**
* {@inheritDoc}
*/
public function dump(MessageCatalogue $messages, $domain = 'messages')
{
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;
$xliff = $dom->appendChild($dom->createElement('xliff'));
$xliff->setAttribute('version', '1.2');
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
$xliffFile = $xliff->appendChild($dom->createElement('file'));
$xliffFile->setAttribute('source-language', $messages->getLocale());
$xliffFile->setAttribute('datatype', 'plaintext');
$xliffFile->setAttribute('original', 'file.ext');
$xliffBody = $xliffFile->appendChild($dom->createElement('body'));
$id = 1;
foreach ($messages->all($domain) as $source => $target) {
$trans = $dom->createElement('trans-unit');
$trans->setAttribute('id', $id);
$s = $trans->appendChild($dom->createElement('source'));
$s->appendChild($dom->createTextNode($source));
$t = $trans->appendChild($dom->createElement('target'));
$t->appendChild($dom->createTextNode($target));
$xliffBody->appendChild($trans);
$id++;
}

return $dom->saveXML();
}
}
31 changes: 31 additions & 0 deletions src/Symfony/Component/Translation/Dumper/YamlDumper.php
@@ -0,0 +1,31 @@
<?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;
use Symfony\Component\Yaml\Yaml;

/**
* YamlDumper generates a yaml formated string representation of a message catalogue
*
* @author Michel Salib <michelsalib@hotmail.com>
*/
class YamlDumper implements DumperInterface
{
/**
* {@inheritDoc}
*/
public function dump(MessageCatalogue $messages, $domain = 'messages')
{
return Yaml::dump($messages->all($domain));
}
}
@@ -0,0 +1,36 @@
<?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\PhpDumper;

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

$dumper = new PhpDumper();
$dumperString = $dumper->dump($catalogue);

$resource = __DIR__.'/../fixtures/resources.php';
$file = new \SplFileObject($resource);
$fileString = '';
while(!$file->eof()) {
$fileString .= $file->fgets();
}

$this->assertEquals($fileString, $dumperString);
}
}
@@ -0,0 +1,36 @@
<?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\XliffDumper;

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

$dumper = new XliffDumper();
$dumperString = $dumper->dump($catalogue);

$resource = __DIR__.'/../fixtures/resources.xliff';
$file = new \SplFileObject($resource);
$fileString = '';
while(!$file->eof()) {
$fileString .= $file->fgets();
}

$this->assertEquals($fileString, $dumperString);
}
}
@@ -0,0 +1,36 @@
<?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\YamlDumper;

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

$dumper = new YamlDumper();
$dumperString = $dumper->dump($catalogue);

$resource = __DIR__.'/../fixtures/resources.yml';
$file = new \SplFileObject($resource);
$fileString = '';
while(!$file->eof()) {
$fileString .= $file->fgets();
}

$this->assertEquals($fileString, $dumperString);
}
}
@@ -1,5 +1,5 @@
<?php

return array(
'foo' => 'bar',
return array (
'foo' => 'bar',
);
20 changes: 10 additions & 10 deletions tests/Symfony/Tests/Component/Translation/fixtures/resources.xliff
@@ -1,11 +1,11 @@
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1">
<source>foo</source>
<target>bar</target>
</trans-unit>
</body>
</file>
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1">
<source>foo</source>
<target>bar</target>
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit 6278fcb

Please sign in to comment.