protected function writeReal(MessageCollection $collection) { $template = <<<XML <?xml version="1.0" encoding="utf-8"?> <resources></resources> XML; $writer = new SimpleXMLElement($template); $mangler = $this->group->getMangler(); $collection->filter('hastranslation', false); if (count($collection) === 0) { return ''; } /** * @var $m TMessage */ foreach ($collection as $key => $m) { $key = $mangler->unmangle($key); $value = $m->translation(); $value = str_replace(TRANSLATE_FUZZY, '', $value); // Handle plurals if (strpos($value, '{{PLURAL') === false) { $element = $writer->addChild('string', $this->formatElementContents($value)); } else { $element = $writer->addChild('plurals'); $forms = $this->unflattenPlural($value); foreach ($forms as $quantity => $content) { $item = $element->addChild('item', $this->formatElementContents($content)); $item->addAttribute('quantity', $quantity); } } $element->addAttribute('name', $key); // This is non-standard if ($m->hasTag('fuzzy')) { $element->addAttribute('fuzzy', 'true'); } } // Make the output pretty with DOMDocument $dom = new DOMDocument('1.0'); $dom->formatOutput = true; $dom->loadXML($writer->asXML()); return $dom->saveXML(); }
/** * Returns translation page with all possible translations replaced in * and ugly translation tags removed. * * @param MessageCollection $collection Collection that holds translated messages. * @return string Whole page as wikitext. */ public function getTranslationPageText($collection) { $text = $this->template; // The source // For finding the messages $prefix = $this->title->getPrefixedDBKey() . '/'; if ($collection instanceof MessageCollection) { $collection->loadTranslations(DB_MASTER); $collection->filter('translated', false); } foreach ($this->sections as $ph => $s) { $sectiontext = null; if (isset($collection[$prefix . $s->id])) { /** * @var TMessage $msg */ $msg = $collection[$prefix . $s->id]; $sectiontext = $msg->translation(); } // Use the original text if no translation is available. // For the source language, this will actually be the source, which // contains variable declarations (tvar) instead of variables ($1). // The getTextWithVariables will convert declarations to normal variables // for us so that the variable substitutions below will also work // for the source language. if ($sectiontext === null || $sectiontext === $s->getText()) { $sectiontext = $s->getTextWithVariables(); } // Substitute variables into section text and substitute text into document $sectiontext = strtr($sectiontext, $s->getVariables()); $text = str_replace($ph, $sectiontext, $text); } $nph = array(); $text = TranslatablePage::armourNowiki($nph, $text); // Remove translation markup from the template to produce final text $cb = array(__CLASS__, 'replaceTagCb'); $text = preg_replace_callback('~(<translate>)(.*)(</translate>)~sU', $cb, $text); $text = TranslatablePage::unArmourNowiki($nph, $text); return $text; }