From 0135ac63a168e736e68abacf9aeccc618c146dcd Mon Sep 17 00:00:00 2001 From: Kirill Hainovsky Date: Tue, 27 Mar 2018 14:29:23 +0300 Subject: [PATCH] Added modified flag in order to fix merge algorithm (#261) Renamed property Added modifiedManually property mapping for Propel and Document Code style fix --- Manager/TransUnitManager.php | 25 +++++++++++++------ Manager/TransUnitManagerInterface.php | 3 +-- Model/Translation.php | 25 +++++++++++++++++-- Resources/config/doctrine/Translation.orm.xml | 2 +- .../config/model/Translation.mongodb.xml | 2 ++ Resources/config/propel/schema.xml | 1 + Translation/Importer/FileImporter.php | 22 +++++++++------- 7 files changed, 58 insertions(+), 22 deletions(-) diff --git a/Manager/TransUnitManager.php b/Manager/TransUnitManager.php index 34241aa8..02f7f377 100644 --- a/Manager/TransUnitManager.php +++ b/Manager/TransUnitManager.php @@ -113,7 +113,7 @@ public function addTranslation(TransUnitInterface $transUnit, $locale, $content, /** * {@inheritdoc} */ - public function updateTranslation(TransUnitInterface $transUnit, $locale, $content, $flush = false, $merge = false, \DateTime $modifiedOn = null) + public function updateTranslation(TransUnitInterface $transUnit, $locale, $content, $flush = false, $merge = false) { $translation = null; $i = 0; @@ -129,10 +129,7 @@ public function updateTranslation(TransUnitInterface $transUnit, $locale, $conte /* @var Translation $translation */ $translation = $transUnit->getTranslations()->get($i - 1); if ($merge) { - if ($translation->getContent() == $content) { - return null; - } - if ($translation->getCreatedAt() != $translation->getUpdatedAt() && (!$modifiedOn || $translation->getUpdatedAt() > $modifiedOn)) { + if ($translation->isModifiedManually() || $translation->getContent() == $content) { return null; } @@ -144,6 +141,7 @@ public function updateTranslation(TransUnitInterface $transUnit, $locale, $conte $this->storage->persist($newTranslation); $translation = $newTranslation; } + $translation->setContent($content); } @@ -165,8 +163,15 @@ public function updateTranslationsContent(TransUnitInterface $transUnit, array $ { foreach ($translations as $locale => $content) { if (!empty($content)) { - if ($transUnit->hasTranslation($locale)) { - $this->updateTranslation($transUnit, $locale, $content); + /** @var TranslationInterface|null $translation */ + $translation = $transUnit->getTranslation($locale); + $contentUpdated = true; + + if ($translation instanceof TranslationInterface) { + $originalContent = $translation->getContent(); + $translation = $this->updateTranslation($transUnit, $locale, $content); + + $contentUpdated = ($translation->getContent() != $originalContent); if ($this->storage instanceof PropelStorage) { $this->storage->persist($transUnit); @@ -174,7 +179,11 @@ public function updateTranslationsContent(TransUnitInterface $transUnit, array $ } else { //We need to get a proper file for this translation $file = $this->getTranslationFile($transUnit, $locale); - $this->addTranslation($transUnit, $locale, $content, $file); + $translation = $this->addTranslation($transUnit, $locale, $content, $file); + } + + if ($translation instanceof Translation && $contentUpdated) { + $translation->setModifiedManually(true); } } } diff --git a/Manager/TransUnitManagerInterface.php b/Manager/TransUnitManagerInterface.php index bb6a1748..d0899955 100644 --- a/Manager/TransUnitManagerInterface.php +++ b/Manager/TransUnitManagerInterface.php @@ -47,10 +47,9 @@ public function addTranslation(TransUnitInterface $transUnit, $locale, $content, * @param string $content * @param boolean $flush * @param boolean $merge - * @param \DateTime|null $modifiedOn * @return TranslationInterface */ - public function updateTranslation(TransUnitInterface $transUnit, $locale, $content, $flush = false, $merge = false, \DateTime $modifiedOn = null); + public function updateTranslation(TransUnitInterface $transUnit, $locale, $content, $flush = false, $merge = false); /** * Update the content of each translations for the given trans unit. diff --git a/Model/Translation.php b/Model/Translation.php index eb128d8c..7e96ff3d 100644 --- a/Model/Translation.php +++ b/Model/Translation.php @@ -40,6 +40,11 @@ abstract class Translation */ protected $updatedAt; + /** + * @var boolean + */ + protected $modifiedManually = false; + /** * Set locale * @@ -64,7 +69,7 @@ public function getLocale() /** * Set content * - * @param text $content + * @param string $content */ public function setContent($content) { @@ -74,7 +79,7 @@ public function setContent($content) /** * Get content * - * @return text + * @return string */ public function getContent() { @@ -120,4 +125,20 @@ public function getUpdatedAt() { return $this->updatedAt; } + + /** + * @return bool + */ + public function isModifiedManually() + { + return $this->modifiedManually; + } + + /** + * @param bool $modifiedManually + */ + public function setModifiedManually($modifiedManually) + { + $this->modifiedManually = $modifiedManually; + } } diff --git a/Resources/config/doctrine/Translation.orm.xml b/Resources/config/doctrine/Translation.orm.xml index 333d584f..0af94293 100644 --- a/Resources/config/doctrine/Translation.orm.xml +++ b/Resources/config/doctrine/Translation.orm.xml @@ -20,7 +20,7 @@ - + diff --git a/Resources/config/model/Translation.mongodb.xml b/Resources/config/model/Translation.mongodb.xml index 34f66004..2b8ec3c0 100644 --- a/Resources/config/model/Translation.mongodb.xml +++ b/Resources/config/model/Translation.mongodb.xml @@ -8,6 +8,8 @@ + + diff --git a/Resources/config/propel/schema.xml b/Resources/config/propel/schema.xml index b46c369e..b3db04fd 100755 --- a/Resources/config/propel/schema.xml +++ b/Resources/config/propel/schema.xml @@ -35,6 +35,7 @@ + diff --git a/Translation/Importer/FileImporter.php b/Translation/Importer/FileImporter.php index c48e63bd..d29c0c54 100644 --- a/Translation/Importer/FileImporter.php +++ b/Translation/Importer/FileImporter.php @@ -2,6 +2,7 @@ namespace Lexik\Bundle\TranslationBundle\Translation\Importer; +use Lexik\Bundle\TranslationBundle\Entity\Translation; use Lexik\Bundle\TranslationBundle\Storage\StorageInterface; use Lexik\Bundle\TranslationBundle\Document\TransUnit as TransUnitDocument; use Lexik\Bundle\TranslationBundle\Manager\FileManagerInterface; @@ -122,18 +123,21 @@ public function import(\Symfony\Component\Finder\SplFileInfo $file, $forceUpdate $transUnit = $this->transUnitManager->create($key, $domain); } - $translation = $this->transUnitManager->addTranslation($transUnit, $locale, $content, $translationFile); - if ($translation instanceof TranslationInterface) { - $imported++; - } elseif ($forceUpdate) { - $translation = $this->transUnitManager->updateTranslation($transUnit, $locale, $content); - $imported++; - } elseif ($merge) { - $translation = $this->transUnitManager->updateTranslation($transUnit, $locale, $content, false, true); + $translation = $this->transUnitManager->addTranslation($transUnit, $locale, $content, $translationFile); if ($translation instanceof TranslationInterface) { $imported++; + } else if($forceUpdate) { + $translation = $this->transUnitManager->updateTranslation($transUnit, $locale, $content); + if ($translation instanceof Translation) { + $translation->setModifiedManually(false); + } + $imported++; + } else if($merge) { + $translation = $this->transUnitManager->updateTranslation($transUnit, $locale, $content, false, true); + if ($translation instanceof TranslationInterface) { + $imported++; + } } - } $keys[] = $normalizedKey;