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
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.
I'm really excited to see my name here :) I implemented the "--no-fill" option to address a specific need my team encountered with Weblate.
In Weblate, a string is only considered "untranslated" if the target field is empty. Although we could work around this by using the "__" prefix, there was another key reason behind implementing "--no-fill": our workflow relies on Weblate’s auto-translation plugin, which only processes genuinely untranslated (empty) strings.
With "--no-fill", we can now ensure new entries remain blank in the target field, enabling auto-translation and saving us a significant amount of time in the process.