浏览代码

Merge pull request #444 from recipe/ticket_438

Fixed issue #438. [Translatable] DefaultLocale
Gediminas Morkevicius 12 年之前
父节点
当前提交
1f9334ee62
共有 2 个文件被更改,包括 58 次插入0 次删除
  1. 14 0
      lib/Gedmo/Translatable/TranslatableListener.php
  2. 44 0
      tests/Gedmo/Translatable/PersonalTranslationTest.php

+ 14 - 0
lib/Gedmo/Translatable/TranslatableListener.php

@@ -489,9 +489,14 @@ class TranslatableListener extends MappedEventSubscriber
                 continue; // locale is same and nothing changed
                 continue; // locale is same and nothing changed
             }
             }
             $translation = null;
             $translation = null;
+            $translationInDefaultLocale = null;
             // lookup persisted translations
             // lookup persisted translations
             if ($ea->usesPersonalTranslation($translationClass)) {
             if ($ea->usesPersonalTranslation($translationClass)) {
                 foreach ($ea->getScheduledObjectInsertions($uow) as $trans) {
                 foreach ($ea->getScheduledObjectInsertions($uow) as $trans) {
+                    if ($locale !== $this->defaultLocale && get_class($trans) === $translationClass &&
+                        $trans->getLocale() === $this->defaultLocale) {
+                        $translationInDefaultLocale = $trans;
+                    }
                     $wasPersistedSeparetely = get_class($trans) === $translationClass
                     $wasPersistedSeparetely = get_class($trans) === $translationClass
                         && $trans->getLocale() === $locale
                         && $trans->getLocale() === $locale
                         && $trans->getField() === $field
                         && $trans->getField() === $field
@@ -513,6 +518,7 @@ class TranslatableListener extends MappedEventSubscriber
                     $config['useObjectClass']
                     $config['useObjectClass']
                 );
                 );
             }
             }
+
             // create new translation if translation not already created and locale is differentent from default locale, otherwise, we have the date in the original record
             // create new translation if translation not already created and locale is differentent from default locale, otherwise, we have the date in the original record
             $persistNewTranslation = !$translation
             $persistNewTranslation = !$translation
                 && ($locale !== $this->defaultLocale || $this->persistDefaultLocaleTranslation)
                 && ($locale !== $this->defaultLocale || $this->persistDefaultLocaleTranslation)
@@ -552,6 +558,14 @@ class TranslatableListener extends MappedEventSubscriber
                     }
                     }
                 }
                 }
             }
             }
+
+            if ($isInsert && $translationInDefaultLocale !== null) {
+                // We can't rely on object field value which is created in non default locale.
+                // If we provide translation for default locale as well, the latter is considered to be trusted
+                // and object content should be overridden.
+                $wrapped->setPropertyValue($field, $translationInDefaultLocale->getContent());
+                $ea->recomputeSingleObjectChangeset($uow, $meta, $object);
+            }
         }
         }
         $this->translatedInLocale[$oid] = $locale;
         $this->translatedInLocale[$oid] = $locale;
         // check if we have default translation and need to reset the translation
         // check if we have default translation and need to reset the translation

+ 44 - 0
tests/Gedmo/Translatable/PersonalTranslationTest.php

@@ -127,6 +127,50 @@ class PersonalTranslationTest extends BaseTestCaseORM
         $this->assertEquals('override', $trans[0]['content']);
         $this->assertEquals('override', $trans[0]['content']);
     }
     }
 
 
+    /**
+     * Covers issue #438
+     * @test
+     */
+    function shouldPersistDefaultLocaleValue()
+    {
+        $this->translatableListener->setTranslatableLocale('de');
+        $this->translatableListener->setDefaultLocale('en');
+        $article = new Article;
+        $article->setTitle('de');
+
+        $enTranslation = new PersonalArticleTranslation;
+        $enTranslation
+            ->setField('title')
+            ->setContent('en')
+            ->setObject($article)
+            ->setLocale('en')
+        ;
+        $this->em->persist($enTranslation);
+
+        $deTranslation = new PersonalArticleTranslation;
+        $deTranslation
+            ->setField('title')
+            ->setContent('de')
+            ->setObject($article)
+            ->setLocale('de')
+        ;
+        $this->em->persist($deTranslation);
+
+        $this->em->persist($article);
+        $this->em->flush();
+
+        $this->startQueryLog();
+        $this->translatableListener->setTranslatableLocale('en');
+        $articles = $this->em->createQuery('SELECT t FROM '.self::ARTICLE.' t')->getArrayResult();
+        $sqlQueriesExecuted = $this->queryAnalyzer->getExecutedQueries();
+        $this->assertEquals('en', $articles[0]['title']);
+        $trans = $this->em->createQuery('SELECT t FROM '.self::TRANSLATION.' t')->getArrayResult();
+        $this->assertCount(2, $trans);
+        foreach ($trans as $item){
+            $this->assertEquals($item['locale'], $item['content']);
+        }
+    }
+
     /**
     /**
      * @test
      * @test
      */
      */