New in Symfony 7.2: Translations Linter
October 18, 2024 • Published by Javier Eguiluz
Symfony 7.2 is backed by:
Linters are static code analysis tools used to flag errors like syntactic and
stylistic issues. Symfony includes many linter commands to check YAML config files
(lint:yaml
), container services (lint:container
), Twig templates
(lint:twig
) and even the syntax of XLIFF translation files (lint:xliff
).
In Symfony 7.2 we're adding a new linter to check the content of translations.
Unlike the lint:xliff
command, this new lint:translations
command checks
all your translations, no matter which of the supported translation formats
you use:
1 2 3 4
$ php bin/console lint:translations
# you can also lint only some of the supported locales
$ php bin/console lint:translations --locale=en --locale=tr
This command will be especially helpful when using the ICU MessageFormat syntax
in your translations. This syntax is powerful but sometimes tricky, so it's common
to miss closing braces (}
), forget about the other
key when using plurals, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
$ php bin/console lint:translations
--------- -------------------------------- --------
Locale Domains Valid?
--------- -------------------------------- --------
ar validators, security, messages Yes
bn messages, validators No
de validators, security, messages Yes
en validators, security, messages Yes
es validators, security, messages Yes
tr validators, security, messages Yes
--------- -------------------------------- --------
Errors for locale "bn" and domain "messages"
--------------------------------------------
Translation key "post.num_comments" is invalid:
[ERROR] Invalid message format (error #65807): msgfmt_create: message formatter
creation failed: U_DEFAULT_KEYWORD_MISSING
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.
This will be a significant addition to assessing the code quality of my projects.
The way I usually do it is this:
```
$ symfony composer app-check
```
In the `composer.json` I define:
```
"scripts": {
...
"app-check": [
"vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php",
"bin/console --no-interaction lint:yaml config",
"bin/console --no-interaction lint:twig templates",
"bin/console --no-interaction lint:xliff translations",
"bin/console --no-interaction lint:yaml translations",
"bin/console --no-interaction doctrine:schema:update --dump-sql",
"bin/console importmap:outdated",
"vendor/bin/rector process src --dry-run",
"phpDocumentor.phar -d ./src -t ./docs/app-doc"
]
},
```