3 namespace Drupal\Component\Gettext;
6 * PoItem handles one translation.
8 * @todo: This class contains some really old legacy code.
9 * @see https://www.drupal.org/node/1637662
14 * The language code this translation is in.
21 * The context this translation belongs to.
25 private $_context = '';
28 * The source string or array of strings if it has plurals.
30 * @var string or array
36 * Flag indicating if this translation has plurals.
43 * The comment of this translation.
50 * The translation string or array of strings if it has plurals.
52 * @var string or array
55 private $_translation;
58 * Gets the language code of the currently used language.
60 * @return string with langcode
62 public function getLangcode() {
63 return $this->_langcode;
67 * Set the language code of the current language.
69 * @param string $langcode
71 public function setLangcode($langcode) {
72 $this->_langcode = $langcode;
76 * Gets the context this translation belongs to.
78 * @return string $context
80 public function getContext() {
81 return $this->_context;
85 * Set the context this translation belongs to.
87 * @param string $context
89 public function setContext($context) {
90 $this->_context = $context;
94 * Gets the source string or the array of strings if the translation has
97 * @return string or array $translation
99 public function getSource() {
100 return $this->_source;
104 * Set the source string or the array of strings if the translation has
107 * @param string or array $source
109 public function setSource($source) {
110 $this->_source = $source;
114 * Gets the translation string or the array of strings if the translation has
117 * @return string or array $translation
119 public function getTranslation() {
120 return $this->_translation;
124 * Set the translation string or the array of strings if the translation has
127 * @param string or array $translation
129 public function setTranslation($translation) {
130 $this->_translation = $translation;
134 * Set if the translation has plural values.
136 * @param bool $plural
138 public function setPlural($plural) {
139 $this->_plural = $plural;
143 * Get if the translation has plural values.
147 public function isPlural() {
148 return $this->_plural;
152 * Gets the comment of this translation.
154 * @return String $comment
156 public function getComment() {
157 return $this->_comment;
161 * Set the comment of this translation.
163 * @param string $comment
165 public function setComment($comment) {
166 $this->_comment = $comment;
170 * Create the PoItem from a structured array.
172 * @param array $values
174 public function setFromArray(array $values = []) {
175 if (isset($values['context'])) {
176 $this->setContext($values['context']);
178 if (isset($values['source'])) {
179 $this->setSource($values['source']);
181 if (isset($values['translation'])) {
182 $this->setTranslation($values['translation']);
184 if (isset($values['comment'])) {
185 $this->setComment($values['comment']);
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);
196 * Output the PoItem as a string.
198 public function __toString() {
199 return $this->formatItem();
203 * Format the POItem as a string.
205 private function formatItem() {
208 // Format string context.
209 if (!empty($this->_context)) {
210 $output .= 'msgctxt ' . $this->formatString($this->_context);
213 // Format translation.
214 if ($this->_plural) {
215 $output .= $this->formatPlural();
218 $output .= $this->formatSingular();
221 // Add one empty line to separate the translations.
228 * Formats a plural translation.
230 private function formatPlural() {
233 // Format source strings.
234 $output .= 'msgid ' . $this->formatString($this->_source[0]);
235 $output .= 'msgid_plural ' . $this->formatString($this->_source[1]);
237 foreach ($this->_translation as $i => $trans) {
238 if (isset($this->_translation[$i])) {
239 $output .= 'msgstr[' . $i . '] ' . $this->formatString($trans);
242 $output .= 'msgstr[' . $i . '] ""' . "\n";
250 * Formats a singular translation.
252 private function formatSingular() {
254 $output .= 'msgid ' . $this->formatString($this->_source);
255 $output .= 'msgstr ' . (isset($this->_translation) ? $this->formatString($this->_translation) : '""');
260 * Formats a string for output on multiple lines.
262 private function formatString($string) {
263 // Escape characters for processing.
264 $string = addcslashes($string, "\0..\37\\\"");
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"));
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";
276 // Single line strings are output on the same line.
278 return "\"$parts[0]\"\n";