Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a59e38a

Browse files
committed
review
1 parent 9d201f3 commit a59e38a

File tree

8 files changed

+85
-14
lines changed

8 files changed

+85
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode, callable $e
10291029
->arrayPrototype()
10301030
->fixXmlConfig('parameter')
10311031
->children()
1032-
->variableNode('value')->end()
1032+
->scalarNode('value')->end()
10331033
->stringNode('message')->end()
10341034
->arrayNode('parameters')
10351035
->normalizeKeys(false)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@
296296
<xsd:attribute name="domain" type="xsd:string" />
297297
</xsd:complexType>
298298

299-
<xsd:complexType name="translation_global_parameter" mixed="true">
299+
<xsd:complexType name="translation_global_parameter">
300300
<xsd:simpleContent>
301301
<xsd:extension base="xsd:string">
302302
<xsd:attribute name="name" type="xsd:string" use="required" />

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,17 @@
168168
</tr>
169169
</thead>
170170
<tbody>
171-
{% for locale, values in collector.globalParameters %}
171+
{% for id, value in collector.globalParameters %}
172+
<tr>
173+
<td class="font-normal text-small nowrap"></td>
174+
<td class="font-normal text-small nowrap">{{ id }}</td>
175+
<td class="font-normal text-small nowrap">{{ value }}</td>
176+
</tr>
177+
{% endfor %}
178+
{% for locale, values in collector.globalTranslatedParameters %}
172179
{% for id, value in values %}
173180
<tr>
174-
<td class="font-normal text-small nowrap">{{ locale != '*' ? locale }}</td>
181+
<td class="font-normal text-small nowrap">{{ locale }}</td>
175182
<td class="font-normal text-small nowrap">{{ id }}</td>
176183
<td class="font-normal text-small nowrap">{{ value }}</td>
177184
</tr>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function collect(Request $request, Response $response, ?\Throwable $excep
4545
$this->data['locale'] = $this->translator->getLocale();
4646
$this->data['fallback_locales'] = $this->translator->getFallbackLocales();
4747
$this->data['global_parameters'] = $this->translator->getGlobalParameters();
48+
$this->data['global_translated_parameters'] = $this->translator->getGlobalTranslatedParameters();
4849
}
4950

5051
public function reset(): void
@@ -93,6 +94,14 @@ public function getGlobalParameters(): Data|array
9394
return $this->data['global_parameters'] ?? [];
9495
}
9596

97+
/**
98+
* @internal
99+
*/
100+
public function getGlobalTranslatedParameters(): Data|array
101+
{
102+
return $this->data['global_translated_parameters'] ?? [];
103+
}
104+
96105
public function getName(): string
97106
{
98107
return 'translation';

src/Symfony/Component/Translation/DataCollectorTranslator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ public function getGlobalParameters(): array
9191
return [];
9292
}
9393

94+
public function getGlobalTranslatedParameters(): array
95+
{
96+
if ($this->translator instanceof Translator || method_exists($this->translator, 'getGlobalTranslatedParameters')) {
97+
return $this->translator->getGlobalTranslatedParameters();
98+
}
99+
100+
return [];
101+
}
102+
94103
public function __call(string $method, array $args): mixed
95104
{
96105
return $this->translator->{$method}(...$args);

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Translation\DataCollectorTranslator;
1616
use Symfony\Component\Translation\Loader\ArrayLoader;
17+
use Symfony\Component\Translation\TranslatableMessage;
1718
use Symfony\Component\Translation\Translator;
1819

1920
class DataCollectorTranslatorTest extends TestCase
@@ -90,7 +91,34 @@ public function testGetGlobalParameters()
9091
$translator->addGlobalParameter('app', 'My app');
9192
$collector = new DataCollectorTranslator($translator);
9293

93-
$this->assertEquals(['*' => ['app' => 'My app']], $collector->getGlobalParameters());
94+
$this->assertEquals(['app' => 'My app'], $collector->getGlobalParameters());
95+
}
96+
97+
public function testGetGlobalTranslatedParameters()
98+
{
99+
$translator = new Translator('en');
100+
$translator->addLoader('array', new ArrayLoader());
101+
$translator->addResource('array', ['url.front' => 'https://example.com', 'hello' => 'Welcome to {front_url}'], 'en');
102+
$translator->addResource('array', ['url.front' => 'https://example.fr', 'hello' => 'Welcome to {front_url}'], 'fr');
103+
$translator->addGlobalTranslatableParameter('front_url', new TranslatableMessage('url.front'));
104+
$collector = new DataCollectorTranslator($translator);
105+
106+
$collector->trans('hello', [], null, 'en');
107+
$this->assertEquals(
108+
[
109+
'en' => ['front_url' => 'https://example.com'],
110+
],
111+
$collector->getGlobalTranslatedParameters(),
112+
);
113+
114+
$collector->trans('hello', [], null, 'fr');
115+
$this->assertEquals(
116+
[
117+
'en' => ['front_url' => 'https://example.com'],
118+
'fr' => ['front_url' => 'https://example.fr'],
119+
],
120+
$collector->getGlobalTranslatedParameters(),
121+
);
94122
}
95123

96124
private function createCollector()

src/Symfony/Component/Translation/Tests/TranslatorTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,14 @@ public function testTrans($expected, $id, $translation, $parameters, $locale, $d
399399
}
400400

401401
/**
402-
* @requires extension intl
403-
*
404402
* @dataProvider getTransICUTests
405403
*/
406404
public function testTransICU(...$args)
407405
{
406+
if (!class_exists(\MessageFormatter::class)) {
407+
$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));
408+
}
409+
408410
$this->testTrans(...$args);
409411
}
410412

src/Symfony/Component/Translation/Translator.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface, LocaleA
6161
private bool $hasIntlFormatter;
6262

6363
/**
64-
* @var array<string, <string, string|int|float>>
64+
* @var array<string, string|int|float>
6565
*/
6666
private array $globalParameters = [];
6767

@@ -70,6 +70,11 @@ class Translator implements TranslatorInterface, TranslatorBagInterface, LocaleA
7070
*/
7171
private array $globalTranslatableParameters = [];
7272

73+
/**
74+
* @var array<string, string|int|float>
75+
*/
76+
private array $globalTranslatedParameters = [];
77+
7378
/**
7479
* @throws InvalidArgumentException If a locale contains invalid characters
7580
*/
@@ -165,9 +170,9 @@ public function getFallbackLocales(): array
165170
return $this->fallbackLocales;
166171
}
167172

168-
public function addGlobalParameter(string $id, mixed $value): void
173+
public function addGlobalParameter(string $id, string|int|float $value): void
169174
{
170-
$this->globalParameters['*'][$id] = $value;
175+
$this->globalParameters[$id] = $value;
171176
}
172177

173178
public function addGlobalTranslatableParameter(string $id, string $message, array $parameters = [], ?string $domain = null): void
@@ -183,6 +188,14 @@ public function getGlobalParameters(): array
183188
return $this->globalParameters;
184189
}
185190

191+
/**
192+
* @internal
193+
*/
194+
public function getGlobalTranslatedParameters(): array
195+
{
196+
return $this->globalTranslatedParameters;
197+
}
198+
186199
public function trans(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
187200
{
188201
if (null === $id || '' === $id) {
@@ -202,12 +215,15 @@ public function trans(?string $id, array $parameters = [], ?string $domain = nul
202215
}
203216
}
204217

205-
if ($this->globalTranslatableParameters && !isset($this->globalParameters[$locale])) {
206-
$this->globalParameters[$locale] = []; // Avoid infinite loops
207-
$this->globalParameters[$locale] = array_map(fn ($parameter) => $parameter->trans($this, $locale), $this->globalTranslatableParameters);
218+
if ($this->globalTranslatableParameters && !isset($this->globalTranslatedParameters[$locale])) {
219+
$this->globalTranslatedParameters[$locale] = []; // Avoid infinite loops
220+
$this->globalTranslatedParameters[$locale] = array_map(fn ($parameter) => $parameter->trans($this, $locale), $this->globalTranslatableParameters);
208221
}
209222
if ($this->globalParameters) {
210-
$parameters += ($this->globalParameters[$locale] ?? []) + ($this->globalParameters['*'] ?? []);
223+
$parameters += $this->globalParameters;
224+
}
225+
if (isset($this->globalTranslatedParameters[$locale])) {
226+
$parameters += $this->globalTranslatedParameters[$locale];
211227
}
212228
$parameters = array_map(fn ($parameter) => $parameter instanceof TranslatableInterface ? $parameter->trans($this, $locale) : $parameter, $parameters);
213229

0 commit comments

Comments
 (0)