Skip to content

Navigation Menu

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 62cb6a3

Browse filesBrowse files
committed
[Translation] Allow global parameters
1 parent 7c53816 commit 62cb6a3
Copy full SHA for 62cb6a3

14 files changed

+200
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode, callable $e
929929
->fixXmlConfig('fallback')
930930
->fixXmlConfig('path')
931931
->fixXmlConfig('provider')
932+
->fixXmlConfig('global')
932933
->children()
933934
->arrayNode('fallbacks')
934935
->info('Defaults to the value of "default_locale".')
@@ -983,6 +984,13 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode, callable $e
983984
->end()
984985
->defaultValue([])
985986
->end()
987+
->arrayNode('globals')
988+
->info('Global parameters.')
989+
->example(['pi' => 3.14])
990+
->normalizeKeys(false)
991+
->useAttributeAsKey('name')
992+
->prototype('scalar')->end()
993+
->end()
986994
->end()
987995
->end()
988996
->end()

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

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

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

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
@@ -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),
+14Lines 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+
]);
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+
]);
+17Lines 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>
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>
+10Lines 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'
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: []

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,22 @@ public function testTranslatorCacheDirDisabled()
11821182
$this->assertNull($options['cache_dir']);
11831183
}
11841184

1185+
public function testTranslatorGlobals()
1186+
{
1187+
$container = $this->createContainerFromFile('translator_globals');
1188+
$calls = $container->getDefinition('translator.globals')->getMethodCalls();
1189+
$this->assertSame([
1190+
['addGlobal', ['%%app_name%%', 'My application']],
1191+
['addGlobal', ['{app_version}', '1.2.3']],
1192+
], $calls);
1193+
}
1194+
1195+
public function testTranslatorWithoutGlobals()
1196+
{
1197+
$container = $this->createContainerFromFile('translator_without_globals');
1198+
$this->assertFalse($container->hasDefinition('translator.globals'));
1199+
}
1200+
11851201
public function testValidation()
11861202
{
11871203
$container = $this->createContainerFromFile('full');

‎src/Symfony/Component/Translation/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Mark class `DataCollectorTranslator` as `final`
8+
* Add `GlobalsTranslator` to allow defining global parameters
89

910
7.0
1011
---
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation;
13+
14+
use Symfony\Contracts\Translation\TranslatorInterface;
15+
16+
final class GlobalsTranslator implements TranslatorInterface
17+
{
18+
private array $globals = [];
19+
20+
public function __construct(
21+
private readonly TranslatorInterface $translator,
22+
) {
23+
}
24+
25+
public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string
26+
{
27+
$parameters = array_replace($this->globals, $parameters);
28+
29+
return $this->translator->trans($id, $parameters, $domain, $locale);
30+
}
31+
32+
public function getLocale(): string
33+
{
34+
return $this->translator->getLocale();
35+
}
36+
37+
public function addGlobal(string $name, mixed $value): void
38+
{
39+
$this->globals[$name] = $value;
40+
}
41+
}
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Translation\GlobalsTranslator;
16+
use Symfony\Component\Translation\Loader\ArrayLoader;
17+
use Symfony\Component\Translation\Translator;
18+
19+
class GlobalTranslatorTest extends TestCase
20+
{
21+
public function testTrans()
22+
{
23+
$translator = new Translator('en');
24+
$translator->addLoader('array', new ArrayLoader());
25+
$translator->addResource('array', ['test' => 'Welcome %name%!'], 'en');
26+
27+
$globalTranslator = new GlobalsTranslator($translator);
28+
$globalTranslator->addGlobal('%name%', 'Global name');
29+
30+
$this->assertSame('Welcome Global name!', $globalTranslator->trans('test'));
31+
$this->assertSame('Welcome Name!', $globalTranslator->trans('test', ['%name%' => 'Name']));
32+
}
33+
34+
public function testGetLocale()
35+
{
36+
$translator = new Translator('en');
37+
$globalTranslator = new GlobalsTranslator($translator);
38+
39+
$this->assertSame('en', $globalTranslator->getLocale());
40+
}
41+
}

0 commit comments

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