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 d3ded2f

Browse filesBrowse files
author
Amrouche Hamza
committed
[Translation] TransChoice invalide plural translation
1 parent 95932df commit d3ded2f
Copy full SHA for d3ded2f

File tree

4 files changed

+25
-6
lines changed
Filter options

4 files changed

+25
-6
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/identity_translator.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/identity_translator.xml
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
<service id="Symfony\Component\Translation\TranslatorInterface" alias="translator" />
1111
<service id="Symfony\Contracts\Translation\TranslatorInterface" alias="translator" />
1212

13-
<service id="identity_translator" class="Symfony\Component\Translation\IdentityTranslator" />
13+
<service id="identity_translator" class="Symfony\Component\Translation\IdentityTranslator" >
14+
<argument/>
15+
<argument>%kernel.debug%</argument>
16+
</service>
1417
<service id="translator.selector" class="Symfony\Component\Translation\MessageSelector">
1518
<deprecated>The "%service_id%" service is deprecated since Symfony 4.2, use "identity_translator" instead.</deprecated>
1619
</service>

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/IdentityTranslator.php
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@ class IdentityTranslator implements LegacyTranslatorInterface, TranslatorInterfa
2525
use TranslatorTrait;
2626

2727
private $selector;
28+
private $behaveSilently;
2829

2930
/**
3031
* @param MessageSelector|null $selector The message selector for pluralization
3132
*/
32-
public function __construct(MessageSelector $selector = null)
33+
public function __construct(MessageSelector $selector = null, bool $behaveSilently = false)
3334
{
3435
$this->selector = $selector;
36+
$this->behaveSilently = $behaveSilently;
3537

36-
if (__CLASS__ !== \get_class($this)) {
37-
@trigger_error(sprintf('Calling "%s()" is deprecated since Symfony 4.2.', __METHOD__), E_USER_DEPRECATED);
38+
if (__CLASS__ !== \get_class($this) && null !== $this->selector) {
39+
@trigger_error(sprintf('Passing %s in "%s()" is deprecated since Symfony 4.2.', MessageSelector::class,__METHOD__), E_USER_DEPRECATED);
3840
}
3941
}
4042

@@ -51,7 +53,7 @@ public function transChoice($id, $number, array $parameters = [], $domain = null
5153
return strtr($this->selector->choose((string) $id, $number, $locale ?: $this->getLocale()), $parameters);
5254
}
5355

54-
return $this->trans($id, ['%count%' => $number] + $parameters, $domain, $locale);
56+
return $this->trans($id, ['%count%' => $number] + $parameters, $domain, $locale, $this->behaveSilently);
5557
}
5658

5759
private function getPluralizationRule(int $number, string $locale): int

‎src/Symfony/Contracts/Tests/Translation/TranslatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Contracts/Tests/Translation/TranslatorTest.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ public function testThrowExceptionIfMatchingMessageCannotBeFound($id, $number)
167167
$translator->trans($id, ['%count%' => $number]);
168168
}
169169

170+
/**
171+
* @dataProvider getNonMatchingMessages
172+
*/
173+
public function testNotThrowExceptionIfMatchingMessageCannotBeFound($id, $number)
174+
{
175+
$translator = $this->getTranslator();
176+
177+
$this->assertEmpty($translator->trans($id, ['%count%' => $number], null, null, true));
178+
}
179+
170180
public function getNonMatchingMessages()
171181
{
172182
return [

‎src/Symfony/Contracts/Translation/TranslatorTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Contracts/Translation/TranslatorTrait.php
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function getLocale()
4141
/**
4242
* {@inheritdoc}
4343
*/
44-
public function trans($id, array $parameters = [], $domain = null, $locale = null)
44+
public function trans($id, array $parameters = [], $domain = null, $locale = null, $behaveSilently = false)
4545
{
4646
$id = (string) $id;
4747

@@ -115,6 +115,10 @@ public function trans($id, array $parameters = [], $domain = null, $locale = nul
115115
return strtr($standardRules[0], $parameters);
116116
}
117117

118+
if (true === $behaveSilently) {
119+
return;
120+
}
121+
118122
$message = sprintf('Unable to choose a translation for "%s" with locale "%s" for value "%d". Double check that this translation has the correct plural options (e.g. "There is one apple|There are %%count%% apples").', $id, $locale, $number);
119123

120124
if (\class_exists(InvalidArgumentException::class)) {

0 commit comments

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