Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-- add dumpers for translation component
-- 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
Showing
10 changed files
with
282 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Symfony/Component/Translation/Dumper/DumperInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
tests/Symfony/Tests/Component/Translation/Dumper/PhpFileDumperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
tests/Symfony/Tests/Component/Translation/Dumper/XliffFileDumperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
tests/Symfony/Tests/Component/Translation/Dumper/YamlFileDumperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
tests/Symfony/Tests/Component/Translation/fixtures/resources.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?php | ||
|
||
return array( | ||
'foo' => 'bar', | ||
return array ( | ||
'foo' => 'bar', | ||
); |
20 changes: 10 additions & 10 deletions
20
tests/Symfony/Tests/Component/Translation/fixtures/resources.xliff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
There is a typo here. (
tranlsation
instead oftranslation
)