New in Symfony 7.3: Global Translation Parameters


Contributed by
Hubert Lenoir
in
#53425

The Symfony Translation component provides tools to internationalize your
application. One of its main features is the translation of messages in PHP
code or Twig templates:

{{ message|trans({'%name%': 'Fabien'}) }}

In this example, message is a variable that contains the actual message,
and %name% is a placeholder used to insert dynamic content.
When the same translation parameter (e.g. company names, version numbers)
appears across multiple messages, repeating its value everywhere becomes
cumbersome.
That's why Symfony 7.3 introduces global translation parameters, which can
be used in any translation message without passing them explicitly. First,
define these parameters using the new translator.globals option:

# config/packages/translator.yaml
translator:
# ...
globals:
'{app_name}': 'My application'
'{version}': '1.2.3'
# the value van be a TranslatableMessage itself
'{homepage}': { message: 'homepage_url', parameters: { show_scheme: false }, domain: 'global' }

Once defined, you can use these parameters in translation messages anywhere in
your application:

{{ 'Application version: {version}'|trans }}
{# output: "Application version: 1.2.3" #}

{# parameters passed explicitly override global parameters #}
{{ 'Package version: {version}'|trans({'{version}': '2.3.4'}) }}
{# output: "Package version: 2.3.4" #}

The Symfony Profiler has also been updated to display global translation
parameters, helping you debug and fine-tune your translations with ease:

Sponsor the Symfony project.