/** * * $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); }
/** * @Route("/normalize/{projectId}/{erase}", name="normalize") * @ Method("POST") * @ParamConverter("project", class="TranslationsBundle:Project", options={"id" = "projectId"}) */ public function normalizeAction(Request $request, Project $project, $erase = '') { $this->init(); // completar los documentos a los que le falten subdocumentos de traducciones //$this->translationsManager->userHasProject($this->user, $project); $permissions = $this->translationsManager->getPermissionForUserAndProject($this->user, $project); $permissions = $permissions->getPermissions(); if ($permissions['general'] != Permission::OWNER) { return $this->printResult(array('result' => false, 'reason' => 'not enough permissions to do this')); } $managedLocales = explode(',', $project->getManagedLocales()); /** @var Translation[] $translations */ $translations = $this->getTranslationRepository()->findBy(array('projectId' => $project->getId())); $normalized = array(); foreach ($translations as $translation) { $bundle = ""; $transArray = $translation->getTranslations(); foreach ($managedLocales as $locale) { if (!isset($transArray[$locale])) { $transArray[$locale] = Translation::genTranslationItem(''); $normalized[] = $translation->getKey() . "[{$locale}]"; } else { if (!$bundle && isset($transArray[$locale]['fileName']) && $transArray[$locale]['fileName']) { if (preg_match("@/(?<bundle>\\w*?Bundle)/@", $transArray[$locale]['fileName'], $match)) { $bundle = $match['bundle']; } else { if (preg_match("@/app/@", $transArray[$locale]['fileName'])) { $bundle = "app*"; } } } } } if ($bundle && !$translation->getBundle()) { $normalized[] = $translation->getKey() . " -> " . $bundle; $translation->setBundle($bundle); } $translation->setTranslations($transArray); $this->dm->persist($translation); } $this->dm->flush(); if ($erase === 'erase-duplicates') { // eliminar los documentos que no tengan translation en ingles (para borrar duplicados) foreach ($translations as $translation) { $transArray = $translation->getTranslations(); if (!$transArray['en']['message']) { print 'erasing ... ' . $translation->getId() . '<br/>'; $this->dm->remove($translation); } } $this->dm->flush(); } return $this->printResult(array('result' => true, 'normalized' => $normalized)); }
/** * saves the message into $translation[$key][$locale][$message] and normalize rest of translations of this key * * @param Project $project * @param $criteria * @param $key * @param $locale * @param $message * * @return Translation */ public function putTranslation(Project $project, $criteria, $key, $locale, $message) { // first get the record if (strpos($criteria, "Bundle") != false) { $translation = $this->getTranslationRepository()->getTranslationByBundle($project->getId(), $criteria, $key); } else { $translation = $this->getTranslationRepository()->getTranslation($project->getId(), $criteria, $key); } if (!$translation) { return; } $managedLocales = explode(',', $project->getManagedLocales()); $translation = $this->normalizeTranslation($translation, $managedLocales); // now normalize (creates items for managed locales that not exists) $transArray = $translation->getTranslations(); $transArray[$locale] = array_merge($transArray[$locale], Translation::genTranslationItem($message)); $translation->setTranslations($transArray); // last, return return $translation; }