YamlFileLoader.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. * @api
  19. */
  20. class YamlFileLoader extends ArrayLoader implements LoaderInterface
  21. {
  22. /**
  23. * {@inheritdoc}
  24. *
  25. * @api
  26. */
  27. public function load($resource, $locale, $domain = 'messages')
  28. {
  29. $messages = Yaml::parse($resource);
  30. // empty file
  31. if (null === $messages) {
  32. $messages = array();
  33. }
  34. // not an array
  35. if (!is_array($messages)) {
  36. throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $resource));
  37. }
  38. $catalogue = parent::load($messages, $locale, $domain);
  39. $catalogue->addResource(new FileResource($resource));
  40. return $catalogue;
  41. }
  42. }