3 namespace Drupal\Component\Gettext;
6 * Defines a Gettext PO stream writer.
8 class PoStreamWriter implements PoWriterInterface, PoStreamInterface {
11 * URI of the PO stream that is being written.
18 * The Gettext PO header.
20 * @var \Drupal\Component\Gettext\PoHeader
25 * File handle of the current PO stream.
32 * Gets the PO header of the current stream.
34 * @return \Drupal\Component\Gettext\PoHeader
35 * The Gettext PO header.
37 public function getHeader() {
38 return $this->_header;
42 * Set the PO header for the current stream.
44 * @param \Drupal\Component\Gettext\PoHeader $header
45 * The Gettext PO header to set.
47 public function setHeader(PoHeader $header) {
48 $this->_header = $header;
52 * Gets the current language code used.
57 public function getLangcode() {
58 return $this->_langcode;
62 * Set the language code.
64 * @param string $langcode
67 public function setLangcode($langcode) {
68 $this->_langcode = $langcode;
74 public function open() {
75 // Open in write mode. Will overwrite the stream if it already exists.
76 $this->_fd = fopen($this->getURI(), 'w');
77 // Write the header at the start.
82 * Implements Drupal\Component\Gettext\PoStreamInterface::close().
85 * If the stream is not open.
87 public function close() {
92 throw new Exception('Cannot close stream that is not open.');
97 * Write data to the stream.
100 * Piece of string to write to the stream. If the value is not directly a
101 * string, casting will happen in writing.
104 * If writing the data is not possible.
106 private function write($data) {
107 $result = fwrite($this->_fd, $data);
108 if ($result === FALSE) {
109 throw new Exception('Unable to write data: ' . substr($data, 0, 20));
114 * Write the PO header to the stream.
116 private function writeHeader() {
117 $this->write($this->_header);
123 public function writeItem(PoItem $item) {
130 public function writeItems(PoReaderInterface $reader, $count = -1) {
131 $forever = $count == -1;
132 while (($count-- > 0 || $forever) && ($item = $reader->readItem())) {
133 $this->writeItem($item);
138 * Implements Drupal\Component\Gettext\PoStreamInterface::getURI().
141 * If the URI is not set.
143 public function getURI() {
144 if (empty($this->_uri)) {
145 throw new Exception('No URI set.');
153 public function setURI($uri) {