Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Translation\Loader; Chris@0: Chris@0: use Symfony\Component\Translation\Exception\NotFoundResourceException; Chris@0: Chris@0: /** Chris@0: * CsvFileLoader loads translations from CSV files. Chris@0: * Chris@0: * @author Saša Stamenković Chris@0: */ Chris@0: class CsvFileLoader extends FileLoader Chris@0: { Chris@0: private $delimiter = ';'; Chris@0: private $enclosure = '"'; Chris@0: private $escape = '\\'; Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function loadResource($resource) Chris@0: { Chris@17: $messages = []; Chris@0: Chris@0: try { Chris@0: $file = new \SplFileObject($resource, 'rb'); Chris@0: } catch (\RuntimeException $e) { Chris@0: throw new NotFoundResourceException(sprintf('Error opening file "%s".', $resource), 0, $e); Chris@0: } Chris@0: Chris@0: $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY); Chris@0: $file->setCsvControl($this->delimiter, $this->enclosure, $this->escape); Chris@0: Chris@0: foreach ($file as $data) { Chris@17: if ('#' !== substr($data[0], 0, 1) && isset($data[1]) && 2 === \count($data)) { Chris@0: $messages[$data[0]] = $data[1]; Chris@0: } Chris@0: } Chris@0: Chris@0: return $messages; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the delimiter, enclosure, and escape character for CSV. Chris@0: * Chris@14: * @param string $delimiter Delimiter character Chris@14: * @param string $enclosure Enclosure character Chris@14: * @param string $escape Escape character Chris@0: */ Chris@0: public function setCsvControl($delimiter = ';', $enclosure = '"', $escape = '\\') Chris@0: { Chris@0: $this->delimiter = $delimiter; Chris@0: $this->enclosure = $enclosure; Chris@0: $this->escape = $escape; Chris@0: } Chris@0: }