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;
|
|
|
|
|
2012-12-12 19:43:13 +00:00
|
|
|
use Symfony\Component\Translation\Exception\NotFoundResourceException;
|
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>
|
|
|
|
*/
|
2015-04-15 01:53:15 +01:00
|
|
|
class CsvFileLoader extends FileLoader
|
2011-01-08 14:09:18 +00:00
|
|
|
{
|
2011-07-07 11:27:49 +01:00
|
|
|
private $delimiter = ';';
|
|
|
|
private $enclosure = '"';
|
2014-10-22 19:27:13 +01:00
|
|
|
private $escape = '\\';
|
2011-07-07 10:46:07 +01:00
|
|
|
|
2011-01-08 14:09:18 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-04-15 01:53:15 +01:00
|
|
|
protected function loadResource($resource)
|
2011-01-08 14:09:18 +00:00
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
$messages = [];
|
2012-12-12 19:43:13 +00:00
|
|
|
|
2011-03-25 10:51:32 +00:00
|
|
|
try {
|
2011-03-25 12:38:09 +00:00
|
|
|
$file = new \SplFileObject($resource, 'rb');
|
2011-11-11 11:23:38 +00:00
|
|
|
} catch (\RuntimeException $e) {
|
2012-12-12 19:43:13 +00:00
|
|
|
throw new NotFoundResourceException(sprintf('Error opening file "%s".', $resource), 0, $e);
|
2011-01-08 14:09:18 +00:00
|
|
|
}
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-03-25 10:51:32 +00:00
|
|
|
$file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY);
|
2011-07-07 10:46:07 +01:00
|
|
|
$file->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
|
2011-01-08 14:09:18 +00:00
|
|
|
|
2011-11-11 11:23:38 +00:00
|
|
|
foreach ($file as $data) {
|
2019-08-06 10:10:43 +01:00
|
|
|
if (false === $data) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-07-05 12:24:53 +01:00
|
|
|
if ('#' !== substr($data[0], 0, 1) && isset($data[1]) && 2 === \count($data)) {
|
2011-01-08 14:09:18 +00:00
|
|
|
$messages[$data[0]] = $data[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 01:53:15 +01:00
|
|
|
return $messages;
|
2011-01-08 14:09:18 +00:00
|
|
|
}
|
2011-07-07 10:46:07 +01:00
|
|
|
|
|
|
|
/**
|
2011-07-07 11:27:49 +01:00
|
|
|
* Sets the delimiter, enclosure, and escape character for CSV.
|
2011-07-07 10:46:07 +01:00
|
|
|
*/
|
2019-06-27 12:49:03 +01:00
|
|
|
public function setCsvControl(string $delimiter = ';', string $enclosure = '"', string $escape = '\\')
|
2011-07-07 10:46:07 +01:00
|
|
|
{
|
|
|
|
$this->delimiter = $delimiter;
|
|
|
|
$this->enclosure = $enclosure;
|
2014-10-22 19:27:13 +01:00
|
|
|
$this->escape = $escape;
|
2011-07-07 10:46:07 +01:00
|
|
|
}
|
2011-01-08 14:09:18 +00:00
|
|
|
}
|