Skip to content

Instantly share code, notes, and snippets.

@istvankis
Created April 30, 2016 11:39
Show Gist options
  • Save istvankis/0010cf0e318258146d4f203007aaf1e2 to your computer and use it in GitHub Desktop.
Save istvankis/0010cf0e318258146d4f203007aaf1e2 to your computer and use it in GitHub Desktop.
AppBundle\Entity\Lang:
lang_0:
lang:
en: French
fr: Français
lang_1:
lang:
en: English
fr: Anglais
alice.processor.translatable:
class: AppBundle\DataFixtures\Processor\TranslatableProcessor
arguments:
- '@doctrine.orm.entity_manager'
- '@gedmo.listener.translatable'
tags: [ { name: hautelook_alice.alice.processor } ]
<?php
namespace AppBundle\DataFixtures\Processor;
use Doctrine\ORM\EntityManager;
use Gedmo\Tool\Wrapper\AbstractWrapper;
use Gedmo\Translatable\Translatable;
use Gedmo\Translatable\TranslatableListener;
use Nelmio\Alice\ProcessorInterface;
class TranslatableProcessor implements ProcessorInterface
{
/**
* @var EntityManager
*/
private $entityManager;
/**
* @var TranslatableListener
*/
private $translatableListener;
/**
* @var Translatable
*/
private $translateRepository;
/**
* TranslatableProcessor constructor.
* @param EntityManager $entityManager
* @param TranslatableListener $translatableListener
*/
public function __construct(EntityManager $entityManager, TranslatableListener $translatableListener)
{
$this->entityManager = $entityManager;
$this->translatableListener = $translatableListener;
$this->translateRepository = $entityManager->getRepository("Gedmo\\Translatable\\Entity\\Translation");
}
public function postProcess($object)
{
}
public function preProcess($object)
{
$translationConfig = $this->translatableListener
->getConfiguration($this->entityManager, get_class($object));
if ($translationConfig && isset($translationConfig['fields'])) {
foreach ($translationConfig['fields'] as $field) {
$wrapped = AbstractWrapper::wrap($object, $this->entityManager);
$propertyValue = $wrapped->getPropertyValue($field);
if (is_array($propertyValue)) {
$wrapped->setPropertyValue($field, $propertyValue[$translationConfig['fallback'][$field]]);
foreach ($propertyValue as $local => $content) {
$this->translateRepository->translate($object, $field, $local, $content);
}
}
}
}
}
}
@pitpit
Copy link

pitpit commented Jan 27, 2021

Hi,
Have a look on this https://gist.github.com/pitpit/ce6df260fea08e61a6e5c5e7cb668d59 for a way that does not require any code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment