Skip to content

[Translation] document global variables feature configuration #20862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions translation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,67 @@
class to inject the locale switcher service. Otherwise, configure your services
manually and inject the ``translation.locale_switcher`` service.

.. _translation-global-variables:

Global Variables
~~~~~~~~~~~~~~~~

The translator allows you to automatically configure global variables that will be available
for the translation process. These global variables are defined in the ``translations.globals``
option inside the main configuration file:

.. configuration-block::

.. code-block:: yaml

# config/packages/translator.yaml
translator:
# ...
globals:
'%%app_name%%': 'My application'
'{app_version}': '1.2.3'
'{url}': { message: 'url', parameters: { scheme: 'https://' }, domain: 'global' }

Check failure on line 1182 in translation.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Cache Warmup] In FileLoader.php line 177: There is no extension able to load the configuration for "translator" (in " /home/runner/work/symfony-docs/symfony-docs/_sf_app/config/packages/transla tor.yaml"). Looked for namespace "translator", found "framework", "twig", " web_profiler", "monolog", "debug", "maker", "doctrine", "security", "twig_e xtra", "mercure", "webpack_encore" in /home/runner/work/symfony-docs/symfon y-docs/_sf_app/config/packages/translator.yaml (which is being imported fro m "/home/runner/work/symfony-docs/symfony-docs/_sf_app/src/Kernel.php"). In YamlFileLoader.php line 814: There is no extension able to load the configuration for "translator" (in " /home/runner/work/symfony-docs/symfony-docs/_sf_app/config/packages/transla tor.yaml"). Looked for namespace "translator", found "framework", "twig", " web_profiler", "monolog", "debug", "maker", "doctrine", "security", "twig_e xtra", "mercure", "webpack_encore".

.. code-block:: xml

<!-- config/packages/translation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:translator>
<!-- ... -->
<framework:global name="%%app_name%%">My application</framework:global>
<framework:global name="{app_version}" value="1.2.3" />

Check failure on line 1200 in translation.rst

View workflow job for this annotation

GitHub Actions / Lint (DOCtor-RST)

Please remove space before "/>"
<framework:global name="{url}" message="url" domain="global">
<framework:parameter name="scheme">https://</framework:parameter>
</framework:global>
</framework:translator>
</framework:config>
</container>

.. code-block:: php

// config/packages/translator.php
use Symfony\Config\TwigConfig;

return static function (TwigConfig $translator): void {
// ...
$translator->globals('%%app_name%%')->value('My application');
$translator->globals('{app_version}')->value('1.2.3');
$translator->globals('{url}')->value(['message' => 'url', 'parameters' => ['scheme' => 'https://']);

Check failure on line 1217 in translation.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[PHP syntax] Syntax error, unexpected ')', expecting ']'
};

Check failure on line 1218 in translation.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Cache Warmup] 2025-04-07T06:46:25+00:00 [critical] Uncaught Error: Unclosed '[' does not match ')'

.. versionadded:: 7.3

The ``translator:globals`` option was introduced in Symfony 7.3.

.. _translation-debug:

How to Find Missing or Unused Translation Messages
Expand Down
Loading