@@ -416,6 +416,84 @@ You can also specify the message domain and pass some additional variables:
416416 major difference: automatic output escaping is **not ** applied to translations
417417 using a tag.
418418
419+ Global Translation Parameters
420+ -----------------------------
421+
422+ .. versionadded :: 7.3
423+
424+ The global translation parameters feature was introduced in Symfony 7.3.
425+
426+ If the content of a translation parameter is repeated across multiple
427+ translation messages (e.g. a company name, or a version number), you can define
428+ it as a global translation parameter. This helps you avoid repeating the same
429+ values manually in each message.
430+
431+ You can configure these global parameters in the ``translations.globals `` option
432+ of your main configuration file using either ``%...% `` or ``{...} `` syntax:
433+
434+ .. configuration-block ::
435+
436+ .. code-block :: yaml
437+
438+ # config/packages/translator.yaml
439+ translator :
440+ # ...
441+ globals :
442+ # when using the '%' wrapping characters, you must escape them
443+ ' %%app_name%% ' : ' My application'
444+ ' {app_version} ' : ' 1.2.3'
445+ ' {url} ' : { message: 'url', parameters: { scheme: 'https://' }, domain: 'global' }
446+
447+ .. code-block :: xml
448+
449+ <!-- config/packages/translation.xml -->
450+ <?xml version =" 1.0" encoding =" UTF-8" ?>
451+ <container xmlns =" http://symfony.com/schema/dic/services"
452+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
453+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
454+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
455+ https://symfony.com/schema/dic/services/services-1.0.xsd
456+ http://symfony.com/schema/dic/symfony
457+ https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
458+
459+ <framework : config >
460+ <framework : translator >
461+ <!-- ... -->
462+ <!-- when using the '%' wrapping characters, you must escape them -->
463+ <framework : global name =" %%app_name%%" >My application</framework : global >
464+ <framework : global name =" {app_version}" value =" 1.2.3" />
465+ <framework : global name =" {url}" message =" url" domain =" global" >
466+ <framework : parameter name =" scheme" >https://</framework : parameter >
467+ </framework : global >
468+ </framework : translator >
469+ </framework : config >
470+ </container >
471+
472+ .. code-block :: php
473+
474+ // config/packages/translator.php
475+ use Symfony\Config\TwigConfig;
476+
477+ return static function (TwigConfig $translator): void {
478+ // ...
479+ // when using the '%' wrapping characters, you must escape them
480+ $translator->globals('%%app_name%%')->value('My application');
481+ $translator->globals('{app_version}')->value('1.2.3');
482+ $translator->globals('{url}')->value(['message' => 'url', 'parameters' => ['scheme' => 'https://']]);
483+ };
484+
485+ Once defined, you can use these parameters in translation messages anywhere in
486+ your application:
487+
488+ .. code-block :: twig
489+
490+ {{ 'Application version: {version}'|trans }}
491+ {# output: "Application version: 1.2.3" #}
492+
493+ {# parameters passed to the message override global parameters #}
494+ {{ 'Package version: {version}'|trans({'{version}': '2.3.4'}) }}
495+ # Displays "Package version: 2.3.4"
496+
419497 Forcing the Translator Locale
420498-----------------------------
421499
@@ -1160,67 +1238,6 @@ service argument with the :class:`Symfony\\Component\\Translation\\LocaleSwitche
11601238class to inject the locale switcher service. Otherwise, configure your services
11611239manually and inject the ``translation.locale_switcher `` service.
11621240
1163- .. _translation-global-variables :
1164-
1165- Global Variables
1166- ~~~~~~~~~~~~~~~~
1167-
1168- The translator allows you to automatically configure global variables that will be available
1169- for the translation process. These global variables are defined in the ``translations.globals ``
1170- option inside the main configuration file:
1171-
1172- .. configuration-block ::
1173-
1174- .. code-block :: yaml
1175-
1176- # config/packages/translator.yaml
1177- translator :
1178- # ...
1179- globals :
1180- ' %%app_name%% ' : ' My application'
1181- ' {app_version} ' : ' 1.2.3'
1182- ' {url} ' : { message: 'url', parameters: { scheme: 'https://' }, domain: 'global' }
1183-
1184- .. code-block :: xml
1185-
1186- <!-- config/packages/translation.xml -->
1187- <?xml version =" 1.0" encoding =" UTF-8" ?>
1188- <container xmlns =" http://symfony.com/schema/dic/services"
1189- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1190- xmlns : framework =" http://symfony.com/schema/dic/symfony"
1191- xsi : schemaLocation =" http://symfony.com/schema/dic/services
1192- https://symfony.com/schema/dic/services/services-1.0.xsd
1193- http://symfony.com/schema/dic/symfony
1194- https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
1195-
1196- <framework : config >
1197- <framework : translator >
1198- <!-- ... -->
1199- <framework : global name =" %%app_name%%" >My application</framework : global >
1200- <framework : global name =" {app_version}" value =" 1.2.3" />
1201- <framework : global name =" {url}" message =" url" domain =" global" >
1202- <framework : parameter name =" scheme" >https://</framework : parameter >
1203- </framework : global >
1204- </framework : translator >
1205- </framework : config >
1206- </container >
1207-
1208- .. code-block :: php
1209-
1210- // config/packages/translator.php
1211- use Symfony\Config\TwigConfig;
1212-
1213- return static function (TwigConfig $translator): void {
1214- // ...
1215- $translator->globals('%%app_name%%')->value('My application');
1216- $translator->globals('{app_version}')->value('1.2.3');
1217- $translator->globals('{url}')->value(['message' => 'url', 'parameters' => ['scheme' => 'https://']);
1218- };
1219-
1220- .. versionadded :: 7.3
1221-
1222- The ``translator:globals `` option was introduced in Symfony 7.3.
1223-
12241241.. _translation-debug :
12251242
12261243How to Find Missing or Unused Translation Messages
0 commit comments