ArrayLoader.php
1 month ago
CsvFileLoader.php
1 month ago
FileLoader.php
1 month ago
IcuDatFileLoader.php
1 month ago
IcuResFileLoader.php
1 month ago
IniFileLoader.php
1 month ago
JsonFileLoader.php
1 month ago
LoaderInterface.php
1 month ago
MoFileLoader.php
1 month ago
PhpFileLoader.php
1 month ago
PoFileLoader.php
1 month ago
QtFileLoader.php
1 month ago
XliffFileLoader.php
1 month ago
YamlFileLoader.php
1 month ago
CsvFileLoader.php
58 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace IAWPSCOPED\Symfony\Component\Translation\Loader; |
| 12 | |
| 13 | use IAWPSCOPED\Symfony\Component\Translation\Exception\NotFoundResourceException; |
| 14 | /** |
| 15 | * CsvFileLoader loads translations from CSV files. |
| 16 | * |
| 17 | * @author Saša Stamenković <umpirsky@gmail.com> |
| 18 | * @internal |
| 19 | */ |
| 20 | class CsvFileLoader extends FileLoader |
| 21 | { |
| 22 | private string $delimiter = ';'; |
| 23 | private string $enclosure = '"'; |
| 24 | private string $escape = '\\'; |
| 25 | /** |
| 26 | * {@inheritdoc} |
| 27 | */ |
| 28 | protected function loadResource(string $resource) : array |
| 29 | { |
| 30 | $messages = []; |
| 31 | try { |
| 32 | $file = new \SplFileObject($resource, 'rb'); |
| 33 | } catch (\RuntimeException $e) { |
| 34 | throw new NotFoundResourceException(\sprintf('Error opening file "%s".', $resource), 0, $e); |
| 35 | } |
| 36 | $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY); |
| 37 | $file->setCsvControl($this->delimiter, $this->enclosure, $this->escape); |
| 38 | foreach ($file as $data) { |
| 39 | if (\false === $data) { |
| 40 | continue; |
| 41 | } |
| 42 | if ('#' !== \substr($data[0], 0, 1) && isset($data[1]) && 2 === \count($data)) { |
| 43 | $messages[$data[0]] = $data[1]; |
| 44 | } |
| 45 | } |
| 46 | return $messages; |
| 47 | } |
| 48 | /** |
| 49 | * Sets the delimiter, enclosure, and escape character for CSV. |
| 50 | */ |
| 51 | public function setCsvControl(string $delimiter = ';', string $enclosure = '"', string $escape = '\\') |
| 52 | { |
| 53 | $this->delimiter = $delimiter; |
| 54 | $this->enclosure = $enclosure; |
| 55 | $this->escape = $escape; |
| 56 | } |
| 57 | } |
| 58 |