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 protected $context = '';
28 * The source string or array of strings if it has plurals.
37 * Flag indicating if this translation has plurals.
44 * The comment of this translation.
51 * The translation string or array of strings if it has plurals.
56 protected $translation;
59 * Gets the language code of the currently used language.
61 * @return string with langcode
63 public function getLangcode() {
64 return $this->langcode;
68 * Set the language code of the current language.
70 * @param string $langcode
72 public function setLangcode($langcode) {
73 $this->langcode = $langcode;
77 * Gets the context this translation belongs to.
79 * @return string $context
81 public function getContext() {
82 return $this->context;
86 * Set the context this translation belongs to.
88 * @param string $context
90 public function setContext($context) {
91 $this->context = $context;
95 * Gets the source string or the array of strings if the translation has
98 * @return string or array $translation
100 public function getSource() {
101 return $this->source;
105 * Set the source string or the array of strings if the translation has
108 * @param string|array $source
110 public function setSource($source) {
111 $this->source = $source;
115 * Gets the translation string or the array of strings if the translation has
118 * @return string or array $translation
120 public function getTranslation() {
121 return $this->translation;
125 * Set the translation string or the array of strings if the translation has
128 * @param string|array $translation
130 public function setTranslation($translation) {
131 $this->translation = $translation;
135 * Set if the translation has plural values.
137 * @param bool $plural
139 public function setPlural($plural) {
140 $this->plural = $plural;
144 * Get if the translation has plural values.
148 public function isPlural() {
149 return $this->plural;
153 * Gets the comment of this translation.
155 * @return String $comment
157 public function getComment() {
158 return $this->comment;
162 * Set the comment of this translation.
164 * @param string $comment
166 public function setComment($comment) {
167 $this->comment = $comment;
171 * Create the PoItem from a structured array.
173 * @param array $values
175 public function setFromArray(array $values = []) {
176 if (isset($values['context'])) {
177 $this->setContext($values['context']);
179 if (isset($values['source'])) {
180 $this->setSource($values['source']);
182 if (isset($values['translation'])) {
183 $this->setTranslation($values['translation']);
185 if (isset($values['comment'])) {
186 $this->setComment($values['comment']);
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);
197 * Output the PoItem as a string.
199 public function __toString() {
200 return $this->formatItem();
204 * Format the POItem as a string.
206 private function formatItem() {
209 // Format string context.
210 if (!empty($this->context)) {
211 $output .= 'msgctxt ' . $this->formatString($this->context);
214 // Format translation.
216 $output .= $this->formatPlural();
219 $output .= $this->formatSingular();
222 // Add one empty line to separate the translations.
229 * Formats a plural translation.
231 private function formatPlural() {
234 // Format source strings.
235 $output .= 'msgid ' . $this->formatString($this->source[0]);
236 $output .= 'msgid_plural ' . $this->formatString($this->source[1]);
238 foreach ($this->translation as $i => $trans) {
239 if (isset($this->translation[$i])) {
240 $output .= 'msgstr[' . $i . '] ' . $this->formatString($trans);
243 $output .= 'msgstr[' . $i . '] ""' . "\n";
251 * Formats a singular translation.
253 private function formatSingular() {
255 $output .= 'msgid ' . $this->formatString($this->source);
256 $output .= 'msgstr ' . (isset($this->translation) ? $this->formatString($this->translation) : '""');
261 * Formats a string for output on multiple lines.
263 private function formatString($string) {
264 // Escape characters for processing.
265 $string = addcslashes($string, "\0..\37\\\"");
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"));
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";
277 // Single line strings are output on the same line.
279 return "\"$parts[0]\"\n";