New in Symfony 4.3: Extracting Translation Contents from PHP Files

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.

New in Symfony 4.3: Extracting Translation Contents from PHP Files symfony.com/blog/new-in-symfony-4-3-extracting-translation-contents-from-php-files

Tweet this

Comments

Thanks!
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 !
Thanks!!!

Comments are closed.

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