Skip to content

Commit

Permalink
Added modified flag in order to fix merge algorithm (#261)
Browse files Browse the repository at this point in the history
Renamed property

Added modifiedManually property mapping for Propel and Document

Code style fix
  • Loading branch information
hainovsky authored and Gilles Gauthier committed Mar 27, 2018
1 parent 5ed8963 commit 0135ac6
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 22 deletions.
25 changes: 17 additions & 8 deletions Manager/TransUnitManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -144,6 +141,7 @@ public function updateTranslation(TransUnitInterface $transUnit, $locale, $conte
$this->storage->persist($newTranslation);
$translation = $newTranslation;
}

$translation->setContent($content);
}

Expand All @@ -165,16 +163,27 @@ 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);
}
} 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);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions Manager/TransUnitManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 23 additions & 2 deletions Model/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ abstract class Translation
*/
protected $updatedAt;

/**
* @var boolean
*/
protected $modifiedManually = false;

/**
* Set locale
*
Expand All @@ -64,7 +69,7 @@ public function getLocale()
/**
* Set content
*
* @param text $content
* @param string $content
*/
public function setContent($content)
{
Expand All @@ -74,7 +79,7 @@ public function setContent($content)
/**
* Get content
*
* @return text
* @return string
*/
public function getContent()
{
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion Resources/config/doctrine/Translation.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<id name="id" type="integer" column="id">
<generator strategy="IDENTITY"/>
</id>

<field name="modifiedManually" column="modified_manually" type="boolean" />
<many-to-one field="file" target-entity="Lexik\Bundle\TranslationBundle\Entity\File" inversed-by="translations">
<join-column name="file_id" referenced-column-name="id" />
</many-to-one>
Expand Down
2 changes: 2 additions & 0 deletions Resources/config/model/Translation.mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

<field fieldName="locale" name="locale" type="string" />

<field fieldName="modifiedManually" name="modified_manually" type="boolean" />

<field fieldName="content" name="content" type="string" />

<field fieldName="createdAt" name="created_at" type="timestamp" />
Expand Down
1 change: 1 addition & 0 deletions Resources/config/propel/schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<column name="file_id" type="integer" />
<column name="trans_unit_id" type="integer" required="true" />
<column name="locale" type="varchar" size="10" required="true" />
<column name="modified_manually" type="boolean" />
<column name="content" type="longvarchar" />

<unique>
Expand Down
22 changes: 13 additions & 9 deletions Translation/Importer/FileImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 0135ac6

Please sign in to comment.