New in Symfony 4.3: Extracting Translation Contents from PHP Files
May 24, 2019 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
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 are closed.
To ensure that comments stay relevant, they are closed for old posts.