Browse Source

[translatable] allow to store translation in default locale

gedi 13 years ago
parent
commit
58418f1640

+ 25 - 2
lib/Gedmo/Translatable/TranslatableListener.php

@@ -97,6 +97,14 @@ class TranslatableListener extends MappedEventSubscriber
      */
     private $translatedInLocale = array();
 
+    /**
+     * Wether or not, to persist default locale
+     * translation or keep it in original record
+     *
+     * @var boolean
+     */
+    private $persistDefaultLocaleTranslation = false;
+
     /**
      * Specifies the list of events to listen
      *
@@ -124,6 +132,19 @@ class TranslatableListener extends MappedEventSubscriber
         return $this;
     }
 
+    /**
+     * Wether or not, to persist default locale
+     * translation or keep it in original record
+     *
+     * @param boolean $bool
+     * @return \Gedmo\Translatable\TranslatableListener
+     */
+    public function setPersistDefaultLocaleTranslation($bool)
+    {
+        $this->persistDefaultLocaleTranslation = (bool)$bool;
+        return $this;
+    }
+
     /**
      * Add additional $translation for pending $oid object
      * which is being inserted
@@ -477,7 +498,10 @@ class TranslatableListener extends MappedEventSubscriber
                 );
             }
             // create new translation if translation not already created and locale is differentent from default locale, otherwise, we have the date in the original record
-            if (!$translation && $locale !== $this->defaultLocale) {
+            $persistNewTranslation = !$translation
+                && ($locale !== $this->defaultLocale || $this->persistDefaultLocaleTranslation)
+            ;
+            if ($persistNewTranslation) {
                 $translation = new $translationClass();
                 $translation->setLocale($locale);
                 $translation->setField($field);
@@ -487,7 +511,6 @@ class TranslatableListener extends MappedEventSubscriber
                     $translation->setObjectClass($meta->name);
                     $translation->setForeignKey($objectId);
                 }
-                $scheduleUpdate = !$isInsert;
             }
 
             if ($translation) {

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

@@ -47,6 +47,18 @@ class PersonalTranslationTest extends BaseTestCaseORM
         $this->getMockSqliteEntityManager($evm);
     }
 
+    /**
+     * @test
+     */
+    function shouldPersistDefaultLocaleTranslationIfRequired()
+    {
+        $this->translatableListener->setPersistDefaultLocaleTranslation(true);
+        $this->populate();
+        $article = $this->em->find(self::ARTICLE, array('id' => 1));
+        $translations = $article->getTranslations();
+        $this->assertEquals(3, count($translations));
+    }
+
     /**
      * @test
      */

+ 33 - 5
tests/Gedmo/Translatable/TranslatableTest.php

@@ -40,11 +40,35 @@ class TranslatableTest extends BaseTestCaseORM
         $evm->addEventSubscriber($this->translatableListener);
 
         $this->getMockSqliteEntityManager($evm);
-        $this->populate();
     }
 
-    public function testFixtureGeneratedTranslations()
+    /**
+     * @test
+     */
+    function shouldPersistDefaultLocaleTranslationIfRequired()
+    {
+        $this->translatableListener->setPersistDefaultLocaleTranslation(true);
+
+        $article = new Article();
+        $article->setTitle('title in en');
+        $article->setContent('content in en');
+
+        $this->em->persist($article);
+        $this->em->flush();
+
+        $repo = $this->em->getRepository(self::TRANSLATION);
+
+        $translations = $repo->findTranslations($article);
+        $this->assertEquals(1, count($translations));
+        $this->assertArrayHasKey('en_us', $translations);
+    }
+
+    /**
+     * @test
+     */
+    function shouldGenerateTranslations()
     {
+        $this->populate();
         $repo = $this->em->getRepository(self::TRANSLATION);
         $this->assertTrue($repo instanceof Entity\Repository\TranslationRepository);
 
@@ -163,10 +187,11 @@ class TranslatableTest extends BaseTestCaseORM
     }
 
     /**
-     * Translation fallback, related to issue #9 on github
+     * @test
      */
-    public function testTranslationFallback()
+    function shouldSolveTranslationFallbackGithubIssue9()
     {
+        $this->populate();
         $this->translatableListener->setTranslationFallback(false);
         $this->translatableListener->setTranslatableLocale('ru_RU');
 
@@ -186,7 +211,10 @@ class TranslatableTest extends BaseTestCaseORM
         $this->assertEquals($article->getContent(), 'content in en');
     }
 
-    public function testGithubIssue64()
+    /**
+     * @test
+     */
+    function shouldSolveGithubIssue64()
     {
         $judo = new Sport;
         $judo->setTitle('Judo');