Beispiel #1
0
 /**
  *
  * $data[key][locale]
  * {
  *   message,
  *   updatedAt,
  *   bundle,
  *   fileName,
  * }
  *
  */
 protected function receiveKeys(Project $project, $catalog, $data)
 {
     if (!$project || !$catalog || !$data) {
         return $this->exception('Validation exceptions, missing parameters');
     }
     $result = array();
     /** @var Translation[] $messages */
     $messages = $this->getTranslationRepository()->findBy(array('projectId' => $project->getId(), 'catalog' => $catalog));
     if ($this->debug) {
         echo sprintf("found %d in translations\n", count($messages));
     }
     foreach ($messages as $message) {
         $key = $message->getKey();
         $bundle = '';
         $translations = $message->getTranslations();
         $dirty = false;
         if (count($translations)) {
             foreach ($translations as $locale => $translation) {
                 if (isset($data[$key][$locale])) {
                     $current = $data[$key][$locale];
                     $updatedAt = new \DateTime($current['updatedAt']);
                     if ($message->getUpdatedAt()->sec < intval($updatedAt->format("U"))) {
                         $result[$key][$locale] = $current['updatedAt'];
                         $translations[$locale]['message'] = $current['message'];
                         $translations[$locale]['updatedAt'] = $updatedAt;
                         if (isset($current['approved'])) {
                             $translations[$locale]['approved'] = $current['approved'];
                         }
                         $dirty = true;
                     }
                     $translations[$locale]['fileName'] = $current['fileName'];
                     if (!$bundle) {
                         if ($current['bundle']) {
                             $bundle = $current['bundle'];
                         } else {
                             preg_match('/\\/(?<bundle>\\w+Bundle)\\//i', $current['fileName'], $matches);
                             if (isset($matches['bundle'])) {
                                 $bundle = $matches['bundle'];
                             }
                         }
                     }
                     unset($data[$key][$locale]);
                 }
             }
             if ($dirty) {
                 $message->setBundle($bundle);
                 $message->setTranslations($translations);
                 $this->dm->persist($message);
             }
         }
     }
     if ($this->debug) {
         echo sprintf("found %d keys in data\n", count($data));
     }
     foreach ($data as $key => $dataLocale) {
         if (count($dataLocale)) {
             if ($this->debug) {
                 echo sprintf("processing key %s\n", $key);
             }
             $bundle = '';
             $translation = new Translation();
             $translation->setCatalog($catalog);
             $translation->setKey($key);
             $translation->setProjectId($project->getId());
             $translation->setBundle($bundle);
             $translations = array();
             foreach ($dataLocale as $locale => $message) {
                 $translations[$locale] = array('message' => $message['message'], 'updatedAt' => new \DateTime($message['updatedAt']), 'approved' => true, 'fileName' => $message['fileName']);
                 if (!$bundle) {
                     if ($message['bundle']) {
                         $bundle = $message['bundle'];
                     } else {
                         preg_match('/\\/(?<bundle>\\w+Bundle)\\//i', $current['fileName'], $matches);
                         if (isset($matches['bundle'])) {
                             $bundle = $matches['bundle'];
                         }
                     }
                 }
             }
             $translation->setBundle($bundle);
             $translation->setTranslations($translations);
             $this->dm->persist($translation);
         }
     }
     $this->dm->flush();
     // normalize
     $this->translationsManager->regenerateProjectInfo($project->getId());
     return $this->resultOk($result);
 }
Beispiel #2
0
 /**
  * Normalize the translations array info, creating locales and deleting if case
  *
  * @param Translation $translation
  * @param array       $managedLocales
  * @param bool        $deletesIfNotExistsLocaleInManaged
  *
  * @return Translation
  */
 public function normalizeTranslation(Translation $translation, $managedLocales = array(), $deletesIfNotExistsLocaleInManaged = false)
 {
     $transArray = $translation->getTranslations();
     // normalize the translation array
     foreach ($managedLocales as $locale) {
         if (!isset($transArray[$locale])) {
             $transArray[$locale] = Translation::genTranslationItem('');
         }
     }
     // deletes message if locale do not exists yet in managed locales
     if ($deletesIfNotExistsLocaleInManaged) {
         foreach ($transArray as $locale => $data) {
             if (!in_array($locale, $managedLocales)) {
                 unset($transArray[$locale]);
             }
         }
     }
     $translation->setTranslations($transArray);
     return $translation;
 }