4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Loader
;
14 use Symfony\Component\Translation\Exception\InvalidResourceException
;
15 use Symfony\Component\Translation\Exception\NotFoundResourceException
;
16 use Symfony\Component\Config\
Resource\FileResource
;
19 * CsvFileLoader loads translations from CSV files.
21 * @author Saša Stamenković <umpirsky@gmail.com>
25 class CsvFileLoader
extends ArrayLoader
implements LoaderInterface
27 private $delimiter = ';';
28 private $enclosure = '"';
29 private $escape = '\\';
36 public function load($resource, $locale, $domain = 'messages')
38 if (!stream_is_local($resource)) {
39 throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
42 if (!file_exists($resource)) {
43 throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
49 $file = new \
SplFileObject($resource, 'rb');
50 } catch (\RuntimeException
$e) {
51 throw new NotFoundResourceException(sprintf('Error opening file "%s".', $resource), 0, $e);
54 $file->setFlags(\SplFileObject
::READ_CSV
| \SplFileObject
::SKIP_EMPTY
);
55 $file->setCsvControl($this->delimiter
, $this->enclosure
, $this->escape
);
57 foreach ($file as $data) {
58 if (substr($data[0], 0, 1) === '#') {
62 if (!isset($data[1])) {
66 if (count($data) == 2) {
67 $messages[$data[0]] = $data[1];
73 $catalogue = parent
::load($messages, $locale, $domain);
74 $catalogue->addResource(new FileResource($resource));
80 * Sets the delimiter, enclosure, and escape character for CSV.
82 * @param string $delimiter delimiter character
83 * @param string $enclosure enclosure character
84 * @param string $escape escape character
86 public function setCsvControl($delimiter = ';', $enclosure = '"', $escape = '\\')
88 $this->delimiter
= $delimiter;
89 $this->enclosure
= $enclosure;
90 $this->escape
= $escape;