YamlFileLoader.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Loader;
  11. use Symfony\Component\Config\Resource\FileResource;
  12. use Symfony\Component\Yaml\Yaml;
  13. /**
  14. * YamlFileLoader loads translations from Yaml files.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class YamlFileLoader extends ArrayLoader implements LoaderInterface
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function load($resource, $locale, $domain = 'messages')
  24. {
  25. $messages = Yaml::load($resource);
  26. // empty file
  27. if (null === $messages) {
  28. $messages = array();
  29. }
  30. // not an array
  31. if (!is_array($messages)) {
  32. throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $resource));
  33. }
  34. $catalogue = parent::load($messages, $locale, $domain);
  35. $catalogue->addResource(new FileResource($resource));
  36. return $catalogue;
  37. }
  38. }