I'm trying to add a feature of multi language to my application using API Plateform and ReactJS. I've installed StofDoctrineExtensionsBundle, I want to use the extension Translatable. I send the local("EN" or "FR" etc) then I want to send response swtich the local.
use Gedmo\Translatable\Translatable;
/**
* @ApiResource()
* @ORM\Entity(repositoryClass=CountryRepository::class)
*/
class Country implements Translatable
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Gedmo\Translatable
* @ORM\Column(type="string", length=255)
*/
private $name;
services.yml
App\EventSubscriber\LocaleSubscriber:
arguments: ['%kernel.default_locale%']
LocaleSubscriber.php
<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class LocaleSubscriber implements EventSubscriberInterface
{
private $defaultLocale;
public function __construct(string $defaultLocale = 'en')
{
$this->defaultLocale = $defaultLocale;
}
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
// try to see if the locale has been set as a _locale routing parameter
if ($locale = $request->attributes->get('_locale')) {
$request->getSession()->set('_locale', $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 [
// must be registered before (i.e. with a higher priority than) the default Locale listener
KernelEvents::REQUEST => [['onKernelRequest', 20]],
];
}
}
In the response of the webservice which allows me to get the list of countries there is no name field.
How can I get the names of the countries switch the language ? Thanks.