示例#1
0
 /**
  * {@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();
 }
示例#2
0
 /**
  * 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;
 }
示例#3
0
 /**
  * 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]);
         }
     }
 }
示例#4
0
 /**
  * {@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']);
 }