Symfony 2: Static Function [JMSTranslation]

I am currently working on a project that a previous developer integrated with the JMSTranslationBundle. At the moment I have made some changes to the application, one of which was to make the menu very dynamic. (Basically, custom application logic has 3 layers and each layer has its own menu).

The menu is stored in a database and accessed through the doctrine object. To display the tag, I store a "tag code" in the database, which is used by the JMSTranslationBundle as a key to identify it. Desc is empty by default until installed in a translation file. (editable with the _trans route).

The JMS documentation mentions that it is possible to implement a TranslationContainerInterface, so when the translation file (which is now an XLIFF file) is compiled, each class that implements this is called to return a list of Message objects. Here's my problem:

The function to implement is static, which means that when called, my dispatch menu does not load my model menu (which handles the data retrieval logic through the Doctrine repo). This means that I am not getting the repository object (since it is loaded by the service and going through the controller):

public function __construct(MenuRepository $objMenuRepo)...

      

Definition of the implemented function I:

static function getTranslationMessages(){ ... }

      

My question is, how can I get the doctrine (manager or repository) inside this static function. (Since this will only be triggered by the initial translation generation and not by itsef, performance is not an issue I'm worried about).

Also: if anyone has a better alternative to suggest (it has nothing to do with getting rid of this set of translations, trust me, it will take quite a while now), I am open to hear them.

Thank: -)

+3


source to share


2 answers


If some of you are interested, I had to use an alternative solution.

While it does not answer the question of how to use the service in a static context, it will help those facing the same problem I had when trying to implement using JMSTranslation.

To implement the solution (to extract the translation key from the database) I had to use JMS \ TranslationBundle \ Translation \ ExtractorInterface. I am implementing it in this format:

class TranslationRepositoriesExtractor implements ExtractorInterface{
   //Loaded through the service container
   public function __construct(EntityRepository $objRepositoryNeeded);

   // Implementation of the interface ExtractorInterface.
   // Within this function, I've used the EntityRepository received in the 
   // constructor to fetch the list of keys that would be use for translating
   /**
   * @return \JMS\TranslationBundle\Model\Message[]
   */
   public function extract()
}

      

As you can see, the extract function returns an array from \ JMS \ TranslationBundle \ Model \ Message. After implementing this functionality, you must add your object as a service and make it recognizable by the JMSTranslationBundle as an extractor. For this:

 <!-- Replace the id of the service, the class path, the id of the argument and the alias 
      named you want by the value you need in your application -->
    <service id="idOrYourService" class="Path\Of\Class\TranslationRepositoriesExtractor">
        <argument type="service" id="repository.needed" />
        <tag name="jms_translation.extractor" alias="NameOfAlias" />
    </service>

      



The alias tag is used in the JMSTranslationBundle to recognize your class as an extractor.

Finally, when creating the files, I had to enable the extractor. It can be done via config, but in my case it was done manually via command line

php app/console translation:extract --enable-extractor=NameOfAlias en
// NameOfAlias is the same name as the one defined in the tag of your service

      

Hope I haven't forgotten any step (if so, feel free to answer in the comment and I'll update the answer).

Happy coding :-)

0


source


Using this input, I ended up coding this version of the extractor.



<?php
namespace MyBundle\Service;

use Doctrine\ORM\EntityManager;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Model\MessageCatalogue;
use JMS\TranslationBundle\Translation\ExtractorInterface;

/**
 * Extracts translatable strings from Doctrine entities
 *
 * @package MyBundle\Service
 */
class EntityTranslationExtractor implements ExtractorInterface
{
    /**
     * @var EntityManager
     */
    private $entityManager;

    /**
     * EntityTranslationExtractor constructor.
     *
     * @param EntityManager $entityManager
     */
    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    /**
     * @return MessageCatalogue
     */
    public function extract()
    {
        $messageCatalogue = new MessageCatalogue();

        // Sample portion of the extraction
        $translatableEntities = $this->entityManager->getRepository('MyBundle:MyEntity')->findAll();
        foreach ($translatableEntities as $entity) {
            $message = new Message($entity::class .'.'. $entity->getName(). '.name');
            $message->setDesc(ucwords($entity->getName()));
            $messageCatalogue->add($message);
        }

        return $messageCatalogue;
    }
}

      

0


source







All Articles