PoMemoryWriter.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Drupal\Component\Gettext;
  3. /**
  4. * Defines a Gettext PO memory writer, to be used by the installer.
  5. */
  6. class PoMemoryWriter implements PoWriterInterface {
  7. /**
  8. * Array to hold all PoItem elements.
  9. *
  10. * @var array
  11. */
  12. protected $items;
  13. /**
  14. * Constructor, initialize empty items.
  15. */
  16. public function __construct() {
  17. $this->items = [];
  18. }
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function writeItem(PoItem $item) {
  23. if (is_array($item->getSource())) {
  24. $item->setSource(implode(LOCALE_PLURAL_DELIMITER, $item->getSource()));
  25. $item->setTranslation(implode(LOCALE_PLURAL_DELIMITER, $item->getTranslation()));
  26. }
  27. $context = $item->getContext();
  28. $this->items[$context != NULL ? $context : ''][$item->getSource()] = $item->getTranslation();
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function writeItems(PoReaderInterface $reader, $count = -1) {
  34. $forever = $count == -1;
  35. while (($count-- > 0 || $forever) && ($item = $reader->readItem())) {
  36. $this->writeItem($item);
  37. }
  38. }
  39. /**
  40. * Get all stored PoItem's.
  41. *
  42. * @return array PoItem
  43. */
  44. public function getData() {
  45. return $this->items;
  46. }
  47. /**
  48. * Implements Drupal\Component\Gettext\PoMetadataInterface:setLangcode().
  49. *
  50. * Not implemented. Not relevant for the MemoryWriter.
  51. */
  52. public function setLangcode($langcode) {
  53. }
  54. /**
  55. * Implements Drupal\Component\Gettext\PoMetadataInterface:getLangcode().
  56. *
  57. * Not implemented. Not relevant for the MemoryWriter.
  58. */
  59. public function getLangcode() {
  60. }
  61. /**
  62. * Implements Drupal\Component\Gettext\PoMetadataInterface:getHeader().
  63. *
  64. * Not implemented. Not relevant for the MemoryWriter.
  65. */
  66. public function getHeader() {
  67. }
  68. /**
  69. * Implements Drupal\Component\Gettext\PoMetadataInterface:setHeader().
  70. *
  71. * Not implemented. Not relevant for the MemoryWriter.
  72. */
  73. public function setHeader(PoHeader $header) {
  74. }
  75. }