Permalink
Browse files

[Translation] support CsvDumper

[Translation] support CsvDumper
[Translation] support CsvDumper
[Translation] support CsvDumper
[Translation] support CsvDumper
  • Loading branch information...
1 parent 55f5295 commit dea46c721f7fa74a7992ad596361b2fde2f1fc58 @stealth35 stealth35 committed Sep 5, 2011
@@ -0,0 +1,55 @@
+<?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;
+
+/**
+ * CsvDumper generates a csv formated string representation of a message catalogue
+ *
+ * @author Stealth35
+ */
+class CsvDumper implements DumperInterface
+{
+ private $delimiter = ';';
+ private $enclosure = '"';
+
+ /**
+ * {@inheritDoc}
+ */
+ public function dump(MessageCatalogue $messages, $domain = 'messages')
+ {
+ $handle = fopen('php://memory', 'rb+');
+
+ foreach ($messages->all($domain) as $source => $target) {
+ fputcsv($handle, array($source, $target), $this->delimiter, $this->enclosure);
+ }
+
+ rewind($handle);
+ $output = stream_get_contents($handle);
+ fclose($handle);
+
+ return $output;
+ }
+
+ /**
+ * Sets the delimiter and escape character for CSV.
+ *
+ * @param string $delimiter delimiter character
+ * @param string $enclosure enclosure character
+ */
+ public function setCsvControl($delimiter = ';', $enclosure = '"')
+ {
+ $this->delimiter = $delimiter;
+ $this->enclosure = $enclosure;
+ }
+}
@@ -0,0 +1,30 @@
+<?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\CsvDumper;
+
+class CsvFileDumperTest extends \PHPUnit_Framework_TestCase
+{
+ public function testDump()
+ {
+ $catalogue = new MessageCatalogue('en');
+ $catalogue->add(array('foo' => 'bar', 'bar' => 'foo
+foo', 'foo;foo' => 'bar'));
+
+ $dumper = new CsvDumper();
+ $dumperString = $dumper->dump($catalogue);
+
+ $this->assertEquals(file_get_contents(__DIR__.'/../fixtures/valid.csv'), $dumperString);
+ }
+}
@@ -0,0 +1,4 @@
+foo;bar
+bar;"foo
+foo"
+"foo;foo";bar

0 comments on commit dea46c7

Please sign in to comment.