4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Writer;
14 use Symfony\Component\Translation\MessageCatalogue;
15 use Symfony\Component\Translation\Dumper\DumperInterface;
16 use Symfony\Component\Translation\Exception\InvalidArgumentException;
17 use Symfony\Component\Translation\Exception\RuntimeException;
20 * TranslationWriter writes translation messages.
22 * @author Michel Salib <michelsalib@hotmail.com>
24 class TranslationWriter implements TranslationWriterInterface
26 private $dumpers = array();
29 * Adds a dumper to the writer.
31 * @param string $format The format of the dumper
32 * @param DumperInterface $dumper The dumper
34 public function addDumper($format, DumperInterface $dumper)
36 $this->dumpers[$format] = $dumper;
40 * Disables dumper backup.
42 public function disableBackup()
44 foreach ($this->dumpers as $dumper) {
45 if (method_exists($dumper, 'setBackup')) {
46 $dumper->setBackup(false);
52 * Obtains the list of supported formats.
56 public function getFormats()
58 return array_keys($this->dumpers);
62 * Writes translation from the catalogue according to the selected format.
64 * @param MessageCatalogue $catalogue The message catalogue to write
65 * @param string $format The format to use to dump the messages
66 * @param array $options Options that are passed to the dumper
68 * @throws InvalidArgumentException
70 public function write(MessageCatalogue $catalogue, $format, $options = array())
72 if (!isset($this->dumpers[$format])) {
73 throw new InvalidArgumentException(sprintf('There is no dumper associated with format "%s".', $format));
76 // get the right dumper
77 $dumper = $this->dumpers[$format];
79 if (isset($options['path']) && !is_dir($options['path']) && !@mkdir($options['path'], 0777, true) && !is_dir($options['path'])) {
80 throw new RuntimeException(sprintf('Translation Writer was not able to create directory "%s"', $options['path']));
84 $dumper->dump($catalogue, $options);
88 * Writes translation from the catalogue according to the selected format.
90 * @param MessageCatalogue $catalogue The message catalogue to write
91 * @param string $format The format to use to dump the messages
92 * @param array $options Options that are passed to the dumper
94 * @throws InvalidArgumentException
96 * @deprecated since 3.4 will be removed in 4.0. Use write instead.
98 public function writeTranslations(MessageCatalogue $catalogue, $format, $options = array())
100 @trigger_error(sprintf('Method %s() is deprecated since Symfony 3.4 and will be removed in 4.0. Use write() instead.', __METHOD__), E_USER_DEPRECATED);
101 $this->write($catalogue, $format, $options);