/** * Returns the headers as a string. * * @param Translations $translations * * @return string */ private static function generateHeaders(Translations $translations) { $headers = ''; foreach ($translations->getHeaders() as $name => $value) { $headers .= sprintf("%s: %s\n", $name, $value); } return $headers; }
/** * {@inheritdoc} */ public static function toString(Translations $translations, array $options = []) { $dom = new DOMDocument('1.0', 'utf-8'); $dom->formatOutput = true; $xliff = $dom->appendChild($dom->createElement('xliff')); $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0'); $xliff->setAttribute('version', '2.0'); $xliff->setAttribute('srcLang', $translations->getLanguage()); $xliff->setAttribute('trgLang', $translations->getLanguage()); $file = $xliff->appendChild($dom->createElement('file')); $file->setAttribute('id', $translations->getDomain() . '.' . $translations->getLanguage()); //Save headers as notes $notes = $dom->createElement('notes'); foreach ($translations->getHeaders() as $name => $value) { $notes->appendChild(self::createTextNode($dom, 'note', $value))->setAttribute('id', $name); } if ($notes->hasChildNodes()) { $file->appendChild($notes); } foreach ($translations as $translation) { $unit = $dom->createElement('unit'); $unit->setAttribute('id', md5($translation->getContext() . $translation->getOriginal())); //Save comments as notes $notes = $dom->createElement('notes'); $notes->appendChild(self::createTextNode($dom, 'note', $translation->getContext()))->setAttribute('category', 'context'); foreach ($translation->getComments() as $comment) { $notes->appendChild(self::createTextNode($dom, 'note', $comment))->setAttribute('category', 'comment'); } foreach ($translation->getExtractedComments() as $comment) { $notes->appendChild(self::createTextNode($dom, 'note', $comment))->setAttribute('category', 'extracted-comment'); } foreach ($translation->getFlags() as $flag) { $notes->appendChild(self::createTextNode($dom, 'note', $flag))->setAttribute('category', 'flag'); } foreach ($translation->getReferences() as $reference) { $notes->appendChild(self::createTextNode($dom, 'note', $reference[0] . ':' . $reference[1]))->setAttribute('category', 'reference'); } $unit->appendChild($notes); $segment = $unit->appendChild($dom->createElement('segment')); $segment->appendChild(self::createTextNode($dom, 'source', $translation->getOriginal())); $segment->appendChild(self::createTextNode($dom, 'target', $translation->getTranslation())); foreach ($translation->getPluralTranslations() as $plural) { if ($plural !== '') { $segment->appendChild(self::createTextNode($dom, 'target', $plural)); } } $file->appendChild($unit); } return $dom->saveXML(); }
/** * {@parentDoc}. */ public static function toString(Translations $translations) { $lines = array('msgid ""', 'msgstr ""'); $headers = $translations->getHeaders(); $headers['PO-Revision-Date'] = date('c'); foreach ($headers as $name => $value) { $lines[] = '"' . $name . ': ' . $value . '\\n"'; } $lines[] = ''; //Translations foreach ($translations as $translation) { if ($translation->hasComments()) { foreach ($translation->getComments() as $comment) { $lines[] = '# ' . $comment; } } if ($translation->hasExtractedComments()) { foreach ($translation->getExtractedComments() as $comment) { $lines[] = '#. ' . $comment; } } if ($translation->hasReferences()) { foreach ($translation->getReferences() as $reference) { $lines[] = '#: ' . $reference[0] . (!is_null($reference[1]) ? ':' . $reference[1] : null); } } if ($translation->hasFlags()) { $lines[] = '#, ' . implode(',', $translation->getFlags()); } if ($translation->hasContext()) { $lines[] = 'msgctxt ' . self::convertString($translation->getContext()); } self::addLines($lines, 'msgid', $translation->getOriginal()); if ($translation->hasPlural()) { self::addLines($lines, 'msgid_plural', $translation->getPlural()); self::addLines($lines, 'msgstr[0]', $translation->getTranslation()); foreach ($translation->getPluralTranslation() as $k => $v) { self::addLines($lines, 'msgstr[' . ($k + 1) . ']', $v); } } else { self::addLines($lines, 'msgstr', $translation->getTranslation()); } $lines[] = ''; } return implode("\n", $lines); }
/** * {@parentDoc}. */ public static function toString(Translations $translations, array $options = []) { $pluralForm = $translations->getPluralForms(); $pluralSize = is_array($pluralForm) ? $pluralForm[0] - 1 : null; $lines = ['msgid ""', 'msgstr ""']; foreach ($translations->getHeaders() as $name => $value) { $lines[] = sprintf('"%s: %s\\n"', $name, $value); } $lines[] = ''; //Translations foreach ($translations as $translation) { if ($translation->hasComments()) { foreach ($translation->getComments() as $comment) { $lines[] = '# ' . $comment; } } if ($translation->hasExtractedComments()) { foreach ($translation->getExtractedComments() as $comment) { $lines[] = '#. ' . $comment; } } if ($translation->hasReferences()) { foreach ($translation->getReferences() as $reference) { $lines[] = '#: ' . $reference[0] . (!is_null($reference[1]) ? ':' . $reference[1] : null); } } if ($translation->hasFlags()) { $lines[] = '#, ' . implode(',', $translation->getFlags()); } if ($translation->hasContext()) { $lines[] = 'msgctxt ' . self::convertString($translation->getContext()); } self::addLines($lines, 'msgid', $translation->getOriginal()); if ($translation->hasPlural()) { self::addLines($lines, 'msgid_plural', $translation->getPlural()); self::addLines($lines, 'msgstr[0]', $translation->getTranslation()); foreach ($translation->getPluralTranslations($pluralSize) as $k => $v) { self::addLines($lines, 'msgstr[' . ($k + 1) . ']', $v); } } else { self::addLines($lines, 'msgstr', $translation->getTranslation()); } $lines[] = ''; } return implode("\n", $lines); }
/** * Merges this translations with other translations. * * @param Translations $translations The translations instance to merge with * @param int|null $method One or various Translations::MERGE_* constants to define how to merge the translations */ public function mergeWith(Translations $translations, $method = null) { if ($method === null) { $method = self::$mergeDefault; } if ($method & self::MERGE_HEADERS) { foreach ($translations->getHeaders() as $name => $value) { if (!$this->getHeader($name)) { $this->setHeader($name, $value); } } } $add = (bool) ($method & self::MERGE_ADD); foreach ($translations as $entry) { if ($existing = $this->find($entry)) { $existing->mergeWith($entry, $method); } elseif ($add) { $this[] = clone $entry; } } if ($method & self::MERGE_REMOVE) { $filtered = array(); foreach ($this as $entry) { if ($translations->find($entry)) { $filtered[] = $entry; } } $this->exchangeArray($filtered); } if ($method & self::MERGE_LANGUAGE) { $language = $translations->getLanguage(); $pluralForm = $translations->getPluralForms(); if (!$pluralForm) { if (!empty($language)) { $this->setLanguage($language); } } else { if (!empty($language)) { $this->setHeader(self::HEADER_LANGUAGE, $language); } $this->setPluralForms($pluralForm[0], $pluralForm[1]); } } }
/** * {@parentDoc} */ public static function toString(Translations $translations) { $array = array(); $headers = ''; foreach ($translations->getHeaders() as $headerName => $headerValue) { $headers .= "{$headerName}: {$headerValue}\n"; } if ($headers !== '') { $array[''] = $headers; } foreach ($translations as $translation) { if (!$translation->hasTranslation() && !static::$includeEmptyTranslations) { continue; } if ($translation->hasContext()) { $originalString = $translation->getContext() . "" . $translation->getOriginal(); } else { $originalString = $translation->getOriginal(); } $array[$originalString] = $translation; } ksort($array); $numEntries = count($array); $originalsTable = ''; $translationsTable = ''; $originalsIndex = array(); $translationsIndex = array(); foreach ($array as $originalString => $translation) { if (is_string($translation)) { // Headers $translationString = $translation; } else { /* @var $translation \Gettext\Translation */ if ($translation->hasPlural()) { $originalString .= "" . $translation->getPlural(); } $translationString = $translation->getTranslation(); if ($translation->hasPluralTranslation()) { $translationString .= "" . implode("", $translation->getPluralTranslation()); } } $originalsIndex[] = array('relativeOffset' => strlen($originalsTable), 'length' => strlen($originalString)); $originalsTable .= $originalString . ""; $translationsIndex[] = array('relativeOffset' => strlen($translationsTable), 'length' => strlen($translationString)); $translationsTable .= $translationString . ""; } // Offset of table with the original strings index: right after the header (which is 7 words) $originalsIndexOffset = 7 * 4; // Size of table with the original strings index $originalsIndexSize = $numEntries * (4 + 4); // Offset of table with the translation strings index: right after the original strings index table $translationsIndexOffset = $originalsIndexOffset + $originalsIndexSize; // Size of table with the translation strings index $translationsIndexSize = $numEntries * (4 + 4); // Hashing table starts after the header and after the index table $originalsStringsOffset = $translationsIndexOffset + $translationsIndexSize; // Translations start after the keys $translationsStringsOffset = $originalsStringsOffset + strlen($originalsTable); // Let's generate the .mo file binary data $mo = ''; // Magic number $mo .= pack('L', 0x950412de); // File format revision $mo .= pack('L', 0); // Number of strings $mo .= pack('L', $numEntries); // Offset of table with original strings $mo .= pack('L', $originalsIndexOffset); // Offset of table with translation strings $mo .= pack('L', $translationsIndexOffset); // Size of hashing table: we don't use it. $mo .= pack('L', 0); // Offset of hashing table: it would start right after the translations index table $mo .= pack('L', $translationsIndexOffset + $translationsIndexSize); // Write the lengths & offsets of the original strings foreach ($originalsIndex as $info) { $mo .= pack('L', $info['length']); $mo .= pack('L', $originalsStringsOffset + $info['relativeOffset']); } // Write the lengths & offsets of the translated strings foreach ($translationsIndex as $info) { $mo .= pack('L', $info['length']); $mo .= pack('L', $translationsStringsOffset + $info['relativeOffset']); } // Write original strings $mo .= $originalsTable; // Write translation strings $mo .= $translationsTable; return $mo; }