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 652c658

Browse filesBrowse files
Jean-Berufabpot
authored andcommitted
[Translation] Allow default parameters
1 parent a9a1226 commit 652c658
Copy full SHA for 652c658
Expand file treeCollapse file tree

20 files changed

+312
-3
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+28Lines 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()

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@
185185
use Symfony\Component\Translation\Extractor\PhpAstExtractor;
186186
use Symfony\Component\Translation\LocaleSwitcher;
187187
use Symfony\Component\Translation\PseudoLocalizationTranslator;
188+
use Symfony\Component\Translation\TranslatableMessage;
188189
use Symfony\Component\Translation\Translator;
189190
use Symfony\Component\TypeInfo\Type;
190191
use Symfony\Component\TypeInfo\TypeResolver\PhpDocAwareReflectionTypeResolver;
@@ -1614,6 +1615,10 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
16141615
$translator->replaceArgument(4, $options);
16151616
}
16161617

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

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd
+19Lines 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" />

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php
+1Lines 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),
+15Lines 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+
]);
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+
]);
+20Lines 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>
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>
+11Lines 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' }
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.