Problem with translations and api platform symfony

Viewed 498

I have a problem with translation in symfony.

I have installed "symfony/translation": "4.4.*"

My translation configuration is

framework:
default_locale: "%locale%"
translator:
    default_path: '%kernel.project_dir%/translations'
    fallbacks:
        - '%locale%'

I have an EventSubscriber called LocaleSubscriber

class LocaleSubscriber implements EventSubscriberInterface
{
private $availableLocales;
private $defaultLocale;
private $translatableListener;
protected $currentLocale;

public function __construct(
    TranslatableListener $translatableListener, 
    LocaleRepository $localeRepository)
{
    $this->translatableListener = $translatableListener;
    $this->availableLocales = $localeRepository->getAvailableLocales();
    $this->defaultLocale = $localeRepository->getDefaultLocale();
}

public static function getSubscribedEvents()
{
    return array(
        KernelEvents::REQUEST => ['onKernelRequest', EventPriorities::PRE_WRITE],
        KernelEvents::RESPONSE => ['setContentLanguage']
    );
}

public function onKernelRequest(RequestEvent $event)
{
    // Persist DefaultLocale in translation table
    $this->translatableListener->setPersistDefaultLocaleTranslation(true);

    /** @var Request $request */
    $request = $event->getRequest();
    if ($request->headers->has("X-LOCALE")) {
        $locale = $request->headers->get('X-LOCALE');
        if (in_array($locale, $this->availableLocales)) {
            $request->setLocale($locale);
        } else {
            $request->setLocale($this->defaultLocale);
        }
    } else {
        $request->setLocale($this->defaultLocale);
    }
    
    // Set currentLocale
    $this->translatableListener->setTranslatableLocale($request->getLocale());
    $this->currentLocale = $request->getLocale();
}

/**
 * @param ResponseEvent $event
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function setContentLanguage(ResponseEvent $event)
{
    $response = $event->getResponse();
    $response->headers->add(array('Content-Language' => $this->currentLocale));

    return $response;
}

}

Its set the language for all request with X-LOCALE header (I send it from angular app)

So I have a Controller for RecoverPassword

class RecoverPasswordAction
{
/**
 * @var ValidatorInterface
 */
private $validator;
/**
 * @var EntityManagerInterface
 */
private $entityManager;

/**
 * @var JWTEncoderInterface
 */
private $encoder;

/**
 * @var UserLoadService
 */
private $userLoadService;

/**
 * @var TranslatorInterface
 */
private $translator;

public function __construct(
    ValidatorInterface $validator,
    EntityManagerInterface $entityManager,
    JWTEncoderInterface $encoder,
    UserLoadService $userLoadService,
    TranslatorInterface $translator
)
{
    $this->validator = $validator;
    $this->entityManager = $entityManager;
    $this->encoder = $encoder;
    $this->userLoadService = $userLoadService;
    $this->translator = $translator;
}

public function __invoke(UserRecoverPassword $data, RequestStack $requestStack)
{
    **var_dump($this->translator->trans('symfony.great'));die;**

    $token = $requestStack->getMasterRequest()->attributes->get('token');
    $this->validator->validate($data);
    try{
        $vars = $this->encoder->decode($token);
        $user = $this->userLoadService->getUser($vars['email']);
        $this->userLoadService->recoverPasswordUser($user, $data->newPassword);
    }catch (\Exception $exception){
        return new JsonResponse(['status' => 'FAILED', 'message' => $this->translator->trans($exception->getMessage())],500);
    }
    //return new JsonResponse(['status' => 'OK', 'message' => 'The password of '.$user->getUsername().' was recover']);
    return new JsonResponse([
        'status' => 'OK', 
        'message' => $this->translator->trans(
            'user.success',
            [],
            'messages',
            $requestStack->getCurrentRequest()->getLocale()
        )
        ]);
}

}

But when I request this endpoint, I get this response string(13) "symfony.great"

However the validation errors runs ok, I don't know why. what am i missing?

0 Answers
Related