6c70990851d00b5e98baa69a37cc29962a630cf1
[yaffs-website] / web / core / lib / Drupal / Component / Gettext / PoItem.php
1 <?php
2
3 namespace Drupal\Component\Gettext;
4
5 /**
6  * PoItem handles one translation.
7  *
8  * @todo: This class contains some really old legacy code.
9  * @see https://www.drupal.org/node/1637662
10  */
11 class PoItem {
12
13   /**
14    * The language code this translation is in.
15    *
16    * @var string
17    */
18   private $_langcode;
19
20   /**
21    * The context this translation belongs to.
22    *
23    * @var string
24    */
25   private $_context = '';
26
27   /**
28    * The source string or array of strings if it has plurals.
29    *
30    * @var string|array
31    *
32    * @see $_plural
33    */
34   private $_source;
35
36   /**
37    * Flag indicating if this translation has plurals.
38    *
39    * @var bool
40    */
41   private $_plural;
42
43   /**
44    * The comment of this translation.
45    *
46    * @var string
47    */
48   private $_comment;
49
50   /**
51    * The translation string or array of strings if it has plurals.
52    *
53    * @var string|array
54    * @see $_plural
55    */
56   private $_translation;
57
58   /**
59    * Gets the language code of the currently used language.
60    *
61    * @return string with langcode
62    */
63   public function getLangcode() {
64     return $this->_langcode;
65   }
66
67   /**
68    * Set the language code of the current language.
69    *
70    * @param string $langcode
71    */
72   public function setLangcode($langcode) {
73     $this->_langcode = $langcode;
74   }
75
76   /**
77    * Gets the context this translation belongs to.
78    *
79    * @return string $context
80    */
81   public function getContext() {
82     return $this->_context;
83   }
84
85   /**
86    * Set the context this translation belongs to.
87    *
88    * @param string $context
89    */
90   public function setContext($context) {
91     $this->_context = $context;
92   }
93
94   /**
95    * Gets the source string or the array of strings if the translation has
96    * plurals.
97    *
98    * @return string or array $translation
99    */
100   public function getSource() {
101     return $this->_source;
102   }
103
104   /**
105    * Set the source string or the array of strings if the translation has
106    * plurals.
107    *
108    * @param string|array $source
109    */
110   public function setSource($source) {
111     $this->_source = $source;
112   }
113
114   /**
115    * Gets the translation string or the array of strings if the translation has
116    * plurals.
117    *
118    * @return string or array $translation
119    */
120   public function getTranslation() {
121     return $this->_translation;
122   }
123
124   /**
125    * Set the translation string or the array of strings if the translation has
126    * plurals.
127    *
128    * @param string|array $translation
129    */
130   public function setTranslation($translation) {
131     $this->_translation = $translation;
132   }
133
134   /**
135    * Set if the translation has plural values.
136    *
137    * @param bool $plural
138    */
139   public function setPlural($plural) {
140     $this->_plural = $plural;
141   }
142
143   /**
144    * Get if the translation has plural values.
145    *
146    * @return bool
147    */
148   public function isPlural() {
149     return $this->_plural;
150   }
151
152   /**
153    * Gets the comment of this translation.
154    *
155    * @return String $comment
156    */
157   public function getComment() {
158     return $this->_comment;
159   }
160
161   /**
162    * Set the comment of this translation.
163    *
164    * @param string $comment
165    */
166   public function setComment($comment) {
167     $this->_comment = $comment;
168   }
169
170   /**
171    * Create the PoItem from a structured array.
172    *
173    * @param array $values
174    */
175   public function setFromArray(array $values = []) {
176     if (isset($values['context'])) {
177       $this->setContext($values['context']);
178     }
179     if (isset($values['source'])) {
180       $this->setSource($values['source']);
181     }
182     if (isset($values['translation'])) {
183       $this->setTranslation($values['translation']);
184     }
185     if (isset($values['comment'])) {
186       $this->setComment($values['comment']);
187     }
188     if (isset($this->_source) &&
189         strpos($this->_source, LOCALE_PLURAL_DELIMITER) !== FALSE) {
190       $this->setSource(explode(LOCALE_PLURAL_DELIMITER, $this->_source));
191       $this->setTranslation(explode(LOCALE_PLURAL_DELIMITER, $this->_translation));
192       $this->setPlural(count($this->_source) > 1);
193     }
194   }
195
196   /**
197    * Output the PoItem as a string.
198    */
199   public function __toString() {
200     return $this->formatItem();
201   }
202
203   /**
204    * Format the POItem as a string.
205    */
206   private function formatItem() {
207     $output = '';
208
209     // Format string context.
210     if (!empty($this->_context)) {
211       $output .= 'msgctxt ' . $this->formatString($this->_context);
212     }
213
214     // Format translation.
215     if ($this->_plural) {
216       $output .= $this->formatPlural();
217     }
218     else {
219       $output .= $this->formatSingular();
220     }
221
222     // Add one empty line to separate the translations.
223     $output .= "\n";
224
225     return $output;
226   }
227
228   /**
229    * Formats a plural translation.
230    */
231   private function formatPlural() {
232     $output = '';
233
234     // Format source strings.
235     $output .= 'msgid ' . $this->formatString($this->_source[0]);
236     $output .= 'msgid_plural ' . $this->formatString($this->_source[1]);
237
238     foreach ($this->_translation as $i => $trans) {
239       if (isset($this->_translation[$i])) {
240         $output .= 'msgstr[' . $i . '] ' . $this->formatString($trans);
241       }
242       else {
243         $output .= 'msgstr[' . $i . '] ""' . "\n";
244       }
245     }
246
247     return $output;
248   }
249
250   /**
251    * Formats a singular translation.
252    */
253   private function formatSingular() {
254     $output = '';
255     $output .= 'msgid ' . $this->formatString($this->_source);
256     $output .= 'msgstr ' . (isset($this->_translation) ? $this->formatString($this->_translation) : '""');
257     return $output;
258   }
259
260   /**
261    * Formats a string for output on multiple lines.
262    */
263   private function formatString($string) {
264     // Escape characters for processing.
265     $string = addcslashes($string, "\0..\37\\\"");
266
267     // Always include a line break after the explicit \n line breaks from
268     // the source string. Otherwise wrap at 70 chars to accommodate the extra
269     // format overhead too.
270     $parts = explode("\n", wordwrap(str_replace('\n', "\\n\n", $string), 70, " \n"));
271
272     // Multiline string should be exported starting with a "" and newline to
273     // have all lines aligned on the same column.
274     if (count($parts) > 1) {
275       return "\"\"\n\"" . implode("\"\n\"", $parts) . "\"\n";
276     }
277     // Single line strings are output on the same line.
278     else {
279       return "\"$parts[0]\"\n";
280     }
281   }
282
283 }