New in Symfony 7.2: Improved Translation Extractor
Symfony provides the translation:extract command to extract all
translatable content from Twig templates and PHP code (e.g. forms):
# extracts all content that should be translated into Turkish
# and outputs them, but it doesn’t update the translation catalog
$ php bin/console translation:extract –dump-messages tr
# updates the Korean translation catalog with all missing content
# compared to the application’s translatable content
$ php bin/console translation:extract –force ko
In Symfony 7.2 we’re enhancing this command with new options.
Custom Prefix and Update Behavior
By default, when the translation:extract command finds new content, it
creates a new entry for translation using the same content as both the source
and the pending translation. For example, if your application uses the XLIFF
format for translations and templates use translation keys instead of real
content, you’ll see entries like this in the translation catalog:
<trans-unit id=„IsTxDdZ“ resname=„notification.comment_created“>
<source>notification.comment_created</source>
<target>__notification.comment_created</target>
</trans-unit>
The __ prefix is added to help you quickly spot untranslated content in your
application. In Symfony 7.2, you can customize this prefix with the new –prefix option:
$ php bin/console translation:extract –force –prefix=„**MISSING**“ ko
Alternatively, you can leave the pending translation completely empty when
creating new entries in the translation catalog:
$ php bin/console translation:extract –force –no-fill ko
When adding the –no-fill option, the new entry will look like this:
<trans-unit id=„IsTxDdZ“ resname=„notification.comment_created“>
<source>notification.comment_created</source>
<target></target>
</trans-unit>
Custom Sorting of Translation Contents
The translation:extract command includes a –sort option to display the
translated content sorted alphabetically (ASC or DESC) when showing messages
in the terminal:
$ php bin/console translation:extract –dump-messages –sort=desc tr
In Symfony 7.2, we’re enhancing this option to also apply to translation
catalog files. When using it in combination with the –force option, you
can now sort the content of the translation catalog alphabetically:
$ php bin/console translation:extract –force –sort=desc tr
$ php bin/console translation:extract –force –sort=asc ko
Symfony Blog