2011-01-08 14:09:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2011-01-08 14:09:18 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-08 14:09:18 +00:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2011-01-08 14:09:18 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Translation\Loader;
|
|
|
|
|
2011-02-10 15:15:51 +00:00
|
|
|
use Symfony\Component\Config\Resource\FileResource;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2011-01-08 14:09:18 +00:00
|
|
|
/**
|
|
|
|
* CsvFileLoader loads translations from CSV files.
|
|
|
|
*
|
|
|
|
* @author Saša Stamenković <umpirsky@gmail.com>
|
2011-03-24 08:07:52 +00:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-08 14:09:18 +00:00
|
|
|
*/
|
|
|
|
class CsvFileLoader extends ArrayLoader implements LoaderInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2011-03-24 08:07:52 +00:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-08 14:09:18 +00:00
|
|
|
*/
|
|
|
|
public function load($resource, $locale, $domain = 'messages')
|
|
|
|
{
|
|
|
|
$messages = array();
|
2011-03-25 10:51:32 +00:00
|
|
|
|
|
|
|
try {
|
2011-03-25 12:38:09 +00:00
|
|
|
$file = new \SplFileObject($resource, 'rb');
|
2011-03-25 10:51:32 +00:00
|
|
|
} catch(\RuntimeException $e) {
|
2011-03-25 12:38:09 +00:00
|
|
|
throw new \InvalidArgumentException(sprintf('Error opening file "%s".', $resource));
|
2011-01-08 14:09:18 +00:00
|
|
|
}
|
2011-03-25 10:51:32 +00:00
|
|
|
|
|
|
|
$file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY);
|
|
|
|
$file->setCsvControl(';');
|
2011-01-08 14:09:18 +00:00
|
|
|
|
2011-03-25 10:51:32 +00:00
|
|
|
foreach($file as $data) {
|
2011-01-08 14:09:18 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|