Browse Source

some additional features and tests

everzet 13 years ago
parent
commit
426551056f

+ 18 - 4
lib/Gedmo/Translator/TranslationProxy.php

@@ -13,7 +13,7 @@ use Doctrine\Common\Collections\Collection;
  */
 class TranslationProxy
 {
-    protected $locale;
+    private $locale;
     private $translatable;
     private $properties = array();
     private $class;
@@ -96,7 +96,17 @@ class TranslationProxy
             return $this->setTranslatedValue($property, $value);
         }
 
-        $this->translatable->property = $value;
+        $this->translatable->$property = $value;
+    }
+
+    /**
+     * Returns locale name for the current translation proxy instance.
+     *
+     * @return  string
+     */
+    public function getProxyLocale()
+    {
+        return $this->locale;
     }
 
     /**
@@ -108,7 +118,9 @@ class TranslationProxy
      */
     public function getTranslatedValue($property)
     {
-        return $this->findOrCreateTranslationForProperty($property, $this->locale)->getValue();
+        return $this
+            ->findOrCreateTranslationForProperty($property, $this->getProxyLocale())
+            ->getValue();
     }
 
     /**
@@ -119,7 +131,9 @@ class TranslationProxy
      */
     public function setTranslatedValue($property, $value)
     {
-        $this->findOrCreateTranslationForProperty($property, $this->locale)->setValue($value);
+        $this
+            ->findOrCreateTranslationForProperty($property, $this->getProxyLocale())
+            ->setValue($value);
     }
 
     /**

+ 2 - 2
tests/Gedmo/Translator/Fixture/Person.php

@@ -21,12 +21,12 @@ class Person
     /**
      * @ORM\Column(name="name", type="string", length=128)
      */
-    private $name;
+    public $name;
 
     /**
      * @ORM\Column(name="desc", type="string", length=128)
      */
-    private $description;
+    public $description;
 
     public function getId()
     {

+ 14 - 0
tests/Gedmo/Translator/TranslatableTest.php

@@ -86,6 +86,20 @@ class TranslatableTest extends BaseTestCaseORM
         $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription());
     }
 
+    public function testTranslatableWithMagicProperties()
+    {
+        $person = new Person();
+        $person->setName('Jen');
+        $person->translate('ru_RU')->name = 'Женя';
+        $person->translate('ru_RU')->description = 'multilingual description';
+
+        $this->assertSame('Jen', $person->name);
+        $this->assertSame('Jen', $person->translate()->name);
+        $this->assertSame('Женя', $person->translate('ru_RU')->name);
+        $this->assertSame('multilingual description', $person->translate('ru_RU')->description);
+        $this->assertSame('multilingual description', $person->description);
+    }
+
     public function testTranslatableWithCustomProxy()
     {
         $person = new PersonCustom();