Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Added CsvFileLoader to support csv translation resources. #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Symfony/Component/Translation/Loader/CsvFileLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Symfony\Component\Translation\Loader;

use Symfony\Component\Translation\Resource\FileResource;

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <[email protected]>
*
* 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ć <[email protected]>
*/
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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());
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"foo"; "bar"
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down