Giter Club home page Giter Club logo

doctrine-translatable-bundle's Introduction

doctrine-translatable-bundle

Build Status

Integrate the doctrine-translatable extension in Symfony2.

The documentation can be found in the Resources/doc directory.

doctrine-translatable-bundle's People

Contributors

barthy-koeln avatar jeffreymoelands avatar jvandesande avatar nielsjanssen avatar sandermarechal avatar spdionis avatar sustmi avatar virtualize avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

doctrine-translatable-bundle's Issues

Is it possible to dynamically set the currentLocale?

Hi,

Is it possible to dynamically (manually) set the currentLocale in code withoug calling a setCurrentLocale in the entity manually?
In this case; i want to serialize an entity to a json file; and foreach json file I want another language to be active in the serialize process.

The currentLocale is being set by the TranslateListener on request; but can I change the currentLocale in code too?

Missing TranslatableListener

The problem

I query entities like in the following controller action:

    /**
     * @param int $id
     * @param EntityManagerInterface $entityManager
     * @param SerializerInterface $serializer
     * @return JsonResponse
     * @Route("/project/{id}", name="project_show")
     */
    public function projectShow(int $id, EntityManagerInterface $entityManager, SerializerInterface $serializer) {
        $project = $entityManager->getRepository('App:Project')->findOneBy([
            'id' => $id
        ]);

        return new JsonResponse($serializer->serialize($project, 'json'));
    }

This returns an object of type Project with a property of translations with a value of null.

Debugging Event Listeners

The command php bin/console debug:event-dispatcher shows that the LocaleListener is active, but shows no trace of the TranslatableListener.
The profiler's "Events" section reflects and confirm those findings.

Debugging the Container

The command php bin/console debug:container shows the following:

Name Service
prezent_doctrine_translatable.driver_chain Metadata\Driver\DriverChain
prezent_doctrine_translatable.listener Prezent\Doctrine\Translatable\EventListener\TranslatableListener
prezent_doctrine_translatable.listener.locale Prezent\Doctrine\TranslatableBundle\EventListener\LocaleListener
prezent_doctrine_translatable.metadata_factory Metadata\MetadataFactory

Things I Tried

  • Configuring the listener inside my services.yaml
  • Finding the Listener classes in app/cache/[env]/ProjectContainer as you mentioned in #3 and #13 . The cache layout may have changed with Symfony 4, but both listener class names can be found using a recursive search in the cache directory.

Notes

  • I also use stof/doctrine-extensions-bundle and hence the gedmo/doctrine-extensions for TimeStampable and Softdeleteable behaviour. Could there be any clash?
  • I can post the entire entity mapping if needed. I have used the bundle in Symfony 3 projects before and truly believe that the problem does not reside there.
  • The Bundle was not automatically added to the Kernel.php file upon installation via composer. I added it manually.

Versions

Module Version
Symfony 4.1.0
php 7.2.6
prezent/doctrine-translatable 1.2.1
prezent/doctrine-translatable-bundle 1.0.5

What can I do? Thanks in advance for you help.

currentLocale not being set from Request

I may be having a similar issue to what was mentioned here: #3

Basically, currentLocale is not being automatically set from the Request for me.

I need to manually set it in my Controller for things to work:

$locale = $this->get('request')->getLocale();

$product = new Product();
$product->setCurrentLocale($locale);

If don't set it manually, I get the exception: 'No locale has been set and currentLocale is empty'

I noticed in previous issue #3 that you mentioned that both Prezent\Doctrine\TranslatableBundle\EventListener\LocaleListener and Prezent\Doctrine\Translatable\EventListener\TranslatableListener should appear in app/cache/[env]/ProjectContainer.

In my app/cache/dev/appDevDebugProjectContainer.php I see this:

    /**
     * Gets the 'prezent_doctrine_translatable.listener' service.
     *
     * This service is shared.
     * This method always returns the same instance of the service.
     *
     * @return \Prezent\Doctrine\Translatable\EventListener\TranslatableListener A Prezent\Doctrine\Translatable\EventListener\TranslatableListener instance.
     */
    protected function getPrezentDoctrineTranslatable_ListenerService()
    {
        $this->services['prezent_doctrine_translatable.listener'] = $instance = new \Prezent\Doctrine\Translatable\EventListener\TranslatableListener($this->get('prezent_doctrine_translatable.metadata_factory'));

        $instance->setCurrentLocale('en');
        $instance->setFallbackLocale('en');

        return $instance;
    }

    /**
     * Gets the 'prezent_doctrine_translatable.listener.locale' service.
     *
     * This service is shared.
     * This method always returns the same instance of the service.
     *
     * @return \Prezent\Doctrine\TranslatableBundle\EventListener\LocaleListener A Prezent\Doctrine\TranslatableBundle\EventListener\LocaleListener instance.
     */
    protected function getPrezentDoctrineTranslatable_Listener_LocaleService()
    {
        return $this->services['prezent_doctrine_translatable.listener.locale'] = new \Prezent\Doctrine\TranslatableBundle\EventListener\LocaleListener($this->get('prezent_doctrine_translatable.listener'));
    }

Below is my code for the translatable and translation entities which I am using to test this bundle. Please let me know if there is anything I might be doing wrong, or if more information is needed.

Translatable entity Product.php:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Prezent\Doctrine\Translatable\Annotation as Prezent;
use Prezent\Doctrine\Translatable\Entity\AbstractTranslatable;
use AppBundle\Entity\ProductTranslation;

/**
 * @ORM\Entity
 */
class Product extends AbstractTranslatable
{

    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Prezent\Translations(targetEntity="AppBundle\Entity\ProductTranslation")
     */
    protected $translations;

    /**
     * @Prezent\CurrentLocale
     */
    private $currentLocale;

    private $currentTranslation;

    public function __construct()
    {
        $this->translations = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    public function setCurrentLocale($locale)
    {
        $this->currentLocale = $locale;

        return $this;
    }

    public function getCurrentLocale()
    {
        return $this->currentLocale;
    }

    /**
     * Translation helper method
     */
    public function translate($locale = null)
    {
        if (null === $locale) {
            $locale = $this->currentLocale;
        }

        if (!$locale) {
            throw new \RuntimeException('No locale has been set and currentLocale is empty');
        }

        if ($this->currentTranslation && $this->currentTranslation->getLocale() === $locale) {
            return $this->currentTranslation;
        }

        if (!$translation = $this->translations->get($locale)) {
            $translation = new ProductTranslation();
            $translation->setLocale($locale);
            $this->addTranslation($translation);
        }

        $this->currentTranslation = $translation;
        return $translation;
    }

    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->translate()->setName($name);
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->translate()->getName();
    }

    /**
     * Set color
     *
     * @param string $color
     */
    public function setColor($color)
    {
        $this->translate()->setColor($color);
    }

    /**
     * Get color
     *
     * @return string 
     */
    public function getColor()
    {
        return $this->translate()->getColor();
    }

}

Translation entity ProductTranslation.php:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Prezent\Doctrine\Translatable\Annotation as Prezent;
use Prezent\Doctrine\Translatable\Entity\AbstractTranslation;

/**
 * @ORM\Entity
 */
class ProductTranslation extends AbstractTranslation
{

    /**
     * @Prezent\Translatable(targetEntity="AppBundle\Entity\Product")
     */
    protected $translatable;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $color;

    /**
     * Set name
     *
     * @param string $name
     * @return ProductTranslation
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set color
     *
     * @param string $color
     * @return ProductTranslation
     */
    public function setColor($color)
    {
        $this->color = $color;

        return $this;
    }

    /**
     * Get color
     *
     * @return string 
     */
    public function getColor()
    {
        return $this->color;
    }

}

Change to LocaleListener breaks Symfony 3.4 LTS

#27

This PR broke our 3.4 LTS app with the error

FatalThrowableError

Type error: Argument 1 passed to Prezent\Doctrine\TranslatableBundle\EventListener\LocaleListener::onKernelRequest() must be an instance of Symfony\Component\HttpKernel\RequestEvent, instance of Symfony\Component\HttpKernel\Event\GetResponseEvent given, called in ... Symfony/Component/EventDispatcher/Debug/WrappedListener.php on line 115

Yaml Driver

When i add bundle to registerBundles() method, i can not generate any entities from yaml mapping by generator, i get this error:

Cannot adapt Doctrine driver of class Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver

Incorrect composer requirements

It's currently no possible to install "prezent/doctrine-translatable-bundle 1.0.*@dev" with "minimum-stability": "stable".

It's because there is a requirement on "prezent/doctrine-translatable >=1.0" which doesn't exists as stable. Only 1.0-dev is available on packagist.

Composer error:

- Installation request for prezent/doctrine-translatable-bundle 1.0.*@dev -> satisfiable by prezent/doctrine-translatable-bundle[1.0.x-dev].
- prezent/doctrine-translatable-bundle 1.0.x-dev requires prezent/doctrine-translatable >=1.0,<2.0-dev -> no matching package found.

Should prezent release a stable version of prezent/doctrine-translatable ? Or should we not use doctrine-translatable-bundle 1.0.* ?

Symfony 3.4 / Symfony 4 / Sonata 4.x

Looks like there is an upstream function signature change in Sonata that is now bring the installation of this bundle related to using Symfony 4 and Sonata 4.x branches.

Looks like this bundle depends on Sonata in the Filter/TranslatableFilter.php

Will you be branching?

YamlDriver not found

Hi,

Thanks for your job.

I have a bug during installation:

PHP Fatal error:  Class 'Prezent\Doctrine\Translatable\Mapping\Driver\YamlDriver' not found in /Users/comur/Documents/workspace/wid.localhost.net/app/cache/dev/appDevDebugProjectContainer.php on line 517

As i can see, you put "prezent/doctrine-translatable": ">=0.1,<0.2-dev", in the bundle's requirement but 0.1 doesn't have this YamlDriver class yet so this causes a bug in service container when initializing it.

I suggest to put dev-master as prezent/doctrine-translatable requirement or create a new version of prezent/doctrine-translatable.

Thanks for your help.

Release 1.0.4 is broken (Undefined variable: container)

There is an undefined variable $container in PrezentDoctrineTranslatableExtension::loadSonata() which triggers an error when the bundle is registered: https://github.com/Prezent/doctrine-translatable-bundle/blob/1.0.4/DependencyInjection/PrezentDoctrineTranslatableExtension.php#L45

In PrezentDoctrineTranslatableExtension.php line 45:
                                                             
  [Symfony\Component\Debug\Exception\ContextErrorException]  
  Notice: Undefined variable: container  

Fixed in #21, I'm creating this issue just for transparency - so it's clear what is happening.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.