| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\Loader; |
| 13 |
|
| 14 |
use Symfony\Component\Translation\Exception\NotFoundResourceException; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
m> |
| 20 |
|
| 21 |
class CsvFileLoader extends FileLoader |
| 22 |
{ |
| 23 |
private string $delimiter = ';'; |
| 24 |
private string $enclosure = '"'; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
private string $escape = ''; |
| 29 |
|
| 30 |
protected function loadResource(string $resource): array |
| 31 |
{ |
| 32 |
$messages = []; |
| 33 |
|
| 34 |
try { |
| 35 |
$file = new \SplFileObject($resource, 'rb'); |
| 36 |
} catch (\RuntimeException $e) { |
| 37 |
throw new NotFoundResourceException(\sprintf('Error opening file "%s".', $resource), 0, $e); |
| 38 |
} |
| 39 |
|
| 40 |
$file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE); |
| 41 |
$file->setCsvControl($this->delimiter, $this->enclosure, $this->escape); |
| 42 |
|
| 43 |
foreach ($file as $data) { |
| 44 |
if (false === $data) { |
| 45 |
continue; |
| 46 |
} |
| 47 |
|
| 48 |
if (!str_starts_with($data[0], '#') && isset($data[1]) && 2 === \count($data)) { |
| 49 |
$messages[$data[0]] = $data[1]; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return $messages; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public function setCsvControl(string $delimiter = ';', string $enclosure = '"', string $escape = ''): void |
| 60 |
{ |
| 61 |
$this->delimiter = $delimiter; |
| 62 |
$this->enclosure = $enclosure; |
| 63 |
if ('' !== $escape) { |
| 64 |
trigger_deprecation('symfony/translation', '7.2', 'The "escape" parameter of the "%s" method is deprecated. It will be removed in 8.0.', __METHOD__); |
| 65 |
} |
| 66 |
|
| 67 |
$this->escape = $escape; |
| 68 |
} |
| 69 |
} |