PoMemoryWriter.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drupal\Component\Gettext\PoMemoryWriter.
  5. */
  6. /**
  7. * Defines a Gettext PO memory writer, to be used by the installer.
  8. */
  9. class PoMemoryWriter implements PoWriterInterface {
  10. /**
  11. * Array to hold all PoItem elements.
  12. *
  13. * @var array
  14. */
  15. private $_items;
  16. /**
  17. * Constructor, initialize empty items.
  18. */
  19. public function __construct() {
  20. $this->_items = array();
  21. }
  22. /**
  23. * Implements PoWriterInterface::writeItem().
  24. */
  25. public function writeItem(PoItem $item) {
  26. $context = $item->getContext();
  27. $context = $context != NULL ? $context : '';
  28. if ($item->isPlural()) {
  29. $sources = $item->getSource();
  30. $translations = $item->getTranslation();
  31. // Build additional source strings for plurals.
  32. $entries = array_keys($translations);
  33. for ($i = 3; $i <= count($entries); $i++) {
  34. $sources[] = $sources[1];
  35. }
  36. $translations = array_map('_locale_import_append_plural', $translations, $entries);
  37. $sources = array_map('_locale_import_append_plural', $sources, $entries);
  38. foreach ($entries as $index) {
  39. $this->_items[][$sources[$index]] = $translations[$index];
  40. }
  41. }
  42. else {
  43. $this->_items[$context][$item->getSource()] = $item->getTranslation();
  44. }
  45. }
  46. /**
  47. * Implements PoWriterInterface::writeItems().
  48. */
  49. public function writeItems(PoReaderInterface $reader, $count = -1) {
  50. $forever = $count == -1;
  51. while (($count-- > 0 || $forever) && ($item = $reader->readItem())) {
  52. $this->writeItem($item);
  53. }
  54. }
  55. /**
  56. * Get all stored PoItem's.
  57. *
  58. * @return array
  59. * Array of PO item's.
  60. */
  61. public function getData() {
  62. return $this->_items;
  63. }
  64. /**
  65. * Implements Drupal\Component\Gettext\PoMetadataInterface:setLangcode().
  66. *
  67. * Not implemented. Not relevant for the MemoryWriter.
  68. */
  69. public function setLangcode($langcode) {
  70. }
  71. /**
  72. * Implements Drupal\Component\Gettext\PoMetadataInterface:getLangcode().
  73. *
  74. * Not implemented. Not relevant for the MemoryWriter.
  75. */
  76. public function getLangcode() {
  77. }
  78. /**
  79. * Implements Drupal\Component\Gettext\PoMetadataInterface:getHeader().
  80. *
  81. * Not implemented. Not relevant for the MemoryWriter.
  82. */
  83. public function getHeader() {
  84. }
  85. /**
  86. * Implements Drupal\Component\Gettext\PoMetadataInterface:setHeader().
  87. *
  88. * Not implemented. Not relevant for the MemoryWriter.
  89. */
  90. public function setHeader(PoHeader $header) {
  91. }
  92. }