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 7ca354b

Browse filesBrowse files
committed
[Translation] Allow global parameters
1 parent 04551ad commit 7ca354b
Copy full SHA for 7ca354b

20 files changed

+312
-8
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
@@ -966,6 +966,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode, callable $e
966966
->fixXmlConfig('fallback')
967967
->fixXmlConfig('path')
968968
->fixXmlConfig('provider')
969+
->fixXmlConfig('global')
969970
->children()
970971
->arrayNode('fallbacks')
971972
->info('Defaults to the value of "default_locale".')
@@ -1020,6 +1021,33 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode, callable $e
10201021
->end()
10211022
->defaultValue([])
10221023
->end()
1024+
->arrayNode('globals')
1025+
->info('Global parameters.')
1026+
->example(['app_version' => 3.14])
1027+
->normalizeKeys(false)
1028+
->useAttributeAsKey('name')
1029+
->arrayPrototype()
1030+
->fixXmlConfig('parameter')
1031+
->children()
1032+
->variableNode('value')->end()
1033+
->stringNode('message')->end()
1034+
->arrayNode('parameters')
1035+
->normalizeKeys(false)
1036+
->useAttributeAsKey('name')
1037+
->scalarPrototype()->end()
1038+
->end()
1039+
->stringNode('domain')->end()
1040+
->end()
1041+
->beforeNormalization()
1042+
->ifTrue(static fn ($v) => !\is_array($v))
1043+
->then(static fn ($v) => ['value' => $v])
1044+
->end()
1045+
->validate()
1046+
->ifTrue(static fn ($v) => !(isset($v['value']) xor isset($v['message'])))
1047+
->thenInvalid('The "globals" parameter should be either a string or an array with a "value" or a "message" key')
1048+
->end()
1049+
->end()
1050+
->end()
10231051
->end()
10241052
->end()
10251053
->end()

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,14 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
16111611
$translator->replaceArgument(4, $options);
16121612
}
16131613

1614+
foreach ($config['globals'] as $name => $global) {
1615+
if (isset($global['value'])) {
1616+
$translator->addMethodCall('addGlobalParameter', [$name, $global['value']]);
1617+
} else {
1618+
$translator->addMethodCall('addGlobalTranslatableParameter', [$name, $global['message'], $global['parameters'] ?? [], $global['domain'] ?? null]);
1619+
}
1620+
}
1621+
16141622
if ($config['pseudo_localization']['enabled']) {
16151623
$options = $config['pseudo_localization'];
16161624
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" mixed="true">
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
@@ -769,6 +769,7 @@ protected static function getBundleDefaultConfig()
769769
'localizable_html_attributes' => [],
770770
],
771771
'providers' => [],
772+
'globals' => [],
772773
],
773774
'validation' => [
774775
'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: []

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

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

1201+
public function testTranslatorGlobals()
1202+
{
1203+
$container = $this->createContainerFromFile('translator_globals');
1204+
1205+
$calls = $container->getDefinition('translator.default')->getMethodCalls();
1206+
1207+
$this->assertCount(5, $calls);
1208+
$this->assertSame(
1209+
['addGlobalParameter', ['%%app_name%%', 'My application']],
1210+
$calls[2],
1211+
);
1212+
$this->assertSame(
1213+
['addGlobalParameter', ['{app_version}', '1.2.3']],
1214+
$calls[3],
1215+
);
1216+
$this->assertSame(
1217+
['addGlobalTranslatableParameter', ['{url}', 'url', ['scheme' => 'https://'], 'global']],
1218+
$calls[4],
1219+
);
1220+
}
1221+
1222+
public function testTranslatorWithoutGlobals()
1223+
{
1224+
$container = $this->createContainerFromFile('translator_without_globals');
1225+
1226+
$calls = $container->getDefinition('translator.default')->getMethodCalls();
1227+
1228+
$this->assertCount(2, $calls);
1229+
}
1230+
12011231
public function testValidation()
12021232
{
12031233
$container = $this->createContainerFromFile('full');

‎src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/translation.html.twig

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/translation.html.twig
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,31 @@
156156
{% endblock messages %}
157157
{% endif %}
158158

159+
{% if collector.globalParameters is not empty %}
160+
<h2>Global parameters</h2>
161+
162+
<table>
163+
<thead>
164+
<tr>
165+
<th>Locale</th>
166+
<th>Message ID</th>
167+
<th>Value</th>
168+
</tr>
169+
</thead>
170+
<tbody>
171+
{% for locale, values in collector.globalParameters %}
172+
{% for id, value in values %}
173+
<tr>
174+
<td class="font-normal text-small nowrap">{{ locale != '*' ? "#{locale}" }}</td>
175+
<td class="font-normal text-small nowrap">{{ id }}</td>
176+
<td class="font-normal text-small nowrap">{{ value }}</td>
177+
</tr>
178+
{% endfor %}
179+
{% endfor %}
180+
</tbody>
181+
</table>
182+
{% endif %}
183+
159184
{% endblock %}
160185

161186
{% macro render_table(messages, is_fallback) %}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add `addGlobalParameter` and `addGlobalTranslatableParameter` to allow defining global translation parameters
8+
49
7.2
510
---
611

‎src/Symfony/Component/Translation/DataCollector/TranslationDataCollector.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/DataCollector/TranslationDataCollector.php
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function collect(Request $request, Response $response, ?\Throwable $excep
4444
{
4545
$this->data['locale'] = $this->translator->getLocale();
4646
$this->data['fallback_locales'] = $this->translator->getFallbackLocales();
47+
$this->data['global_parameters'] = $this->translator->getGlobalParameters();
4748
}
4849

4950
public function reset(): void
@@ -84,6 +85,14 @@ public function getFallbackLocales(): Data|array
8485
return (isset($this->data['fallback_locales']) && \count($this->data['fallback_locales']) > 0) ? $this->data['fallback_locales'] : [];
8586
}
8687

88+
/**
89+
* @internal
90+
*/
91+
public function getGlobalParameters(): Data|array
92+
{
93+
return $this->data['global_parameters'] ?? [];
94+
}
95+
8796
public function getName(): string
8897
{
8998
return 'translation';

‎src/Symfony/Component/Translation/DataCollectorTranslator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/DataCollectorTranslator.php
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ public function getFallbackLocales(): array
8282
return [];
8383
}
8484

85+
public function getGlobalParameters(): array
86+
{
87+
if ($this->translator instanceof Translator || method_exists($this->translator, 'getGlobalParameters')) {
88+
return $this->translator->getGlobalParameters();
89+
}
90+
91+
return [];
92+
}
93+
8594
public function __call(string $method, array $args): mixed
8695
{
8796
return $this->translator->{$method}(...$args);

‎src/Symfony/Component/Translation/Tests/DataCollector/TranslationDataCollectorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/DataCollector/TranslationDataCollectorTest.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,20 @@ public function testCollectAndReset()
137137
$translator = $this->getTranslator();
138138
$translator->method('getLocale')->willReturn('fr');
139139
$translator->method('getFallbackLocales')->willReturn(['en']);
140+
$translator->method('getGlobalParameters')->willReturn(['welcome' => 'Welcome {name}!']);
140141

141142
$dataCollector = new TranslationDataCollector($translator);
142143
$dataCollector->collect($this->createMock(Request::class), $this->createMock(Response::class));
143144

144145
$this->assertSame('fr', $dataCollector->getLocale());
145146
$this->assertSame(['en'], $dataCollector->getFallbackLocales());
147+
$this->assertSame(['welcome' => 'Welcome {name}!'], $dataCollector->getGlobalParameters());
146148

147149
$dataCollector->reset();
148150

149151
$this->assertNull($dataCollector->getLocale());
150152
$this->assertEmpty($dataCollector->getFallbackLocales());
153+
$this->assertEmpty($dataCollector->getGlobalParameters());
151154
}
152155

153156
private function getTranslator()

‎src/Symfony/Component/Translation/Tests/DataCollectorTranslatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/DataCollectorTranslatorTest.php
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ public function testCollectMessages()
8484
$this->assertEquals($expectedMessages, $collector->getCollectedMessages());
8585
}
8686

87+
public function testGetGlobalParameters()
88+
{
89+
$translator = new Translator('en');
90+
$translator->addGlobalParameter('app', 'My app');
91+
$collector = new DataCollectorTranslator($translator);
92+
93+
$this->assertEquals(['*' => ['app' => 'My app']], $collector->getGlobalParameters());
94+
}
95+
8796
private function createCollector()
8897
{
8998
$translator = new Translator('en');

‎src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,12 @@ protected function deleteTmpDir()
5555
}
5656

5757
/**
58+
* @requires extension intl
59+
*
5860
* @dataProvider runForDebugAndProduction
5961
*/
6062
public function testThatACacheIsUsed($debug)
6163
{
62-
if (!class_exists(\MessageFormatter::class)) {
63-
$this->markTestSkipped(\sprintf('Skipping test as the required "%s" class does not exist. Consider installing the "intl" PHP extension or the "symfony/polyfill-intl-messageformatter" package.', \MessageFormatter::class));
64-
}
65-
6664
$locale = 'any_locale';
6765
$format = 'some_format';
6866
$msgid = 'test';

0 commit comments

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