【问题标题】:Symfony 2: Static function [JMSTranslation]Symfony 2:静态函数 [JMSTranslation]
【发布时间】:2014-09-08 19:15:51
【问题描述】:

我目前正在开发一个项目,该项目是以前的开发人员与 JMSTranslationBundle 集成的。 此时,我对应用程序进行了一些修改,其中之一是将菜单设置为高度动态的。 (基本上,应用程序的用户逻辑有 3 层,每一层都有自己的菜单)。

菜单存储在数据库中,可通过学说实体访问。为了显示标签,我将 JMSTranslationBundle 使用的“标签代码”作为识别它的键存储到数据库中。 desc 默认为空,直到设置到翻译文件中。 (可使用 _trans 路线进行编辑)。

在JMS的文档中,提到可以实现TranslationContainerInterface,所以当翻译文件(目前是XLIFF文件)编译完成时,每个实现它的类都会被调用以返回一个Message对象列表。这是我的问题:

要实现的功能是静态的,这意味着在调用时,我的模型菜单(处理通过 Doctrine repo 获取的逻辑)不会通过服务管理器加载。这意味着我没有收到存储库对象(因为它是由服务加载并通过控制器):

public function __construct(MenuRepository $objMenuRepo)...

我实现的函数的定义是:

static function getTranslationMessages(){ ... }

我的问题是:如何在该静态函数中获取学说(管理器或存储库)。 (因为这只会在翻译初始生成时调用,而不是由站点本身调用,所以性能不是我担心的问题)。

另外:如果有人有更好的替代方案来提议(这不会涉及摆脱这个翻译包,相信我,现在需要相当长的时间),我很乐意听到他们的意见。

谢谢你:-)

【问题讨论】:

    标签: symfony static translation


    【解决方案1】:

    如果你们中的一些人感兴趣,我不得不使用替代解决方案。

    虽然它没有回答有关如何在静态上下文中使用服务的问题,但它会帮助那些在尝试使用 JMSTranslation 实现时遇到相同问题的人。

    为了实现解决方案(从数据库中提取翻译密钥),我必须使用 JMS\TranslationBundle\Translation\ExtractorInterface。 我已经按照这种格式实现了它:

    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()
    }
    

    如您所见,extract 函数返回一个 \JMS\TranslationBundle\Model\Message 数组。 实现此功能后,您必须将对象添加为服务并使其可被 JMSTranslationBundle 识别为提取器。这样做:

     <!-- 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>
    

    别名标签在 JMSTranslationBundle 中用于将您的类识别为提取器。

    最后,在生成文件时,我必须启用提取器。这可以通过配置完成,但在我的例子中,是通过命令行手动完成的

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

    希望我没有忘记任何步骤(如果有,请随时在评论中回复,我会更新答案)。

    编码愉快 :-)

    【讨论】:

      【解决方案2】:

      使用这个输入,我最终编写了这个版本的提取器。

      <?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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多