New in Symfony 4.3: Extracting Translation Contents from PHP Files
![Avatar of Javier Eguiluz](https://connect.symfony.com/api/images/17170f22-52c0-4adf-a75d-29cb01d53176.png?format=28x28)
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
Contributed by
Yonel Ceruto
in #30120.
The most time-consuming tasks when translating an application is to extract
all the template contents to be translated and to keep all the translation files
in sync. Symfony includes a command called translation:update
that helps you
with these tasks:
1 2
# updates the French translation with the missing strings found in templates/
$ php bin/console translation:update --dump-messages --force fr
As explained in the docs, the main limitation of this command is that it can only extract contents from templates. In Symfony 4.3, we've improved this command to also extract translation contents from PHP files, such as controllers and services.
Specifically, the command now introspects any PHP file/class defined as service which also injects (or autowires) the Symfony translator service. Consider for example this controller defined as a service:
1 2 3 4 5 6 7 8 9 10 11
use Symfony\Component\Translation\TranslatorInterface;
class SomeController extends AbstractController
{
public function someAction(TranslatorInterface $translator)
{
$message = $translator->trans('some_message_key');
// ...
}
}
In Symfony 4.3, the some_message_key
ID will be extracted when running the
translation:update
command, whereas in previous Symfony versions it was
ignored.
Related to this change, we've also improved the extraction logic for PHP (Pull Request 31249), QT (Pull Request 31248) and PO (Pull Request 30909) files to include the file path and line number where the extracted strings were originally found.
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments
![Avatar of Ghost](https://connect.symfony.com/api/images/6d0e0568-d191-4f5a-a5d5-21b1920dd99a.png?format=48x48)
![Avatar of Romain Gautier](https://connect.symfony.com/api/images/170ecba0-f447-4609-9212-23e39ec8dd7d.png?format=48x48)
![Avatar of Tristan Bessoussa](https://connect.symfony.com/api/images/cbf1746a-94bc-4ca0-92d0-cb7648b0e417.png?format=48x48)
![Avatar of Maxime Steinhausser](https://connect.symfony.com/api/images/fa7426b1-0649-4b18-b021-86acc5c5d525.png?format=48x48)
![Avatar of Jorge Vahldick](https://connect.symfony.com/api/images/0f53e9a6-e34a-4fd2-b926-591d8f78bb82.png?format=48x48)
![Avatar of Nicolas Dewez](https://connect.symfony.com/api/images/62b253f0-c84a-4b24-b268-3ae6f6cb75d2.png?format=48x48)
![Avatar of Alex Rock](https://connect.symfony.com/api/images/930baf4a-adf6-4c18-8055-6a8449281b64.png?format=48x48)
![Avatar of Ahmed TAILOULOUTE](https://connect.symfony.com/api/images/58305db6-5d60-478f-af87-d70335d6fda7.png?format=48x48)
![Avatar of Thomas Royer](https://connect.symfony.com/api/images/a1cb3424-9fe4-4742-9f53-e7b0b77a854e.png?format=48x48)
![Avatar of Sylvain Deloux](https://connect.symfony.com/api/images/35754b67-3401-4082-ab54-487317fb5987.png?format=48x48)
![Avatar of Mathieu MARCHOIS](https://connect.symfony.com/api/images/c46a842a-be65-4d09-ad87-fca2f31926ea.png?format=48x48)
![Avatar of jeremyFreeAgent — Jérémy Romey](https://connect.symfony.com/api/images/278a2fa8-64b4-4ecf-823a-f17827f0d46c.png?format=48x48)
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.