Chris@0: langcode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set the language code of the current language. Chris@0: * Chris@0: * @param string $langcode Chris@0: */ Chris@0: public function setLangcode($langcode) { Chris@17: $this->langcode = $langcode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the context this translation belongs to. Chris@0: * Chris@0: * @return string $context Chris@0: */ Chris@0: public function getContext() { Chris@17: return $this->context; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set the context this translation belongs to. Chris@0: * Chris@0: * @param string $context Chris@0: */ Chris@0: public function setContext($context) { Chris@17: $this->context = $context; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the source string or the array of strings if the translation has Chris@0: * plurals. Chris@0: * Chris@0: * @return string or array $translation Chris@0: */ Chris@0: public function getSource() { Chris@17: return $this->source; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set the source string or the array of strings if the translation has Chris@0: * plurals. Chris@0: * Chris@0: * @param string|array $source Chris@0: */ Chris@0: public function setSource($source) { Chris@17: $this->source = $source; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the translation string or the array of strings if the translation has Chris@0: * plurals. Chris@0: * Chris@0: * @return string or array $translation Chris@0: */ Chris@0: public function getTranslation() { Chris@17: return $this->translation; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set the translation string or the array of strings if the translation has Chris@0: * plurals. Chris@0: * Chris@0: * @param string|array $translation Chris@0: */ Chris@0: public function setTranslation($translation) { Chris@17: $this->translation = $translation; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set if the translation has plural values. Chris@0: * Chris@0: * @param bool $plural Chris@0: */ Chris@0: public function setPlural($plural) { Chris@17: $this->plural = $plural; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get if the translation has plural values. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isPlural() { Chris@17: return $this->plural; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the comment of this translation. Chris@0: * Chris@0: * @return String $comment Chris@0: */ Chris@0: public function getComment() { Chris@17: return $this->comment; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set the comment of this translation. Chris@0: * Chris@0: * @param string $comment Chris@0: */ Chris@0: public function setComment($comment) { Chris@17: $this->comment = $comment; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Create the PoItem from a structured array. Chris@0: * Chris@0: * @param array $values Chris@0: */ Chris@0: public function setFromArray(array $values = []) { Chris@0: if (isset($values['context'])) { Chris@0: $this->setContext($values['context']); Chris@0: } Chris@0: if (isset($values['source'])) { Chris@0: $this->setSource($values['source']); Chris@0: } Chris@0: if (isset($values['translation'])) { Chris@0: $this->setTranslation($values['translation']); Chris@0: } Chris@0: if (isset($values['comment'])) { Chris@0: $this->setComment($values['comment']); Chris@0: } Chris@17: if (isset($this->source) && Chris@18: strpos($this->source, self::DELIMITER) !== FALSE) { Chris@18: $this->setSource(explode(self::DELIMITER, $this->source)); Chris@18: $this->setTranslation(explode(self::DELIMITER, $this->translation)); Chris@17: $this->setPlural(count($this->source) > 1); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Output the PoItem as a string. Chris@0: */ Chris@0: public function __toString() { Chris@0: return $this->formatItem(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Format the POItem as a string. Chris@0: */ Chris@0: private function formatItem() { Chris@0: $output = ''; Chris@0: Chris@0: // Format string context. Chris@17: if (!empty($this->context)) { Chris@17: $output .= 'msgctxt ' . $this->formatString($this->context); Chris@0: } Chris@0: Chris@0: // Format translation. Chris@17: if ($this->plural) { Chris@0: $output .= $this->formatPlural(); Chris@0: } Chris@0: else { Chris@0: $output .= $this->formatSingular(); Chris@0: } Chris@0: Chris@0: // Add one empty line to separate the translations. Chris@0: $output .= "\n"; Chris@0: Chris@0: return $output; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Formats a plural translation. Chris@0: */ Chris@0: private function formatPlural() { Chris@0: $output = ''; Chris@0: Chris@0: // Format source strings. Chris@17: $output .= 'msgid ' . $this->formatString($this->source[0]); Chris@17: $output .= 'msgid_plural ' . $this->formatString($this->source[1]); Chris@0: Chris@17: foreach ($this->translation as $i => $trans) { Chris@17: if (isset($this->translation[$i])) { Chris@0: $output .= 'msgstr[' . $i . '] ' . $this->formatString($trans); Chris@0: } Chris@0: else { Chris@0: $output .= 'msgstr[' . $i . '] ""' . "\n"; Chris@0: } Chris@0: } Chris@0: Chris@0: return $output; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Formats a singular translation. Chris@0: */ Chris@0: private function formatSingular() { Chris@0: $output = ''; Chris@17: $output .= 'msgid ' . $this->formatString($this->source); Chris@17: $output .= 'msgstr ' . (isset($this->translation) ? $this->formatString($this->translation) : '""'); Chris@0: return $output; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Formats a string for output on multiple lines. Chris@0: */ Chris@0: private function formatString($string) { Chris@0: // Escape characters for processing. Chris@0: $string = addcslashes($string, "\0..\37\\\""); Chris@0: Chris@0: // Always include a line break after the explicit \n line breaks from Chris@0: // the source string. Otherwise wrap at 70 chars to accommodate the extra Chris@0: // format overhead too. Chris@0: $parts = explode("\n", wordwrap(str_replace('\n', "\\n\n", $string), 70, " \n")); Chris@0: Chris@0: // Multiline string should be exported starting with a "" and newline to Chris@0: // have all lines aligned on the same column. Chris@0: if (count($parts) > 1) { Chris@0: return "\"\"\n\"" . implode("\"\n\"", $parts) . "\"\n"; Chris@0: } Chris@0: // Single line strings are output on the same line. Chris@0: else { Chris@0: return "\"$parts[0]\"\n"; Chris@0: } Chris@0: } Chris@0: Chris@0: }