You are here

protected function XliffFileDumper::format in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/translation/Dumper/XliffFileDumper.php \Symfony\Component\Translation\Dumper\XliffFileDumper::format()

Transforms a domain of a message catalogue to its string representation.

Parameters

MessageCatalogue $messages:

string $domain:

Return value

string representation

Overrides FileDumper::format

File

vendor/symfony/translation/Dumper/XliffFileDumper.php, line 45

Class

XliffFileDumper
XliffFileDumper generates xliff files from a message catalogue.

Namespace

Symfony\Component\Translation\Dumper

Code

protected function format(MessageCatalogue $messages, $domain) {
  $dom = new \DOMDocument('1.0', 'utf-8');
  $dom->formatOutput = true;
  $xliff = $dom
    ->appendChild($dom
    ->createElement('xliff'));
  $xliff
    ->setAttribute('version', '1.2');
  $xliff
    ->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
  $xliffFile = $xliff
    ->appendChild($dom
    ->createElement('file'));
  $xliffFile
    ->setAttribute('source-language', str_replace('_', '-', $this->defaultLocale));
  $xliffFile
    ->setAttribute('target-language', str_replace('_', '-', $messages
    ->getLocale()));
  $xliffFile
    ->setAttribute('datatype', 'plaintext');
  $xliffFile
    ->setAttribute('original', 'file.ext');
  $xliffBody = $xliffFile
    ->appendChild($dom
    ->createElement('body'));
  foreach ($messages
    ->all($domain) as $source => $target) {
    $translation = $dom
      ->createElement('trans-unit');
    $translation
      ->setAttribute('id', md5($source));
    $translation
      ->setAttribute('resname', $source);
    $s = $translation
      ->appendChild($dom
      ->createElement('source'));
    $s
      ->appendChild($dom
      ->createTextNode($source));

    // Does the target contain characters requiring a CDATA section?
    $text = 1 === preg_match('/[&<>]/', $target) ? $dom
      ->createCDATASection($target) : $dom
      ->createTextNode($target);
    $t = $translation
      ->appendChild($dom
      ->createElement('target'));
    $t
      ->appendChild($text);
    $metadata = $messages
      ->getMetadata($source, $domain);
    if (null !== $metadata && array_key_exists('notes', $metadata) && is_array($metadata['notes'])) {
      foreach ($metadata['notes'] as $note) {
        if (!isset($note['content'])) {
          continue;
        }
        $n = $translation
          ->appendChild($dom
          ->createElement('note'));
        $n
          ->appendChild($dom
          ->createTextNode($note['content']));
        if (isset($note['priority'])) {
          $n
            ->setAttribute('priority', $note['priority']);
        }
        if (isset($note['from'])) {
          $n
            ->setAttribute('from', $note['from']);
        }
      }
    }
    $xliffBody
      ->appendChild($translation);
  }
  return $dom
    ->saveXML();
}