菜谱项目

TranslationWriter.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  14. use Symfony\Component\Translation\Exception\RuntimeException;
  15. /**
  16. * TranslationWriter writes translation messages.
  17. *
  18. * @author Michel Salib <michelsalib@hotmail.com>
  19. */
  20. class TranslationWriter
  21. {
  22. private $dumpers = array();
  23. /**
  24. * Adds a dumper to the writer.
  25. *
  26. * @param string $format The format of the dumper
  27. * @param DumperInterface $dumper The dumper
  28. */
  29. public function addDumper($format, DumperInterface $dumper)
  30. {
  31. $this->dumpers[$format] = $dumper;
  32. }
  33. /**
  34. * Disables dumper backup.
  35. */
  36. public function disableBackup()
  37. {
  38. foreach ($this->dumpers as $dumper) {
  39. if (method_exists($dumper, 'setBackup')) {
  40. $dumper->setBackup(false);
  41. }
  42. }
  43. }
  44. /**
  45. * Obtains the list of supported formats.
  46. *
  47. * @return array
  48. */
  49. public function getFormats()
  50. {
  51. return array_keys($this->dumpers);
  52. }
  53. /**
  54. * Writes translation from the catalogue according to the selected format.
  55. *
  56. * @param MessageCatalogue $catalogue The message catalogue to dump
  57. * @param string $format The format to use to dump the messages
  58. * @param array $options Options that are passed to the dumper
  59. *
  60. * @throws InvalidArgumentException
  61. */
  62. public function writeTranslations(MessageCatalogue $catalogue, $format, $options = array())
  63. {
  64. if (!isset($this->dumpers[$format])) {
  65. throw new InvalidArgumentException(sprintf('There is no dumper associated with format "%s".', $format));
  66. }
  67. // get the right dumper
  68. $dumper = $this->dumpers[$format];
  69. if (isset($options['path']) && !is_dir($options['path']) && !@mkdir($options['path'], 0777, true) && !is_dir($options['path'])) {
  70. throw new RuntimeException(sprintf('Translation Writer was not able to create directory "%s"', $options['path']));
  71. }
  72. // save
  73. $dumper->dump($catalogue, $options);
  74. }
  75. }