Symfony comes with several built-in console commands that help validate different parts of an application. These commands are often referred to as linters and are useful for catching mistakes early, before they reach production. This tutorial shows how to check errors in translation files using the console command in a Symfony 8 application.
The lint:yaml and lint:xliff commands can be used to check the YAML and XML syntax of the translation files, but not their contents. Symfony also provides a dedicated lint:translations command for validating translation messages themselves. This command is particularly useful when working with ICU MessageFormat syntax in translations, which is powerful but can be tricky to use.
Before using the translation linter, make sure the Translation component is installed in the project:
composer require symfony/translation
Let's say that the application defines the locales it supports:
config/packages/translation.yaml
framework:
enabled_locales: [en, de]
# ...
Consider the following translation files using ICU MessageFormat syntax:
translations/messages+intl-icu.en.yaml
say_hello: Hello {name
translations/messages+intl-icu.de.yaml
say_hello: Hallo {name}
At first glance these files may look correct, but the English translation contains a subtle mistake: a missing closing brace.
Symfony provides the lint:translations command to validate translation messages for all locales defined in enabled_locales.
php bin/console lint:translations
The output shows a summary for each locale and domain. Indicates whether the translations are valid. If any issues are found, Symfony displays detailed error information.
-------- -------------------------------- --------
Locale Domains Valid?
-------- -------------------------------- --------
en validators, security, messages No
de validators, security, messages Yes
-------- -------------------------------- --------
Errors for locale "en" and domain "messages"
--------------------------------------------
Translation key "say_hello" is invalid:
[ERROR] Invalid message format (error #65801): msgfmt_create: message formatter creation failed: U_UNMATCHED_BRACES
If we only want to validate translations for a single language, we can use the --locale option:
php bin/console lint:translations --locale=en
We can also specify multiple locales by repeating the option:
php bin/console lint:translations --locale=en --locale=es --locale=pl
Leave a Comment
Cancel reply