John Davidson

php - Symfony Translation - Translate a PDF generated in a command

0 comments
Message:


I have a symfony command line calling an external service which inject by the dependency injection the Translator service.


In this service, I want to change the language of my generated pdf, but it is not taking into account the setting of my local and always use the fallback en


    protected function generatePdf ($data)
{
$this->translator->setLocale($data->getCustomer()->getLanguage()->getLocale());

$view = $this->environment->render('TestBundle:Pdf:test.html.twig', [
'data' => $data
]);

$path = $this->pdfPathFolder.'/'.$data->getCode().'.pdf';
$this->loggableGenerator->generateFromHtml($view, $path, []);

return $path;
}

When I get the local after the set, I get indeed my new value: it, but my pdf is still generated in English.


What can I do to translate my PDF generated in CLI? Did I miss something?



My Answer:
translator->setLocale('fr'); $translatedTitle = $this->translator->trans('pdf.title'); $translatedContent = $this->translator->trans('pdf.content', ['%data%' => $data]); // Generate PDF with translated content }In this code snippet, we are setting the locale of the translator service to 'fr' (French) before translating the title and content of the PDF. This will ensure that the generated PDF is in French instead of the default fallback language (en). Make sure to replace 'pdf.title' and 'pdf.content' with the actual translation keys in your translation files.

Rate this post

4 of 5 based on 4679 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us