blob: 0b21e8c830c06e25509468a45b6e8cd6003fcb3e [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Translation\Dumper;
13
14use Symfony\Component\Translation\Exception\LogicException;
15use Symfony\Component\Translation\MessageCatalogue;
16use Symfony\Component\Translation\Util\ArrayConverter;
17use Symfony\Component\Yaml\Yaml;
18
19/**
20 * YamlFileDumper generates yaml files from a message catalogue.
21 *
22 * @author Michel Salib <michelsalib@hotmail.com>
23 */
24class YamlFileDumper extends FileDumper
25{
26 private $extension;
27
28 public function __construct(string $extension = 'yml')
29 {
30 $this->extension = $extension;
31 }
32
33 /**
34 * {@inheritdoc}
35 */
36 public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = [])
37 {
38 if (!class_exists(Yaml::class)) {
39 throw new LogicException('Dumping translations in the YAML format requires the Symfony Yaml component.');
40 }
41
42 $data = $messages->all($domain);
43
44 if (isset($options['as_tree']) && $options['as_tree']) {
45 $data = ArrayConverter::expandToTree($data);
46 }
47
48 if (isset($options['inline']) && ($inline = (int) $options['inline']) > 0) {
49 return Yaml::dump($data, $inline);
50 }
51
52 return Yaml::dump($data);
53 }
54
55 /**
56 * {@inheritdoc}
57 */
58 protected function getExtension()
59 {
60 return $this->extension;
61 }
62}