How to return a localized property of an entity in Symfony 2

doctrine-orm, entities, orm, php, symfony

Solution

You shouldn't do something like `propertyEn` and `propertyFr` in general.

Just store your translations together with a locale property which makes fetching them from database in a query as easy as:

// example findByLocale($locale) method

->select('entity.property')
->from('AcmeMyBundle:Entity', 'entity')   
// or inside a repository $this->createQueryBuilder('entity')

->where('entity.locale = :locale')
->setParameter('locale', $locale)

But this has all been done before ...

You should either use Gemo\DoctrineExtensions\Translatable, which can easily be integrated with symfony2 using Stof\DoctrineExtensionsBundle

... or my tip if using PHP 5.4+ with traits available KnpLabs\DoctrineBehaviors\Translatable.

In order to integrate these nicely with your forms use a2lix\TranslationFormBundle.

See my answer here for a quick insight on using DoctrineBehaviors\Translatable and the current locale proxy which i found to be a really comfortable solution.

Just create class `Entity` and `EntityTranslation`, include the proxy lines ... call `$entity->getProperty()`

-> current locale applied automatically. as easy as it can be :-)

Problem

I have a "Category" entity containing ``` /** * @ORM\Column(type="string", length=255) */ protected $nameFr; /** * @ORM\Column(type="string", length=255) */ protected $nameEn; ``` Now, I'm trying to display the localized name in the view, I can display one or the other using: ``` {{ categories.nameFr }} and {{ categories.nameEn }} ``` So I made a method called `getName()` so I can use `{{ categories.name }}` I only needed to access the locale so I added a `protected $locale` property to the entity with the setter and getter and I set the locale before calling the view (I use @Template for the return by the way): ``` $locale = $this->getRequest()->getLocale(); $categories->setLocale($locale); return array( 'categories' => $categories ); ``` This was working, but now I implemented a pagination bundle KnpLabs/KnpPaginatorBundle which requires to send a query instead of the entity: ``` $em = $this->getDoctrine()->getManager(); $categoriesQuery = $em->createQueryBuilder() ->select('category') ->from('OylexCategoryBundle:Category', 'category') ; $locale = $this->getRequest()->getLocale(); $categoriesQuery->setLocale($locale); $paginator = $this->get('knp_paginator'); $categoriesPagination = $paginator->paginate( $categoriesQuery, $this->get('request')->query->get('page', 1), 30 ); return array( 'categoriesPagination' => $categoriesPagination ); ``` This fails with the error message: `FatalErrorException: Error: Call to undefined method Doctrine\ORM\QueryBuilder::setLocale()`. If I try the method `setLocale()` on `$categoriesPagination` instead, it fails with the error message: `FatalErrorException: Error: Call to undefined method Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination::setLocale()` Is there a way to pass the locale to the entity? or Is there even a better way to handle this situation? Thanks,

Original source