Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 97de6eb

Browse filesBrowse files
committed
[Translator] document global variables feature configuration
1 parent 20ce189 commit 97de6eb
Copy full SHA for 97de6eb

File tree

Expand file treeCollapse file tree

1 file changed

+61
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+61
-0
lines changed

‎translation.rst

Copy file name to clipboardExpand all lines: translation.rst
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,67 @@ service argument with the :class:`Symfony\\Component\\Translation\\LocaleSwitche
11601160
class to inject the locale switcher service. Otherwise, configure your services
11611161
manually and inject the ``translation.locale_switcher`` service.
11621162

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+
11631224
.. _translation-debug:
11641225

11651226
How to Find Missing or Unused Translation Messages

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.