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
27 * Dumpers used for export.
31 private $dumpers = array();
34 * Adds a dumper to the writer.
36 * @param string $format The format of the dumper
37 * @param DumperInterface $dumper The dumper
39 public function addDumper($format, DumperInterface $dumper)
41 $this->dumpers[$format] = $dumper;
45 * Disables dumper backup.
47 public function disableBackup()
49 foreach ($this->dumpers as $dumper) {
50 if (method_exists($dumper, 'setBackup')) {
51 $dumper->setBackup(false);
57 * Obtains the list of supported formats.
61 public function getFormats()
63 return array_keys($this->dumpers);
67 * Writes translation from the catalogue according to the selected format.
69 * @param MessageCatalogue $catalogue The message catalogue to dump
70 * @param string $format The format to use to dump the messages
71 * @param array $options Options that are passed to the dumper
73 * @throws InvalidArgumentException
75 public function writeTranslations(MessageCatalogue $catalogue, $format, $options = array())
77 if (!isset($this->dumpers[$format])) {
78 throw new InvalidArgumentException(sprintf('There is no dumper associated with format "%s".', $format));
81 // get the right dumper
82 $dumper = $this->dumpers[$format];
84 if (isset($options['path']) && !is_dir($options['path']) && !@mkdir($options['path'], 0777, true) && !is_dir($options['path'])) {
85 throw new RuntimeException(sprintf('Translation Writer was not able to create directory "%s"', $options['path']));
89 $dumper->dump($catalogue, $options);