Skip to content

Commit 320d587

Browse files
committed
feature #53425 [Translation] Allow default parameters (Jean-Beru)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Translation] Allow default parameters | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #48182 | License | MIT This PRs adds a `GlobalsTranslator` which decorates the `Translator` when global parameters are given. Usage: ```yaml # config/framework.yaml framework: # ... translator: # ... globals: '{version}': '1.2.3' '%%application_name%%': 'My app' # "%" must be escaped if it is not a DI parameter ``` ```yaml # messages.en.yaml app: version: 'Application version: {version}' name: 'Application name: %application_name%' ``` ```twig # twig template {{ 'app.version'|trans }} # Displays "Application version: 1.2.3" {{ 'app.version'|trans({ '{version}': '2.3.4' }) }} # Displays "Application version: 2.3.4" {{ 'app.name'|trans }} # Displays "Application name: My app" {{ 'app.name'|trans({ '{application_name}': 'My new app' }) }} # Displays "Application name: My new app" ``` Profiler view: ![image](https://github.com/user-attachments/assets/6ba13995-ca91-4098-a87d-daea89864ab6) Commits ------- 652c658e587 [Translation] Allow default parameters
2 parents a7ec8af + ce42a91 commit 320d587

12 files changed

+163
-2
lines changed

DependencyInjection/Configuration.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode, callable $e
967967
->fixXmlConfig('fallback')
968968
->fixXmlConfig('path')
969969
->fixXmlConfig('provider')
970+
->fixXmlConfig('global')
970971
->children()
971972
->arrayNode('fallbacks')
972973
->info('Defaults to the value of "default_locale".')
@@ -1021,6 +1022,33 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode, callable $e
10211022
->end()
10221023
->defaultValue([])
10231024
->end()
1025+
->arrayNode('globals')
1026+
->info('Global parameters.')
1027+
->example(['app_version' => 3.14])
1028+
->normalizeKeys(false)
1029+
->useAttributeAsKey('name')
1030+
->arrayPrototype()
1031+
->fixXmlConfig('parameter')
1032+
->children()
1033+
->variableNode('value')->end()
1034+
->stringNode('message')->end()
1035+
->arrayNode('parameters')
1036+
->normalizeKeys(false)
1037+
->useAttributeAsKey('name')
1038+
->scalarPrototype()->end()
1039+
->end()
1040+
->stringNode('domain')->end()
1041+
->end()
1042+
->beforeNormalization()
1043+
->ifTrue(static fn ($v) => !\is_array($v))
1044+
->then(static fn ($v) => ['value' => $v])
1045+
->end()
1046+
->validate()
1047+
->ifTrue(static fn ($v) => !(isset($v['value']) xor isset($v['message'])))
1048+
->thenInvalid('The "globals" parameter should be either a string or an array with a "value" or a "message" key')
1049+
->end()
1050+
->end()
1051+
->end()
10241052
->end()
10251053
->end()
10261054
->end()

DependencyInjection/FrameworkExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
use Symfony\Component\Translation\Extractor\PhpAstExtractor;
187187
use Symfony\Component\Translation\LocaleSwitcher;
188188
use Symfony\Component\Translation\PseudoLocalizationTranslator;
189+
use Symfony\Component\Translation\TranslatableMessage;
189190
use Symfony\Component\Translation\Translator;
190191
use Symfony\Component\TypeInfo\Type;
191192
use Symfony\Component\TypeInfo\TypeResolver\PhpDocAwareReflectionTypeResolver;
@@ -1615,6 +1616,10 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
16151616
$translator->replaceArgument(4, $options);
16161617
}
16171618

1619+
foreach ($config['globals'] as $name => $global) {
1620+
$translator->addMethodCall('addGlobalParameter', [$name, $global['value'] ?? new Definition(TranslatableMessage::class, [$global['message'], $global['parameters'] ?? [], $global['domain'] ?? null])]);
1621+
}
1622+
16181623
if ($config['pseudo_localization']['enabled']) {
16191624
$options = $config['pseudo_localization'];
16201625
unset($options['enabled']);

Resources/config/schema/symfony-1.0.xsd

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@
256256
<xsd:element name="path" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
257257
<xsd:element name="pseudo-localization" type="pseudo_localization" minOccurs="0" maxOccurs="1" />
258258
<xsd:element name="provider" type="translation_provider" minOccurs="0" maxOccurs="unbounded" />
259+
<xsd:element name="global" type="translation_global" minOccurs="0" maxOccurs="unbounded" />
259260
</xsd:sequence>
260261
<xsd:attribute name="enabled" type="xsd:boolean" />
261262
<xsd:attribute name="fallback" type="xsd:string" />
@@ -285,6 +286,24 @@
285286
<xsd:attribute name="dsn" type="xsd:string" />
286287
</xsd:complexType>
287288

289+
<xsd:complexType name="translation_global" mixed="true">
290+
<xsd:sequence>
291+
<xsd:element name="parameter" type="translation_global_parameter" minOccurs="0" maxOccurs="unbounded" />
292+
</xsd:sequence>
293+
<xsd:attribute name="name" type="xsd:string" use="required" />
294+
<xsd:attribute name="value" type="xsd:string" />
295+
<xsd:attribute name="message" type="xsd:string" />
296+
<xsd:attribute name="domain" type="xsd:string" />
297+
</xsd:complexType>
298+
299+
<xsd:complexType name="translation_global_parameter">
300+
<xsd:simpleContent>
301+
<xsd:extension base="xsd:string">
302+
<xsd:attribute name="name" type="xsd:string" use="required" />
303+
</xsd:extension>
304+
</xsd:simpleContent>
305+
</xsd:complexType>
306+
288307
<xsd:complexType name="validation">
289308
<xsd:choice minOccurs="0" maxOccurs="unbounded">
290309
<xsd:element name="static-method" type="xsd:string" />

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ protected static function getBundleDefaultConfig()
785785
'localizable_html_attributes' => [],
786786
],
787787
'providers' => [],
788+
'globals' => [],
788789
],
789790
'validation' => [
790791
'enabled' => !class_exists(FullStack::class),
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'annotations' => false,
5+
'http_method_override' => false,
6+
'handle_all_throwables' => true,
7+
'php_errors' => ['log' => true],
8+
'translator' => [
9+
'globals' => [
10+
'%%app_name%%' => 'My application',
11+
'{app_version}' => '1.2.3',
12+
'{url}' => ['message' => 'url', 'parameters' => ['scheme' => 'https://'], 'domain' => 'global'],
13+
],
14+
],
15+
]);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'annotations' => false,
5+
'http_method_override' => false,
6+
'handle_all_throwables' => true,
7+
'php_errors' => ['log' => true],
8+
'translator' => ['globals' => []],
9+
]);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:config secret="s3cr3t" http-method-override="false" handle-all-throwables="true">
10+
<framework:annotations enabled="false" />
11+
<framework:php-errors log="true" />
12+
<framework:translator enabled="true">
13+
<framework:global name="%%app_name%%">My application</framework:global>
14+
<framework:global name="{app_version}" value="1.2.3" />
15+
<framework:global name="{url}" message="url" domain="global">
16+
<framework:parameter name="scheme">https://</framework:parameter>
17+
</framework:global>
18+
</framework:translator>
19+
</framework:config>
20+
</container>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:config secret="s3cr3t" http-method-override="false" handle-all-throwables="true">
10+
<framework:annotations enabled="false" />
11+
<framework:php-errors log="true" />
12+
<framework:translator enabled="true" />
13+
</framework:config>
14+
</container>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
framework:
2+
annotations: false
3+
http_method_override: false
4+
handle_all_throwables: true
5+
php_errors:
6+
log: true
7+
translator:
8+
globals:
9+
'%%app_name%%': 'My application'
10+
'{app_version}': '1.2.3'
11+
'{url}': { message: 'url', parameters: { scheme: 'https://' }, domain: 'global' }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
framework:
2+
annotations: false
3+
http_method_override: false
4+
handle_all_throwables: true
5+
php_errors:
6+
log: true
7+
translator:
8+
globals: []

0 commit comments

Comments
 (0)