8000 merged branch michelsalib/translation-formatter (PR #2045) · symfony/symfony@b217d70 · GitHub
[go: up one dir, main page]

Skip to content

Commit b217d70

Browse files
committed
merged branch michelsalib/translation-formatter (PR #2045)
Commits ------- 6278fcb -- add dumpers for translation component Discussion ---------- [2.1] Add dumpers for translation catalogs As seen here #1283, I push just the translation catalogs dumpers. It involved renaming and some essentially. I also included a Pot dumper/loader that have been provided here : michelsalib/BCCExtraToolsBundle#12. --------------------------------------------------------------------------- by fabpot at 2011/08/28 11:12:30 -0700 Can you add the license header? and the main phpdoc block for each class? --------------------------------------------------------------------------- by michelsalib at 2011/08/29 02:32:50 -0700 Done ! --------------------------------------------------------------------------- by fabpot at 2011/08/29 03:17:43 -0700 Last, but not the least, can you add some unit tests and squash all your commits? Thanks a lot. --------------------------------------------------------------------------- by michelsalib at 2011/08/29 03:21:43 -0700 How do I squash it, should I make a new PR ? --------------------------------------------------------------------------- by fabpot at 2011/08/29 03:25:09 -0700 No need to make a new PR, you can just force the push with `-f`. --------------------------------------------------------------------------- by fabpot at 2011/08/29 03:33:59 -0700 Also, in the tests, it would great if you can add some that do a load/dump/load, just to be sure that everything work fine. --------------------------------------------------------------------------- by michelsalib at 2011/08/29 03:39:41 -0700 What is the need of such a test, considering that the current tests that I am writing are using the same fixtures ? --------------------------------------------------------------------------- by fabpot at 2011/08/29 03:47:50 -0700 The goal is to ensure that the load/dump calls are idempotent. --------------------------------------------------------------------------- by michelsalib at 2011/08/29 03:56:12 -0700 Ye. Actually the load is using referenc files from the fixtures directory. My new tests will be using the dumper with the same files. Isn't that enough ? I try to stay DRY on this one. --------------------------------------------------------------------------- by michelsalib at 2011/08/29 05:09:52 -0700 I just add unit tests and squash the commits. I still need to be conviced about the load/dump/load unit tests. But if I did not made a point, I'll do as requested on next answer.
2 parents a746055 + 6278fcb commit b217d70

File tree

10 files changed

+282
-12
lines changed

10 files changed

+282
-12
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
<parameter key="translation.loader.php.class">Symfony\Component\Translation\Loader\PhpFileLoader</parameter>
1212
<parameter key="translation.loader.yml.class">Symfony\Component\Translation\Loader\YamlFileLoader</parameter>
1313
<parameter key="translation.loader.xliff.class">Symfony\Component\Translation\Loader\XliffFileLoader</parameter>
14+
<parameter key="translation.dumper.php.class">Symfony\Component\Translation\Dumper\PhpDumper</parameter>
15+
<parameter key="translation.dumper.xliff.class">Symfony\Component\Translation\Dumper\XliffDumper</parameter>
16+
<parameter key="translation.dumper.yml.class">Symfony\Component\Translation\Dumper\YamlDumper</parameter>
1417
</parameters>
1518

1619
<services>
@@ -42,5 +45,17 @@
4245
<service id="translation.loader.xliff" class="%translation.loader.xliff.class%">
4346
<tag name="translation.loader" alias="xliff" />
4447
</service>
48+
49+
<service id="translation.dumper.php" class="%translation.dumper.php.class%">
50+
<tag name="translation.dumper" alias="php" />
51+
</service>
52+
53+
<service id="tranlsation.dumper.xliff" class="%translation.dumper.xliff.class%">
54+
<tag name="translation.dumper" alias="xliff" />
55+
</service>
56+
57+
<service id="translation.dumper.yml" class="%translation.dumper.yml.class%">
58+
<tag name="translation.dumper" alias="yml" />
59+
</service>
4560
</services>
4661
</container>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
< 6D40 /td>
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Dumper;
13+
14+
use Symfony\Component\Translation\MessageCatalogue;
15+
16+
/**
17+
* DumperInterface is the interface implemented by all translation dumpers.
18+
*
19+
* @author Michel Salib <michelsalib@hotmail.com>
20+
*/
21+
interface DumperInterface
22+
{
23+
/**
24+
* Generates a string representation of the message catalogue
25+
*
26+
* @param MessageCatalogue $messages The message catalogue
27+
* @param string $domain The domain to dump
28+
*
29+
* @return string The string representation
30+
*/
31+
function dump(MessageCatalogue $messages, $domain = 'messages');
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Dumper;
13+
14+
use Symfony\Component\Translation\MessageCatalogue;
15+
16+
/**
17+
* PhpDumper generates a php formated string representation of a message catalogue
18+
*
19+
* @author Michel Salib <michelsalib@hotmail.com>
20+
*/
21+
class PhpDumper implements DumperInterface
22+
{
23+
/**
24+
* {@inheritDoc}
25+
*/
26+
public function dump(MessageCatalogue $messages, $domain = 'messages')
27+
{
28+
$output = "<?php\n\nreturn ".var_export($messages->all($domain), true).";\n";
29+
30+
return $output;
31+
}
32+
}
Lines changed: 52 additions & 0 deletions
+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Dumper;
13+
14+
use Symfony\Component\Translation\MessageCatalogue;
15+
16+
/**
17+
* XliffDumper generates a xliff formated string representation of a message catalogue
18+
*
19+
* @author Michel Salib <michelsalib@hotmail.com>
20
21+
class XliffDumper implements DumperInterface
22+
{
23+
/**
24+
* {@inheritDoc}
25+
*/
26+
public function dump(MessageCatalogue $messages, $domain = 'messages')
27+
{
28+
$dom = new \DOMDocument('1.0', 'utf-8');
29+
$dom->formatOutput = true;
30+
$xliff = $dom->appendChild($dom->createElement('xliff'));
31+
$xliff->setAttribute('version', '1.2');
32+
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
33+
$xliffFile = $xliff->appendChild($dom->createElement('file'));
34+
$xliffFile->setAttribute('source-language', $messages->getLocale());
35+
$xliffFile->setAttribute('datatype', 'plaintext');
36+
$xliffFile->setAttribute('original', 'file.ext');
37+
$xliffBody = $xliffFile->appendChild($dom->createElement('body'));
38+
$id = 1;
39+
foreach ($messages->all($domain) as $source => $target) {
40+
$trans = $dom->createElement('trans-unit');
41+
$trans->setAttribute('id', $id);
42+
$s = $trans->appendChild($dom->createElement('source'));
43+
$s->appendChild($dom->createTextNode($source));
44+
$t = $trans->appendChild($dom->createElement('target'));
45+
$t->appendChild($dom->createTextNode($target));
46+
$xliffBody->appendChild($trans);
47+
$id++;
48+
}
49+
50+
return $dom->saveXML();
51+
}
52+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Dumper;
13+
14+
use Symfony\Component\Translation\MessageCatalogue;
15+
use Symfony\Component\Yaml\Yaml;
16+
17+
/**
18+
* YamlDumper generates a yaml formated string representation of a message catalogue
19+
*
20+
* @author Michel Salib <michelsalib@hotmail.com>
21+
*/
22+
class YamlDumper implements DumperInterface
23+
{
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
public function dump(MessageCatalogue $messages, $domain = 'messages')
28+
{
29+
return Yaml::dump($messages->all($domain));
30+
}
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Component\Translation\Dumper;
13+
14+
use Symfony\Component\Translation\MessageCatalogue;
15+
use Symfony\Component\Translation\Dumper\PhpDumper;
16+
17+
class PhpFileDumperTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testDump()
20+
{
21+
$catalogue = new MessageCatalogue('en');
22+
$catalogue->add(array('foo' => 'bar'));
23+
24+
$dumper = new PhpDumper();
25+
$dumperString = $dumper->dump($catalogue);
26+
27+
$resource = __DIR__.'/../fixtures/resources.php';
28+
$file = new \SplFileObject($resource);
29+
$fileString = '';
30+
while(!$file->eof()) {
31+
$fileString .= $file->fgets();
32+
}
33+
34+
$this->assertEquals($fileString, $dumperString);
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Component\Translation\Dumper;
13+
14+
use Symfony\Component\Translation\MessageCatalogue;
15+
use Symfony\Component\Translation\Dumper\XliffDumper;
16+
17+
class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testDump()
20+
{
21+
$catalogue = new MessageCatalogue('en');
22+
$catalogue->add(array('foo' => 'bar'));
23+
24+
$dumper = new XliffDumper();
25+
$dumperString = $dumper->dump($catalogue);
26+
27+
$resource = __DIR__.'/../fixtures/resources.xliff';
28+
$file = new \SplFileObject($resource);
29+
$fileString = '';
30+
while(!$file->eof()) {
31+
$fileString .= $file->fgets();
32+
}
33+
34+
$this->assertEquals($fileString, $dumperString);
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Component\Translation\Dumper;
13+
14+
use Symfony\Component\Translation\MessageCatalogue;
15+
use Symfony\Component\Translation\Dumper\YamlDumper;
16+
17+
class YamlFileDumperTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testDump()
20+
{
21+
$catalogue = new MessageCatalogue('en');
22+
$catalogue->add(array('foo' => 'bar'));
23+
24+
$dumper = new YamlDumper();
25+
$dumperString = $dumper->dump($catalogue);
26+
27+
$resource = __DIR__.'/../fixtures/resources.yml';
28+
$file = new \SplFileObject($resource);
29+
$fileString = '';
30+
while(!$file->eof()) {
31+
$fileString .= $file->fgets();
32+
}
33+
34+
$this->assertEquals($fileString, $dumperString);
35+
}
36+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

3-
return array(
4-
'foo' => 'bar',
3+
return array (
4+
'foo' => 'bar',
55
);
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<?xml version="1.0"?>
2-
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
3-
<file source-language="en" datatype="plaintext" original="file.ext">
4-
<body>
5-
<trans-unit id="1">
6-
<source>foo</source>
7-
<target>bar</target>
8-
</trans-unit>
9-
</body>
10-
</file>
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
3+
<file source-language="en" datatype="plaintext" original="file.ext">
4+
<body>
5+
<trans-unit id="1">
6+
<source>foo</source>
7+
<target>bar</target>
8+
</trans-unit>
9+
</body>
10+
</file>
1111
</xliff>

0 commit comments

Comments
 (0)
0