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