symfony

时间:2017-04-10 09:05:42

标签: php symfony translation sonata-admin symfony-sonata

我打算在symfony2上整合翻译功能。

我想要做的是实体翻译,而不是菜单或某处。

class MyEntity
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;



    /**
     * @ORM\Column(type="string")
     */
    private $name; <-- use some lang versions.

我想在$ name属性中加入'Book(english)','libro(spanish)','this(libro)'。

并将这三个单词放在sonata admin捆绑面板上的这个属性中。

我如何整合它??

我应该从哪里开始,哪些捆绑?

奏管理员/翻译束

StofDoctrineExtensionsBundle

...

1 个答案:

答案 0 :(得分:1)

我使用了http://jmsyst.com/bundles/JMSTranslationBundle版本1.3.1。

创建标记为OnStopping的服务:

jms_translation.file_visitor

然后创建提取器文件:

mybundle.translator.entity.extractor:
    class: MyBundle\Translator\EntityExtractor
    tags:
        - { name: jms_translation.file_visitor }

在实体文件中,我可以提取这样的元素:

<?php

namespace MyBundle\Translator;

use JMS\TranslationBundle\Model\FileSource;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Model\MessageCatalogue;
use JMS\TranslationBundle\Translation\Extractor\FileVisitorInterface;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;

class EntityExtractor implements FileVisitorInterface, NodeVisitor
{
    private $traverser;
    private $catalogue;
    private $file;

    public function __construct()
    {
        $this->traverser = new NodeTraverser();
        $this->traverser->addVisitor($this);
    }

    public function enterNode(Node $node)
    {
        if (!$node instanceof Node\Scalar\String_) {
            return;
        }

        $id = $node->value;

        if (preg_match('/.*\./', $id)) {
            $domain = 'messages';
            $message = new Message($id, $domain);
            $message->addSource(new FileSource((string) $this->file, $node->getLine()));

            $this->catalogue->add($message);
        }
    }

    public function visitPhpFile(\SplFileInfo $file, MessageCatalogue $catalogue, array $ast)
    {
        $this->file = $file;
        $this->catalogue = $catalogue;
        if ($this->file->getPathInfo()->getFilename() == 'Entity') {
            $this->traverser->traverse($ast);
        }
    }

    public function beforeTraverse(array $nodes) { }
    public function leaveNode(Node $node) { }
    public function afterTraverse(array $nodes) { }
    public function visitFile(\SplFileInfo $file, MessageCatalogue $catalogue) { }
    public function visitTwigFile(\SplFileInfo $file, MessageCatalogue $catalogue, \Twig_Node $ast) { }
}

那应该给你一个想法!