瀏覽代碼

implemented translations collection translator

everzet 13 年之前
父節點
當前提交
7e3df10b00

+ 51 - 0
lib/Gedmo/Translator/Document/Translation.php

@@ -0,0 +1,51 @@
+<?php
+
+namespace Gedmo\Translator\Document;
+
+use Gedmo\Translator\Translation as BaseTranslation;
+
+use Doctrine\ODM\MongoDB\Mapping\Annotations\MappedSuperclass;
+use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
+use Doctrine\ODM\MongoDB\Mapping\Annotations\String as MongoString;
+
+/**
+* @MappedSuperclass
+*/
+abstract class Translation extends BaseTranslation
+{
+    /**
+     * @Id
+     */
+    protected $id;
+
+    /**
+     * @var string $locale
+     *
+     * @MongoString
+     */
+    protected $locale;
+
+    /**
+     * @var string $property
+     *
+     * @MongoString
+     */
+    protected $property;
+
+    /**
+     * @var text $value
+     *
+     * @MongoString
+     */
+    protected $value;
+
+    /**
+     * Get id
+     *
+     * @return integer $id
+     */
+    public function getId()
+    {
+        return $this->id;
+    }
+}

+ 56 - 0
lib/Gedmo/Translator/Entity/Translation.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace Gedmo\Translator\Entity;
+
+use Gedmo\Translator\Translation as BaseTranslation;
+
+use Doctrine\ORM\Mapping\Column;
+use Doctrine\ORM\Mapping\MappedSuperclass;
+use Doctrine\ORM\Mapping\Id;
+use Doctrine\ORM\Mapping\GeneratedValue;
+
+/**
+* @MappedSuperclass
+*/
+abstract class Translation extends BaseTranslation
+{
+    /**
+     * @var integer $id
+     *
+     * @Column(type="integer")
+     * @Id
+     * @GeneratedValue
+     */
+    protected $id;
+
+    /**
+     * @var string $locale
+     *
+     * @Column(type="string", length=8)
+     */
+    protected $locale;
+
+    /**
+     * @var string $property
+     *
+     * @Column(type="string", length=32)
+     */
+    protected $property;
+
+    /**
+     * @var text $value
+     *
+     * @Column(type="text", nullable=true)
+     */
+    protected $value;
+
+    /**
+     * Get id
+     *
+     * @return integer $id
+     */
+    public function getId()
+    {
+        return $this->id;
+    }
+}

+ 129 - 0
lib/Gedmo/Translator/ObjectTranslator.php

@@ -0,0 +1,129 @@
+<?php
+
+namespace Gedmo\Translator;
+
+use Doctrine\Common\Collections\Collection;
+
+/**
+ * TranslationsCollection
+ *
+ * @author Konstantin Kudryashov <ever.zet@gmail.com>
+ * @package Gedmo.Translator
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class ObjectTranslator
+{
+    private $translatable;
+    private $properties;
+    private $class;
+    private $coll;
+    private $defaultValues = array();
+    private $currentLocale;
+
+    /**
+     * Initializes translations collection
+     *
+     * @param   Object      $translatable   object to translate
+     * @param   array       $properties     object properties to translate
+     * @param   string      $class          translation entity|document class
+     * @param   Collection  $coll           translations collection
+     */
+    public function __construct($translatable, array $properties, $class, Collection $coll)
+    {
+        $this->translatable = $translatable;
+        $this->properties   = $properties;
+        $this->class        = $class;
+        $this->coll         = $coll;
+
+        $translatableClass = new \ReflectionObject($translatable);
+        foreach ($properties as $property) {
+            $translatableProperty = $translatableClass->getProperty($property);
+            $translatableProperty->setAccessible(true);
+            $this->defaultValues[$property] = $translatableProperty->getValue($translatable);
+        }
+    }
+
+    /**
+     * Change translatable properties of translatable entity|document to localized ones
+     *
+     * @param   string  $locale     locale (null === default)
+     */
+    public function translate($locale = null)
+    {
+        $locale = null !== $locale ? strtolower($locale) : null;
+
+        if ($locale === $this->currentLocale) {
+            return $this->translatable;
+        }
+
+        $translatableClass = new \ReflectionObject($this->translatable);
+        // iterate over translatable properties
+        foreach ($this->properties as $property) {
+            $translatableProperty = $translatableClass->getProperty($property);
+            $translatableProperty->setAccessible(true);
+
+            $value = $translatableProperty->getValue($this->translatable);
+
+            // save current locale value
+            if (null === $this->currentLocale) {
+                $this->defaultValues[$property] = $value;
+            } else {
+                $translation = $this->getTranslationForProperty($property, $this->currentLocale);
+                $translation->setValue($value);
+            }
+
+            // load new locale value
+            if (null === $locale) {
+                $value = $this->defaultValues[$property];
+            } else {
+                $translation = $this->getOrCreateTranslationForProperty($property, $locale);
+                $value = $translation->getValue();
+            }
+
+            $translatableProperty->setValue($this->translatable, $value);
+        }
+
+        $this->currentLocale = $locale;
+
+        return $this->translatable;
+    }
+
+    /**
+     * Finds or creates new translation for specified property
+     *
+     * @param   string  $property   object property name
+     * @param   string  $locale     locale name
+     *
+     * @return  Translation
+     */
+    public function getOrCreateTranslationForProperty($property, $locale)
+    {
+        if (!($translation = $this->getTranslationForProperty($property, $locale))) {
+            $translation = new $this->class;
+            $translation->setTranslatable($this->translatable);
+            $translation->setProperty($property);
+            $translation->setLocale($locale);
+            $this->coll->add($translation);
+        }
+
+        return $translation;
+    }
+
+    /**
+     * Finds translation for specified property
+     *
+     * @param   string  $property   object property name
+     * @param   string  $locale     locale name
+     *
+     * @return  Translation
+     */
+    public function getTranslationForProperty($property, $locale)
+    {
+        foreach ($this->coll as $translation) {
+            if ($locale === $translation->getLocale() && $property === $translation->getProperty()) {
+                return $translation;
+            }
+        }
+    }
+}

+ 93 - 0
lib/Gedmo/Translator/Translation.php

@@ -0,0 +1,93 @@
+<?php
+
+namespace Gedmo\Translator;
+
+abstract class Translation
+{
+    protected $translatable;
+    protected $locale;
+    protected $property;
+    protected $value;
+
+    /**
+     * Set translatable
+     *
+     * @param string $translatable
+     */
+    public function setTranslatable($translatable)
+    {
+        $this->translatable = $translatable;
+    }
+
+    /**
+     * Get translatable
+     *
+     * @return string $translatable
+     */
+    public function getTranslatable()
+    {
+        return $this->translatable;
+    }
+
+    /**
+     * Set locale
+     *
+     * @param string $locale
+     */
+    public function setLocale($locale)
+    {
+        $this->locale = $locale;
+    }
+
+    /**
+     * Get locale
+     *
+     * @return string $locale
+     */
+    public function getLocale()
+    {
+        return $this->locale;
+    }
+
+    /**
+     * Set property
+     *
+     * @param string $field
+     */
+    public function setProperty($property)
+    {
+        $this->property = $property;
+    }
+
+    /**
+     * Get property
+     *
+     * @return string $field
+     */
+    public function getProperty()
+    {
+        return $this->property;
+    }
+
+    /**
+     * Set value
+     *
+     * @param text $value
+     * @return AbstractTranslation
+     */
+    public function setValue($value)
+    {
+        $this->value = $value;
+        return $this;
+    }
+
+    /**
+     * Get value
+     *
+     * @return text $value
+     */
+    public function getValue()
+    {
+        return $this->value;
+    }
+}