Skip to content

Commit 5f6aaca

Browse files
committed
[Translation] Allow global parameters
1 parent 82dc312 commit 5f6aaca

File tree

14 files changed

+204
-0
lines changed

14 files changed

+204
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode, callable $e
923923
->fixXmlConfig('fallback')
924924
->fixXmlConfig('path')
925925
->fixXmlConfig('provider')
926+
->fixXmlConfig('global')
926927
->children()
927928
->arrayNode('fallbacks')
928929
->info('Defaults to the value of "default_locale".')
@@ -977,6 +978,13 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode, callable $e
977978
->end()
978979
->defaultValue([])
979980
->end()
981+
->arrayNode('globals')
982+
->info('Global parameters.')
983+
->example(['pi' => 3.14])
984+
->normalizeKeys(false)
985+
->useAttributeAsKey('name')
986+
->prototype('scalar')->end()
987+
->end()
980988
->end()
981989
->end()
982990
->end()

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
use Symfony\Component\Translation\Bridge as TranslationBridge;
165165
use Symfony\Component\Translation\Command\XliffLintCommand as BaseXliffLintCommand;
166166
use Symfony\Component\Translation\Extractor\PhpAstExtractor;
167+
use Symfony\Component\Translation\GlobalsTranslator;
167168
use Symfony\Component\Translation\LocaleSwitcher;
168169
use Symfony\Component\Translation\PseudoLocalizationTranslator;
169170
use Symfony\Component\Translation\Translator;
@@ -1580,6 +1581,18 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
15801581
}
15811582
}
15821583

1584+
if (!empty($config['globals'])) {
1585+
$def = $container
1586+
->register('translator.globals', GlobalsTranslator::class)
1587+
->setDecoratedService('translator', null, -1) // Lower priority than "translator.data_collector"
1588+
->setArguments([
1589+
new Reference('translator.globals.inner'),
1590+
]);
1591+
foreach ($config['globals'] as $key => $value) {
1592+
$def->addMethodCall('addGlobal', [$key, $value]);
1593+
}
1594+
}
1595+
15831596
if (!$config['providers']) {
15841597
return;
15851598
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,15 @@
230230
<xsd:element name="path" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
231231
<xsd:element name="pseudo-localization" type="pseudo_localization" minOccurs="0" maxOccurs="1" />
232232
<xsd:element name="provider" type="translation_provider" minOccurs="0" maxOccurs="unbounded" />
233+
<xsd:element name="global" type="translation_global" minOccurs="0" maxOccurs="unbounded" />
233234
</xsd:sequence>
234235
<xsd:attribute name="enabled" type="xsd:boolean" />
235236
<xsd:attribute name="fallback" type="xsd:string" />
236237
<xsd:attribute name="logging" type="xsd:boolean" />
237238
<xsd:attribute name="formatter" type="xsd:string" />
238239
<xsd:attribute name="cache-dir" type="xsd:string" />
239240
<xsd:attribute name="default-path" type="xsd:string" />
241+
<xsd:attribute name="global" type="xsd:string" />
240242
</xsd:complexType>
241243

242244
<xsd:complexType name="pseudo_localization">
@@ -259,6 +261,11 @@
259261
<xsd:attribute name="dsn" type="xsd:string" />
260262
</xsd:complexType>
261263

264+
<xsd:complexType name="translation_global" mixed="true">
265+
<xsd:attribute name="name" type="xsd:string" use="required" />
266+
<xsd:attribute name="value" type="xsd:string" />
267+
</xsd:complexType>
268+
262269
<xsd:complexType name="validation">
263270
<xsd:choice minOccurs="0" maxOccurs="unbounded">
264271
<xsd:element name="static-method" type="xsd:string" />

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ protected static function getBundleDefaultConfig()
591591
'localizable_html_attributes' => [],
592592
],
593593
'providers' => [],
594+
'globals' => [],
594595
],
595596
'validation' => [
596597
'enabled' => !class_exists(FullStack::class),
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
],
13+
],
14+
]);
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' => [],
9+
]);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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%%" value="My application" />
14+
<framework:global name="{app_version}" value="1.2.3" />
15+
</framework:translator>
16+
</framework:config>
17+
</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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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'
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)