core: translatable not working on item operations
I am using StofDoctrineExtensionsBundle to translate entities. While it is working perfectly on collection operations, it fails on item operations and the returned fields are always in default locale. I use an event subscriber to set the locale:
<?php
namespace MWS\UserBundle\EventSubscriber;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LocaleSubscriber implements EventSubscriberInterface
{
private $defaultLocale;
public function __construct($defaultLocale = 'en_US')
{
$this->defaultLocale = $defaultLocale;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
// try to see if the locale has been set as a accept-language routing parameter
if ($locale = $request->headers->get('accept-language')) {
$request->getSession()->set('_locale', $locale);
$request->setLocale($locale);
} else {
// if no explicit locale has been set on this request, use one from the session
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
}
public static function getSubscribedEvents()
{
return array(
// must be registered after the default Locale listener
KernelEvents::REQUEST => array(array('onKernelRequest', 15)),
);
}
}
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 27 (11 by maintainers)
Today I can come up with a solution. The answer lies not in a custom action but in a custom extension described here. You have to set hints for the query described in the documentation of gedmo translatable. This is my implementation:
Adding hints to the collection request is not necessary, but it gives you one database query for the list view instead of multiple queries depending on your pagination size.
Thanks, @remoteclient, furthermore I had a caching problem with the query in production environment, and I could solve it using the following statement:
$queryBuilder = $queryBuilder->getQuery()->useQueryCache(false);thank you very much , this worked for me
It is worth an entry in the StofDoctrineExtensionsBundle’s or API Platform docs.
It’s working 👍 Thanks a lot!
I have a custom Provider for that. You can make the support function more generic:
I managed to make it work by setting
fetch: 'EXTRA_LAZY'to the attribute.Indeed, translations don’t work on nested properties.
However, I may have found a solution (maybe wrong, but still):
I was implementing the getResult method from QueryResultItemExtensionInterface.
If you want to fetch a collection of entities in a single request, just set a negative priority in service.yaml (yeah, I know it’s discouraged in the official documentation, but I’m the bad guy):
Should work. The code was run on symfony 5.4 lts, api platform - v2.6.8
Thanks @remoteclient. But sadly it is not working w/ nested properties, i.e. w/ serialization groups.
Thanks, i have no my own translatable extension for api-platform. 😃
Would be good enough if we had an entry in apiplatform docs. I’d put it under https://api-platform.com/docs/core/extensions
@remoteclient works like a charm!! Thank you very much.
@ACC-Txomin please leave a message if this works for you too. Then I can close this issue.