CsvFileLoader.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Symfony\Component\Translation\Loader;
  3. use Symfony\Component\Translation\Resource\FileResource;
  4. /*
  5. * This file is part of the Symfony framework.
  6. *
  7. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. /**
  13. * CsvFileLoader loads translations from CSV files.
  14. *
  15. * @author Saša Stamenković <umpirsky@gmail.com>
  16. */
  17. class CsvFileLoader extends ArrayLoader implements LoaderInterface
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function load($resource, $locale, $domain = 'messages')
  23. {
  24. $messages = array();
  25. $file = @fopen($resource, 'rb');
  26. if (!$file) {
  27. throw new \InvalidArgumentException(sprintf('Error opening file "%s".', $resource));
  28. }
  29. while(($data = fgetcsv($file, 0, ';')) !== false) {
  30. if (substr($data[0], 0, 1) === '#') {
  31. continue;
  32. }
  33. if (!isset($data[1])) {
  34. continue;
  35. }
  36. if (count($data) == 2) {
  37. $messages[$data[0]] = $data[1];
  38. } else {
  39. continue;
  40. }
  41. }
  42. $catalogue = parent::load($messages, $locale, $domain);
  43. $catalogue->addResource(new FileResource($resource));
  44. return $catalogue;
  45. }
  46. }