فهرست منبع

added a switch to enable or disable translation fallback to original record

gediminasm 14 سال پیش
والد
کامیت
8494e26d93
2فایلهای تغییر یافته به همراه84 افزوده شده و 30 حذف شده
  1. 34 8
      lib/Gedmo/Translatable/TranslationListener.php
  2. 50 22
      tests/Gedmo/Translatable/TranslatableTest.php

+ 34 - 8
lib/Gedmo/Translatable/TranslationListener.php

@@ -59,6 +59,15 @@ class TranslationListener extends MappedEventSubscriber implements EventSubscrib
      */
     private $_defaultLocale = '';
     
+    /**
+     * If this is set to false, when if entity does
+     * not have a translation for requested locale
+     * it will show a blank value
+     * 
+     * @var boolean
+     */
+    private $_translationFallback = true;
+    
     /**
      * List of translations which do not have the foreign
      * key generated yet - MySQL case. These translations
@@ -94,6 +103,18 @@ class TranslationListener extends MappedEventSubscriber implements EventSubscrib
         return __NAMESPACE__;
     }
     
+    /**
+     * Enable or disable translation fallback
+     * to original record value
+     * 
+     * @param boolean $bool
+     * @return void
+     */
+    public function setTranslationFallback($bool)
+    {
+        $this->_translationFallback = (bool)$bool;
+    }
+    
     /**
      * Get the entity translation class to be used
      * for the entity $class
@@ -272,17 +293,22 @@ class TranslationListener extends MappedEventSubscriber implements EventSubscrib
             $result = $q->getArrayResult();
             // translate entity translatable properties
             foreach ($config['fields'] as $field) {
+                $translated = '';
                 foreach ((array)$result as $entry) {
-                    if ($entry['field'] == $field && strlen($entry['content'])) {
-                        // update translation only if it has it
-                        $meta->getReflectionProperty($field)->setValue($entity, $entry['content']);
-                        $em->getUnitOfWork()->setOriginalEntityProperty(
-                            spl_object_hash($entity), 
-                            $field, 
-                            $entry['content']
-                        );
+                    if ($entry['field'] == $field) {
+                        $translated = $entry['content'];
+                        break;
                     }
                 }
+                // update translation
+                if (strlen($translated) || !$this->_translationFallback) {
+                    $meta->getReflectionProperty($field)->setValue($entity, $translated);
+                    $em->getUnitOfWork()->setOriginalEntityProperty(
+                        spl_object_hash($entity), 
+                        $field, 
+                        $translated
+                    );
+                }
             }    
         }
     }

+ 50 - 22
tests/Gedmo/Translatable/TranslatableTest.php

@@ -51,32 +51,11 @@ class TranslatableTest extends \PHPUnit_Framework_TestCase
             $this->em->getClassMetadata(self::TEST_ENTITY_CLASS_COMMENT),
             $this->em->getClassMetadata('Gedmo\Translatable\Entity\Translation'),
         ));
+        $this->populate();
     }
     
     public function testFixtureGeneratedTranslations()
     {
-        $article = new Article();
-        $article->setTitle('title in en');
-        $article->setContent('content in en');
-        
-        $comment1 = new Comment();
-        $comment1->setSubject('subject1 in en');
-        $comment1->setMessage('message1 in en');
-        
-        $comment2 = new Comment();
-        $comment2->setSubject('subject2 in en');
-        $comment2->setMessage('message2 in en');
-        
-        $article->addComment($comment1);
-        $article->addComment($comment2);
-
-        $this->em->persist($article);
-        $this->em->persist($comment1);
-        $this->em->persist($comment2);
-        $this->em->flush();
-        $this->articleId = $article->getId();
-        $this->em->clear();
-        
         $repo = $this->em->getRepository('Gedmo\Translatable\Entity\Translation');
         $this->assertTrue($repo instanceof Repository\TranslationRepository);
         
@@ -223,4 +202,53 @@ class TranslatableTest extends \PHPUnit_Framework_TestCase
         $translations = $repo->findTranslationsByEntityId($this->articleId);
         $this->assertEquals(count($translations), 0);
     }
+    
+    /**
+     * Translation fallback, related to issue #9 on github
+     */
+    public function testTranslationFallback()
+    {
+        $this->translatableListener->setTranslationFallback(false);
+        $this->translatableListener->setTranslatableLocale('ru_RU');
+        
+        $article = $this->em->find(self::TEST_ENTITY_CLASS_ARTICLE, $this->articleId);
+        $this->assertFalse((bool)$article->getTitle());
+        $this->assertFalse((bool)$article->getContent());
+        
+        foreach ($article->getComments() as $comment) {
+            $this->assertFalse((bool)$comment->getSubject());
+            $this->assertFalse((bool)$comment->getMessage());
+        }
+        $this->em->clear();
+        $this->translatableListener->setTranslationFallback(true);
+        $article = $this->em->find(self::TEST_ENTITY_CLASS_ARTICLE, $this->articleId);
+        
+        $this->assertEquals($article->getTitle(), 'title in en');
+        $this->assertEquals($article->getContent(), 'content in en');
+    }
+    
+    private function populate()
+    {
+        $article = new Article();
+        $article->setTitle('title in en');
+        $article->setContent('content in en');
+        
+        $comment1 = new Comment();
+        $comment1->setSubject('subject1 in en');
+        $comment1->setMessage('message1 in en');
+        
+        $comment2 = new Comment();
+        $comment2->setSubject('subject2 in en');
+        $comment2->setMessage('message2 in en');
+        
+        $article->addComment($comment1);
+        $article->addComment($comment2);
+
+        $this->em->persist($article);
+        $this->em->persist($comment1);
+        $this->em->persist($comment2);
+        $this->em->flush();
+        $this->articleId = $article->getId();
+        $this->em->clear();
+    }
 }