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 * The language code of this writer.
39 * Gets the PO header of the current stream.
41 * @return \Drupal\Component\Gettext\PoHeader
42 * The Gettext PO header.
44 public function getHeader() {
49 * Set the PO header for the current stream.
51 * @param \Drupal\Component\Gettext\PoHeader $header
52 * The Gettext PO header to set.
54 public function setHeader(PoHeader $header) {
55 $this->header = $header;
59 * Gets the current language code used.
64 public function getLangcode() {
65 return $this->langcode;
69 * Set the language code.
71 * @param string $langcode
74 public function setLangcode($langcode) {
75 $this->langcode = $langcode;
81 public function open() {
82 // Open in write mode. Will overwrite the stream if it already exists.
83 $this->fd = fopen($this->getURI(), 'w');
84 // Write the header at the start.
89 * Implements Drupal\Component\Gettext\PoStreamInterface::close().
92 * If the stream is not open.
94 public function close() {
99 throw new \Exception('Cannot close stream that is not open.');
104 * Write data to the stream.
106 * @param string $data
107 * Piece of string to write to the stream. If the value is not directly a
108 * string, casting will happen in writing.
111 * If writing the data is not possible.
113 private function write($data) {
114 $result = fwrite($this->fd, $data);
115 if ($result === FALSE || $result != strlen($data)) {
116 throw new \Exception('Unable to write data: ' . substr($data, 0, 20));
121 * Write the PO header to the stream.
123 private function writeHeader() {
124 $this->write($this->header);
130 public function writeItem(PoItem $item) {
137 public function writeItems(PoReaderInterface $reader, $count = -1) {
138 $forever = $count == -1;
139 while (($count-- > 0 || $forever) && ($item = $reader->readItem())) {
140 $this->writeItem($item);
145 * Implements Drupal\Component\Gettext\PoStreamInterface::getURI().
148 * If the URI is not set.
150 public function getURI() {
151 if (empty($this->uri)) {
152 throw new \Exception('No URI set.');
160 public function setURI($uri) {