/** * {@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(); }
/** * Generates an array with the translations. * * @param Translations $translations * * @return array */ public static function toArray(Translations $translations) { $array = array(); $context_glue = ""; foreach ($translations as $translation) { $key = ($translation->hasContext() ? $translation->getContext() . $context_glue : '') . $translation->getOriginal(); $entry = array($translation->getPlural(), $translation->getTranslation()); if ($translation->hasPluralTranslation()) { $entry = array_merge($entry, $translation->getPluralTranslation()); } $array[$key] = $entry; } $domain = $translations->getDomain() ?: 'messages'; $lang = $translations->getLanguage() ?: 'en'; $fullArray = array($domain => array('' => array('domain' => $domain, 'lang' => $lang, 'plural-forms' => 'nplurals=2; plural=(n != 1);'))); if ($translations->getHeader('Plural-Forms') !== null) { $fullArray[$domain]['']['plural-forms'] = $translations->getHeader('Plural-Forms'); } $fullArray[$domain] = array_merge($fullArray[$domain], $array); return $fullArray; }
/** * Search for specific functions and create translations. * * @param Translations $translations The translations instance where save the values * @param array $options The extractor options */ public function saveGettextFunctions(Translations $translations, array $options) { $functions = $options['functions']; $file = $options['file']; foreach ($this->getFunctions() as $function) { list($name, $line, $args) = $function; if (!isset($functions[$name])) { continue; } $domain = $context = $original = $plural = null; switch ($functions[$name]) { case 'gettext': if (!isset($args[0])) { continue 2; } $original = $args[0]; break; case 'ngettext': if (!isset($args[1])) { continue 2; } list($original, $plural) = $args; break; case 'pgettext': if (!isset($args[1])) { continue 2; } list($context, $original) = $args; break; case 'dgettext': if (!isset($args[1])) { continue 2; } list($domain, $original) = $args; break; case 'dpgettext': if (!isset($args[2])) { continue 2; } list($domain, $context, $original) = $args; break; case 'npgettext': if (!isset($args[2])) { continue 2; } list($context, $original, $plural) = $args; break; case 'dnpgettext': if (!isset($args[4])) { continue 2; } list($domain, $context, $original, $plural) = $args; break; default: throw new Exception(sprintf('Not valid function %s', $functions[$name])); } if ((string) $original !== '' && ($domain === null || $domain === $translations->getDomain())) { $translation = $translations->insert($context, $original, $plural); $translation->addReference($file, $line); if (isset($function[3])) { foreach ($function[3] as $extractedComment) { $translation->addExtractedComment($extractedComment); } } } } }
/** * {@parentDoc}. */ public static function toString(Translations $translations, array $options = []) { $domain = $translations->getDomain() ?: 'messages'; $options += static::$options; return json_encode([$domain => ['' => ['domain' => $domain, 'lang' => $translations->getLanguage() ?: 'en', 'plural-forms' => $translations->getHeader('Plural-Forms') ?: 'nplurals=2; plural=(n != 1);']] + self::buildMessages($translations)], $options['json']); }