diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml index d3b7d74c8c2f..96f6e47d3de1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml @@ -11,6 +11,9 @@ Symfony\Component\Translation\Loader\PhpFileLoader Symfony\Component\Translation\Loader\YamlFileLoader Symfony\Component\Translation\Loader\XliffFileLoader + Symfony\Component\Translation\Dumper\PhpDumper + Symfony\Component\Translation\Dumper\XliffDumper + Symfony\Component\Translation\Dumper\YamlDumper @@ -42,5 +45,17 @@ + + + + + + + + + + + + diff --git a/src/Symfony/Component/Translation/Dumper/DumperInterface.php b/src/Symfony/Component/Translation/Dumper/DumperInterface.php new file mode 100644 index 000000000000..15828a9d8c6b --- /dev/null +++ b/src/Symfony/Component/Translation/Dumper/DumperInterface.php @@ -0,0 +1,32 @@ + + * + * 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 + */ +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'); +} diff --git a/src/Symfony/Component/Translation/Dumper/PhpDumper.php b/src/Symfony/Component/Translation/Dumper/PhpDumper.php new file mode 100644 index 000000000000..4b226efb814c --- /dev/null +++ b/src/Symfony/Component/Translation/Dumper/PhpDumper.php @@ -0,0 +1,32 @@ + + * + * 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 + */ +class PhpDumper implements DumperInterface +{ + /** + * {@inheritDoc} + */ + public function dump(MessageCatalogue $messages, $domain = 'messages') + { + $output = "all($domain), true).";\n"; + + return $output; + } +} diff --git a/src/Symfony/Component/Translation/Dumper/XliffDumper.php b/src/Symfony/Component/Translation/Dumper/XliffDumper.php new file mode 100644 index 000000000000..d4242ad91363 --- /dev/null +++ b/src/Symfony/Component/Translation/Dumper/XliffDumper.php @@ -0,0 +1,52 @@ + + * + * 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 + */ +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(); + } +} diff --git a/src/Symfony/Component/Translation/Dumper/YamlDumper.php b/src/Symfony/Component/Translation/Dumper/YamlDumper.php new file mode 100644 index 000000000000..4494a8ee9715 --- /dev/null +++ b/src/Symfony/Component/Translation/Dumper/YamlDumper.php @@ -0,0 +1,31 @@ + + * + * 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 + */ +class YamlDumper implements DumperInterface +{ + /** + * {@inheritDoc} + */ + public function dump(MessageCatalogue $messages, $domain = 'messages') + { + return Yaml::dump($messages->all($domain)); + } +} diff --git a/tests/Symfony/Tests/Component/Translation/Dumper/PhpFileDumperTest.php b/tests/Symfony/Tests/Component/Translation/Dumper/PhpFileDumperTest.php new file mode 100644 index 000000000000..6467d375ff8f --- /dev/null +++ b/tests/Symfony/Tests/Component/Translation/Dumper/PhpFileDumperTest.php @@ -0,0 +1,36 @@ + + * + * 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); + } +} diff --git a/tests/Symfony/Tests/Component/Translation/Dumper/XliffFileDumperTest.php b/tests/Symfony/Tests/Component/Translation/Dumper/XliffFileDumperTest.php new file mode 100644 index 000000000000..7dff2b19a418 --- /dev/null +++ b/tests/Symfony/Tests/Component/Translation/Dumper/XliffFileDumperTest.php @@ -0,0 +1,36 @@ + + * + * 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); + } +} diff --git a/tests/Symfony/Tests/Component/Translation/Dumper/YamlFileDumperTest.php b/tests/Symfony/Tests/Component/Translation/Dumper/YamlFileDumperTest.php new file mode 100644 index 000000000000..50c9ccdf3de0 --- /dev/null +++ b/tests/Symfony/Tests/Component/Translation/Dumper/YamlFileDumperTest.php @@ -0,0 +1,36 @@ + + * + * 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); + } +} diff --git a/tests/Symfony/Tests/Component/Translation/fixtures/resources.php b/tests/Symfony/Tests/Component/Translation/fixtures/resources.php index b9a9e0187af9..c2913985391c 100644 --- a/tests/Symfony/Tests/Component/Translation/fixtures/resources.php +++ b/tests/Symfony/Tests/Component/Translation/fixtures/resources.php @@ -1,5 +1,5 @@ 'bar', +return array ( + 'foo' => 'bar', ); diff --git a/tests/Symfony/Tests/Component/Translation/fixtures/resources.xliff b/tests/Symfony/Tests/Component/Translation/fixtures/resources.xliff index 0c74c41560f4..699f10dcda3d 100644 --- a/tests/Symfony/Tests/Component/Translation/fixtures/resources.xliff +++ b/tests/Symfony/Tests/Component/Translation/fixtures/resources.xliff @@ -1,11 +1,11 @@ - - - - - - foo - bar - - - + + + + + + foo + bar + + +