TranslationWriter.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\Writer;
  11. use Symfony\Component\Translation\MessageCatalogue;
  12. use Symfony\Component\Translation\Dumper\DumperInterface;
  13. /**
  14. * TranslationWriter writes translation messages.
  15. *
  16. * @author Michel Salib <michelsalib@hotmail.com>
  17. */
  18. class TranslationWriter
  19. {
  20. /**
  21. * Dumpers used for export.
  22. *
  23. * @var array
  24. */
  25. private $dumpers = array();
  26. /**
  27. * Adds a dumper to the writer.
  28. *
  29. * @param string $format The format of the dumper
  30. * @param DumperInterface $dumper The dumper
  31. */
  32. public function addDumper($format, DumperInterface $dumper)
  33. {
  34. $this->dumpers[$format] = $dumper;
  35. }
  36. /**
  37. * Obtains the list of supported formats.
  38. *
  39. * @return array
  40. */
  41. public function getFormats()
  42. {
  43. return array_keys($this->dumpers);
  44. }
  45. /**
  46. * Writes translation from the catalogue according to the selected format.
  47. *
  48. * @param MessageCatalogue $catalogue The message catalogue to dump
  49. * @param string $format The format to use to dump the messages
  50. * @param array $options Options that are passed to the dumper
  51. *
  52. * @throws \InvalidArgumentException
  53. */
  54. public function writeTranslations(MessageCatalogue $catalogue, $format, $options = array())
  55. {
  56. if (!isset($this->dumpers[$format])) {
  57. throw new \InvalidArgumentException(sprintf('There is no dumper associated with format "%s".', $format));
  58. }
  59. // get the right dumper
  60. $dumper = $this->dumpers[$format];
  61. // save
  62. $dumper->dump($catalogue, $options);
  63. }
  64. }