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

New in Symfony 5.4: Translation Improvements

Symfony 5.4 is backed by Private Packagist. Private Packagist is a fast, reliable, and secure Composer repository for your private packages. It mirrors all your open-source dependencies for better availability and monitors them for security vulnerabilities.

Renamed the Translation Update Command

Contributed by
Mathieu Santostefano
in #43758.

The well-known translation:update command extracts translatable contents from templates, controllers, and some PHP files. It's useful to automatically create (and update) the translation files of your applications.

Although the command "updates" the translation files, its main feature is to "extract" translatable contents. That's why in Symfony 5.4 we've updated its name from translation:update to translation:extract.

You can still use the old name, but you should update it as soon as possible because the old name will no longer work in Symfony 6.0.

GitHub Actions Integration

Contributed by
Yannis Foucher
in #39828.

The lint:xliff command has added a new github output format so you can see the translation errors properly formatted in the GitHub Actions output. Use the following command to validate your translation files inside GitHub Actions:

1
$ php bin/console lint:xliff translations/ --format=github

Translatable Help Messages

Contributed by
Patrick Landolt
in #41717.

In Symfony 5.2 we introduced Translatable objects, which include not only the contents to translate but all the information needed to do so (translation parameters, translation domain, etc.)

In Symfony 5.4 we're improving Symfony Forms to allow these objects as the help message of form fields too:

1
2
3
4
5
use Symfony\Component\Translation\TranslatableMessage;

$builder->add('zipCode', null, [
    'help' => new TranslatableMessage('admin.zip_code', ['%current_zip%' => $order->getZipCode()], 'admin'),
]);

Translatable Parameters in Translatable Objects

Contributed by
kylekatarnls
in #41858.

Another improvement related to Translatable objects is that parameters can now also be translatable. This may sound confusing, but consider the following message:

1
2
3
// message 'invoice.status'
(English) 'This invoice is in "%status%" status'
(Spanish) 'El estado de la factura es "%status%"'

In addition to translating the entire message, the %status% parameter must be translated as well. In Symfony 5.4 you can solve that by making the parameter a TranslatableMessage object:

1
2
3
4
5
$message = new TranslatableMessage('invoice.status', ['%status%' => new TranslatableMessage('status.draft')]);
echo $message->trans($translator, 'en');
// This invoice is in "draft" status
echo $message->trans($translator, 'es');
// El estado de la factura es "borrador"
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.

Comments

Where does $order come from in the example?

$builder->add('zipCode', null, [
'help' => new TranslatableMessage('admin.zip_code', ['%current_zip%' => $order->getZipCode()], 'admin'),
]);

Is it injected in the construct? - or is it from the twig template it will get it?
@Martin The code snippet doesn't provide enough information to know where it comes from ... because it's not relevant to the feature shown in this section.

However, since this implies the usage of a "builder", it's probably a form defined in a class and "order" comes from the data passed to the form.
The lead text misses `w`

> In addition, translation messages no integrates with GitHub Actions.
@Alexander thanks for the heads up! The typo is fixed now.
Login with SymfonyConnect to post a comment