New in Symfony 7.2: Improved Translation Extractor
October 28, 2024 • Published by Javier Eguiluz
Symfony 7.2 is backed by:
Symfony provides the translation:extract
command to extract all
translatable content from Twig templates and PHP code (e.g. forms):
1 2 3 4 5 6 7
# 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.
Empty Pending Translations
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:
1 2 3 4
<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. Alternatively, you can leave the pending translation completely empty
when creating new entries in the translation catalog.
To do so, use the new --no-fill
option introduced in Symfony 7.2:
1
$ php bin/console translation:extract --force --no-fill ko
When adding the --no-fill
option, the new entry will look like this:
1 2 3 4
<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:
1
$ 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:
1 2
$ php bin/console translation:extract --force --sort=desc tr
$ php bin/console translation:extract --force --sort=asc ko
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.
As per XLIFF documentation (https://docs.oasis-open.org/xliff/xliff-core/v2.1/xliff-core-v2.1.html) the state of the translation should be expressed as a "state" attribute for which the possible values are:
Example:
That's good to know. Thanks for sharing this Renaud!
However, XLIFF is only one of the many formats supported by this command, so leaving the pending translation empty might be useful in other formats too.