Vardius Vardius - 1 month ago 7
PHP Question

Translate form to custom set locale

Hi i have a use case where i want to translate form to the custom locale then that one set in request

I tried to do something like

$tmpLocale = $request->getLocale();
$request->setLocale('es');
$form = $this->createForm(new DataType());
$formView = $form->renderView();
$request->setLocale($tmpLocale);

return $this->render('AppBundle:Data:edit.html.twig', array(
'data' => $data,
'form' => $formView,
));


but it doesnt work, how can i make it work this ?
i need form labels to be translated as this custom locale.

Answer

I did it by simple change locale at translator service.

$tmpLocale = $this->get('translator')->getLocale();
$this->get('translator')->setLocale('es');
$form = $this->createForm(new DataType());
$formView = $form->createView();
$this->get('translator')->setLocale($tmpLocale);

And this works fine. No need for advanced solution.