whitebear
whitebear

Reputation: 12433

Entity translation system on symfony

I am planning to integrate translation function on symfony2.

What I want to do is entity translation , not menu or somewhere.

like

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.

I would like to put 'Book(english)','libro(spanish)','本(libro)' in $name property.

and put these three words into this property on sonata admin bundle panel.

How can I integrate it??

Where should I start and which bundles??

sonata-admin/translation-bundle

or

StofDoctrineExtensionsBundle

??

Upvotes: 1

Views: 706

Answers (1)

ihsan
ihsan

Reputation: 2289

I used http://jmsyst.com/bundles/JMSTranslationBundle version 1.3.1.

Create a service tagged jms_translation.file_visitor:

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

And then create the extractor file:

<?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) { }
}

And in entity files, I can extract elements like that:

<?php

namespace MyBundle\Entity;

class MyEntity {
    public static function getStatus()
    {
        return [
            'a-hadir-tepat' => 'status.kehadiran.hadir.tepat',
            'b-hadir-telat' => 'status.kehadiran.hadir.telat',
            'c-alpa' => 'status.kehadiran.alpa',
            'd-izin' => 'status.kehadiran.izin',
            'e-sakit' => 'status.kehadiran.sakit',
        ];
    }
}

That should give you the idea!

Upvotes: 1

Related Questions