SymfonyWorld Online 2021 Winter Edition December 9 – 10, 2021 100% Online 25 talks and 10 workshops

New in Symfony 4.3: Extracting Translation Contents from PHP Files

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:

# 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:

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.


Thank you for this contribution !
That's really good to hear ! Thanks for this addition ;)
Awesome work 👏 Thanks @yceruto
Excellent! Thanks very much
Nice ! Good job !
Wow this is awesome!
Thanks 👏👏
That's really nice, thank you!
Thanks :)
Thanks !

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.