From fe028c91fecbe3c1ae624bd4c3189b5ff5baec3e Mon Sep 17 00:00:00 2001 From: umpirsky Date: Fri, 7 Jan 2011 14:53:41 +0100 Subject: [PATCH 1/6] Fixed FileTest::testRename to work for windows file system. --- .../Symfony/Tests/Component/HttpFoundation/File/FileTest.php | 4 ++-- tests/bootstrap.php | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) 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/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')) { From 2e38dade3f160f2fc530b4655a1a37a59e927d04 Mon Sep 17 00:00:00 2001 From: umpirsky Date: Fri, 7 Jan 2011 14:56:37 +0100 Subject: [PATCH 2/6] Revert "Fixed FileTest::testRename to work for windows file system." This reverts commit fe028c91fecbe3c1ae624bd4c3189b5ff5baec3e. --- .../Symfony/Tests/Component/HttpFoundation/File/FileTest.php | 4 ++-- tests/bootstrap.php | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/Symfony/Tests/Component/HttpFoundation/File/FileTest.php b/tests/Symfony/Tests/Component/HttpFoundation/File/FileTest.php index 224efa89d928..c6b953d032eb 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 = realpath(__DIR__.'/Fixtures') . DIRECTORY_SEPARATOR . 'test.target.gif'; + $targetPath = __DIR__.'/Fixtures/test.target.gif'; @unlink($path); @unlink($targetPath); - copy(realpath(__DIR__.'/Fixtures/test.gif'), $path); + copy(__DIR__.'/Fixtures/test.gif', $path); $file = new File($path); $file->rename('test.target.gif'); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 2302d13bdedb..fb080edd1bea 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -9,8 +9,6 @@ * 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')) { From 4d222e2d8c39a684047839dab72dc0e809908f49 Mon Sep 17 00:00:00 2001 From: umpirsky Date: Fri, 7 Jan 2011 15:02:22 +0100 Subject: [PATCH 3/6] Fixed FileTest::testRename to work for windows file system. --- .../Symfony/Tests/Component/HttpFoundation/File/FileTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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'); From f598182afc54972c677952d8b32a75cdd32a1a6c Mon Sep 17 00:00:00 2001 From: umpirsky Date: Fri, 7 Jan 2011 15:09:46 +0100 Subject: [PATCH 4/6] Set default timezone on bootstrap. --- tests/bootstrap.php | 2 ++ 1 file changed, 2 insertions(+) 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')) { From be7bf763c91351d97749651803977aa30a81ba97 Mon Sep 17 00:00:00 2001 From: umpirsky Date: Sat, 8 Jan 2011 15:09:18 +0100 Subject: [PATCH 5/6] Added CsvFileLoader to support csv translation resources. --- .../Translation/Loader/CsvFileLoader.php | 55 +++++++++++++++++++ .../Translation/Loader/CsvFileLoaderTest.php | 40 ++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/Symfony/Component/Translation/Loader/CsvFileLoader.php create mode 100644 tests/Symfony/Tests/Component/Translation/Loader/CsvFileLoaderTest.php 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/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()); + } +} From 7dad13c944acaf782d6c3aced47dcc9edfaab233 Mon Sep 17 00:00:00 2001 From: umpirsky Date: Sat, 8 Jan 2011 15:12:22 +0100 Subject: [PATCH 6/6] Added CsvFileLoader to support csv translation resources. --- tests/Symfony/Tests/Component/Translation/fixtures/empty.csv | 0 tests/Symfony/Tests/Component/Translation/fixtures/resources.csv | 1 + 2 files changed, 1 insertion(+) create mode 100644 tests/Symfony/Tests/Component/Translation/fixtures/empty.csv create mode 100644 tests/Symfony/Tests/Component/Translation/fixtures/resources.csv 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