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;
18 * TranslationWriter writes translation messages.
20 * @author Michel Salib <michelsalib@hotmail.com>
22 class TranslationWriter
25 * Dumpers used for export.
29 private $dumpers = array();
32 * Adds a dumper to the writer.
34 * @param string $format The format of the dumper
35 * @param DumperInterface $dumper The dumper
37 public function addDumper($format, DumperInterface $dumper)
39 $this->dumpers[$format] = $dumper;
43 * Disables dumper backup.
45 public function disableBackup()
47 foreach ($this->dumpers as $dumper) {
48 if (method_exists($dumper, 'setBackup')) {
49 $dumper->setBackup(false);
55 * Obtains the list of supported formats.
59 public function getFormats()
61 return array_keys($this->dumpers);
65 * Writes translation from the catalogue according to the selected format.
67 * @param MessageCatalogue $catalogue The message catalogue to dump
68 * @param string $format The format to use to dump the messages
69 * @param array $options Options that are passed to the dumper
71 * @throws \InvalidArgumentException
73 public function writeTranslations(MessageCatalogue $catalogue, $format, $options = array())
75 if (!isset($this->dumpers[$format])) {
76 throw new \InvalidArgumentException(sprintf('There is no dumper associated with format "%s".', $format));
79 // get the right dumper
80 $dumper = $this->dumpers[$format];
82 if (isset($options['path']) && !is_dir($options['path']) && !@mkdir($options['path'], 0777, true) && !is_dir($options['path'])) {
83 throw new \RuntimeException(sprintf('Translation Writer was not able to create directory "%s"', $options['path']));
87 $dumper->dump($catalogue, $options);