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