/**
  * Returns a multidimensional array.
  * 
  * @param Translations $translations
  * @param bool         $includeHeaders
  * @param bool         $forceArray
  *
  * @return array
  */
 private static function toArray(Translations $translations, $includeHeaders, $forceArray = false)
 {
     $pluralForm = $translations->getPluralForms();
     $pluralSize = is_array($pluralForm) ? $pluralForm[0] - 1 : null;
     $messages = [];
     if ($includeHeaders) {
         $messages[''] = ['' => [self::generateHeaders($translations)]];
     }
     foreach ($translations as $translation) {
         $context = $translation->getContext();
         $original = $translation->getOriginal();
         if (!isset($messages[$context])) {
             $messages[$context] = [];
         }
         if ($translation->hasPluralTranslations(true)) {
             $messages[$context][$original] = $translation->getPluralTranslations($pluralSize);
             array_unshift($messages[$context][$original], $translation->getTranslation());
         } elseif ($forceArray) {
             $messages[$context][$original] = [$translation->getTranslation()];
         } else {
             $messages[$context][$original] = $translation->getTranslation();
         }
     }
     return ['domain' => $translations->getDomain(), 'plural-forms' => $translations->getHeader('Plural-Forms'), 'messages' => $messages];
 }
Пример #2
0
 /**
  * {@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);
 }
Пример #3
0
 /**
  * Generates an array with all translations.
  * 
  * @param Translations $translations
  *
  * @return array
  */
 private static function buildMessages(Translations $translations)
 {
     $pluralForm = $translations->getPluralForms();
     $pluralSize = is_array($pluralForm) ? $pluralForm[0] - 1 : null;
     $messages = [];
     $context_glue = '\\u0004';
     foreach ($translations as $translation) {
         $key = ($translation->hasContext() ? $translation->getContext() . $context_glue : '') . $translation->getOriginal();
         if ($translation->hasPluralTranslations(true)) {
             $message = $translation->getPluralTranslations($pluralSize);
             array_unshift($message, $translation->getTranslation());
         } else {
             $message = [$translation->getTranslation()];
         }
         $messages[$key] = $message;
     }
     return $messages;
 }
Пример #4
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]);
         }
     }
 }