diff --git a/src/Symfony/Component/Translation/Loader/CsvFileLoader.php b/src/Symfony/Component/Translation/Loader/CsvFileLoader.php new file mode 100644 index 000000000000..cc281dfc38ee --- /dev/null +++ b/src/Symfony/Component/Translation/Loader/CsvFileLoader.php @@ -0,0 +1,55 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * CsvFileLoader loads translations from CSV files. + * + * @author Saša Stamenković + */ +class CsvFileLoader extends ArrayLoader implements LoaderInterface +{ + /** + * {@inheritdoc} + */ + public function load($resource, $locale, $domain = 'messages') + { + $messages = array(); + $file = @fopen($resource, 'rb'); + if (!$file) { + throw new \InvalidArgumentException(sprintf('Error opening file "%s".', $resource)); + } + + while(($data = fgetcsv($file, 0, ';')) !== false) { + if (substr($data[0], 0, 1) === '#') { + continue; + } + + if (!isset($data[1])) { + continue; + } + + if (count($data) == 2) { + $messages[$data[0]] = $data[1]; + } else { + continue; + } + } + + $catalogue = parent::load($messages, $locale, $domain); + $catalogue->addResource(new FileResource($resource)); + + return $catalogue; + } +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/File/FileTest.php b/tests/Symfony/Tests/Component/HttpFoundation/File/FileTest.php index c6b953d032eb..224efa89d928 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/File/FileTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/File/FileTest.php @@ -96,10 +96,10 @@ public function testMove() public function testRename() { $path = __DIR__.'/Fixtures/test.copy.gif'; - $targetPath = __DIR__.'/Fixtures/test.target.gif'; + $targetPath = realpath(__DIR__.'/Fixtures') . DIRECTORY_SEPARATOR . 'test.target.gif'; @unlink($path); @unlink($targetPath); - copy(__DIR__.'/Fixtures/test.gif', $path); + copy(realpath(__DIR__.'/Fixtures/test.gif'), $path); $file = new File($path); $file->rename('test.target.gif'); diff --git a/tests/Symfony/Tests/Component/Translation/Loader/CsvFileLoaderTest.php b/tests/Symfony/Tests/Component/Translation/Loader/CsvFileLoaderTest.php new file mode 100644 index 000000000000..fd7c6aa1a948 --- /dev/null +++ b/tests/Symfony/Tests/Component/Translation/Loader/CsvFileLoaderTest.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Component\Translation\Loader; + +use Symfony\Component\Translation\Loader\CsvFileLoader; +use Symfony\Component\Translation\Resource\FileResource; + +class CsvFileLoaderTest extends \PHPUnit_Framework_TestCase +{ + public function testLoad() + { + $loader = new CsvFileLoader(); + $resource = __DIR__.'/../fixtures/resources.csv'; + $catalogue = $loader->load($resource, 'en', 'domain1'); + + $this->assertEquals(array('foo' => 'bar'), $catalogue->all('domain1')); + $this->assertEquals('en', $catalogue->getLocale()); + $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); + } + + public function testLoadDoesNothingIfEmpty() + { + $loader = new CsvFileLoader(); + $resource = __DIR__.'/../fixtures/empty.csv'; + $catalogue = $loader->load($resource, 'en', 'domain1'); + + $this->assertEquals(array(), $catalogue->all('domain1')); + $this->assertEquals('en', $catalogue->getLocale()); + $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); + } +} diff --git a/tests/Symfony/Tests/Component/Translation/fixtures/empty.csv b/tests/Symfony/Tests/Component/Translation/fixtures/empty.csv new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tests/Symfony/Tests/Component/Translation/fixtures/resources.csv b/tests/Symfony/Tests/Component/Translation/fixtures/resources.csv new file mode 100644 index 000000000000..b6467f0653de --- /dev/null +++ b/tests/Symfony/Tests/Component/Translation/fixtures/resources.csv @@ -0,0 +1 @@ +"foo"; "bar" \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php index fb080edd1bea..2302d13bdedb 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +date_default_timezone_set('Europe/Paris'); + if (file_exists($file = __DIR__.'/../autoload.php')) { require_once $file; } elseif (file_exists($file = __DIR__.'/../autoload.php.dist')) {