YamlFileDumper.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. public function __construct(
  23. private string $extension = 'yml',
  24. ) {
  25. }
  26. public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string
  27. {
  28. if (!class_exists(Yaml::class)) {
  29. throw new LogicException('Dumping translations in the YAML format requires the Symfony Yaml component.');
  30. }
  31. $data = $messages->all($domain);
  32. if (isset($options['as_tree']) && $options['as_tree']) {
  33. $data = ArrayConverter::expandToTree($data);
  34. }
  35. if (isset($options['inline']) && ($inline = (int) $options['inline']) > 0) {
  36. return Yaml::dump($data, $inline);
  37. }
  38. return Yaml::dump($data);
  39. }
  40. protected function getExtension(): string
  41. {
  42. return $this->extension;
  43. }
  44. }