dev-master
9999999-devSimple way to translate your entities in forms
MIT
The Requires
- php >=5.3.2
- symfony/framework-bundle >=2.3
- doctrine/orm ~2.2,>=2.2.3
- symfony/http-foundation >=2.3
- symfony/form >=2.3
by Nicolo Singer
form entity translation
Wallogit.com
2017 © Pedro Peláez
Simple way to translate your entities in forms
Simple way to translate your entities in forms., (*1)
Installation: add to you composer.json https://packagist.org/packages/tuxes3/doctrine-util-bundle, (*2)
Add to your project in your AppKernel.php:, (*3)
... new TX3\DoctrineUtilBundle\Tuxes3DoctrineUtilBundle(), ...
Annotated your entities, (*4)
... use TX3\DoctrineUtilBundle\Annotations as TX3; ... /** * @var string * @TX3\Translatable() */ protected $name; /** * @TX3\Translatable(true) */ protected $translations = array(); ...
Use it in a FormType, (*5)
...
$builder->add('translations', 'translation', array(
'label' => 'Tag Name',
));
...
A form is rendered with an input for each defined languag in config.yml:, (*6)
tuxes3_doctrine_util:
locales: [de,en]
Searching by translated field:, (*7)
...
$repo = $this->em->getRepository("XXXXBundle:Tag");
$translationRepo = $this->em->getRepository("Tuxes3DoctrineUtilBundle:Translation");
$tag = $repo->find($translationRepo->id('Tag', 'name', $name)); // 'Tag' ===> Classname of entity, 'name' ===> fieldname
...
Saving a translation in code:, (*8)
$tag = new Tag();
$tag->setName($name);
$this->em->persist($tag);
$this->em->flush(); // <-- flush is important as instead the id is not set... :/
$translation = new Translation();
$translation->setObjid($tag->getId());
$translation->setField('name');
$translation->setClass('Tag');
$translation->setContent($name);
$translation->setLocale('en');
$this->em->persist($translation);
TODO: * Search with language * more options in formtype rendering * Use Doctrine caching * ..., (*9)
Simple way to translate your entities in forms
MIT
form entity translation