YamlFileDumper.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\Dumper;
  11. use Symfony\Component\Translation\Exception\LogicException;
  12. use Symfony\Component\Translation\MessageCatalogue;
  13. use Symfony\Component\Translation\Util\ArrayConverter;
  14. use Symfony\Component\Yaml\Yaml;
  15. /**
  16. * YamlFileDumper generates yaml files from a message catalogue.
  17. *
  18. * @author Michel Salib <michelsalib@hotmail.com>
  19. */
  20. class YamlFileDumper extends FileDumper
  21. {
  22. private string $extension;
  23. public function __construct(string $extension = 'yml')
  24. {
  25. $this->extension = $extension;
  26. }
  27. public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string
  28. {
  29. if (!class_exists(Yaml::class)) {
  30. throw new LogicException('Dumping translations in the YAML format requires the Symfony Yaml component.');
  31. }
  32. $data = $messages->all($domain);
  33. if (isset($options['as_tree']) && $options['as_tree']) {
  34. $data = ArrayConverter::expandToTree($data);
  35. }
  36. if (isset($options['inline']) && ($inline = (int) $options['inline']) > 0) {
  37. return Yaml::dump($data, $inline);
  38. }
  39. return Yaml::dump($data);
  40. }
  41. protected function getExtension(): string
  42. {
  43. return $this->extension;
  44. }
  45. }