Skip to content

Commit 61483de

Browse files
Make trans + %count% parameter resolve plurals
1 parent febeff3 commit 61483de

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CHANGELOG
1818
the "APP_ENV" environment variable instead.
1919
* Deprecated the `--no-debug` console option, set the "APP_DEBUG"
2020
environment variable to "0" instead.
21+
* Deprecated the `Templating\Helper\TranslatorHelper::transChoice()` method, use the `trans()` one instead with a `%count%` parameter
2122

2223
4.1.0
2324
-----

Command/TranslationDebugCommand.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\Component\Translation\MessageCatalogue;
2727
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
2828
use Symfony\Component\Translation\Translator;
29+
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
2930
use Symfony\Contracts\Translation\TranslatorInterface;
3031

3132
/**
@@ -50,8 +51,14 @@ class TranslationDebugCommand extends Command
5051
private $defaultTransPath;
5152
private $defaultViewsPath;
5253

53-
public function __construct(TranslatorInterface $translator, TranslationReaderInterface $reader, ExtractorInterface $extractor, string $defaultTransPath = null, string $defaultViewsPath = null)
54+
/**
55+
* @param TranslatorInterface $translator
56+
*/
57+
public function __construct($translator, TranslationReaderInterface $reader, ExtractorInterface $extractor, string $defaultTransPath = null, string $defaultViewsPath = null)
5458
{
59+
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
60+
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
61+
}
5562
parent::__construct();
5663

5764
$this->translator = $translator;

Templating/Helper/TranslatorHelper.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
1313

1414
use Symfony\Component\Templating\Helper\Helper;
15-
use Symfony\Component\Translation\LegacyTranslatorTrait;
1615
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
1716
use Symfony\Contracts\Translation\TranslatorInterface;
1817
use Symfony\Contracts\Translation\TranslatorTrait;
@@ -28,14 +27,16 @@ class TranslatorHelper extends Helper
2827
trans as private doTrans;
2928
}
3029

31-
use LegacyTranslatorTrait {
32-
transChoice as private doTransChoice;
33-
}
34-
3530
protected $translator;
3631

37-
public function __construct(TranslatorInterface $translator = null)
32+
/**
33+
* @param TranslatorInterface|null $translator
34+
*/
35+
public function __construct($translator = null)
3836
{
37+
if (null !== $translator && !$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
38+
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
39+
}
3940
$this->translator = $translator;
4041
}
4142

@@ -53,11 +54,17 @@ public function trans($id, array $parameters = array(), $domain = 'messages', $l
5354

5455
/**
5556
* @see TranslatorInterface::transChoice()
57+
* @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter
5658
*/
5759
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
5860
{
59-
if (null === $this->translator || !$this->translator instanceof LegacyTranslatorInterface) {
60-
return $this->doTransChoice($id, $number, $parameters, $domain, $locale);
61+
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the trans() one instead with a "%count%" parameter.', __METHOD__), E_USER_DEPRECATED);
62+
63+
if (null === $this->translator) {
64+
return $this->doTrans($id, array('%count%' => $number) + $parameters, $domain, $locale);
65+
}
66+
if ($this->translator instanceof TranslatorInterface) {
67+
return $this->translator->trans($id, array('%count%' => $number) + $parameters, $domain, $locale);
6168
}
6269

6370
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);

Tests/Translation/TranslatorTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ public function testTransWithoutCaching()
5252
$this->assertEquals('foo (FR)', $translator->trans('foo'));
5353
$this->assertEquals('bar (EN)', $translator->trans('bar'));
5454
$this->assertEquals('foobar (ES)', $translator->trans('foobar'));
55+
$this->assertEquals('choice 0 (EN)', $translator->trans('choice', array('%count%' => 0)));
5556
$this->assertEquals('no translation', $translator->trans('no translation'));
5657
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
58+
$this->assertEquals('other choice 1 (PT-BR)', $translator->trans('other choice', array('%count%' => 1)));
5759
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
5860
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
5961
}
@@ -81,8 +83,10 @@ public function testTransWithCaching()
8183
$this->assertEquals('foo (FR)', $translator->trans('foo'));
8284
$this->assertEquals('bar (EN)', $translator->trans('bar'));
8385
$this->assertEquals('foobar (ES)', $translator->trans('foobar'));
86+
$this->assertEquals('choice 0 (EN)', $translator->trans('choice', array('%count%' => 0)));
8487
$this->assertEquals('no translation', $translator->trans('no translation'));
8588
$this->assertEquals('foobarfoo (PT-PT)', $translator->trans('foobarfoo'));
89+
$this->assertEquals('other choice 1 (PT-BR)', $translator->trans('other choice', array('%count%' => 1)));
8690
$this->assertEquals('foobarbaz (fr.UTF-8)', $translator->trans('foobarbaz'));
8791
$this->assertEquals('foobarbax (sr@latin)', $translator->trans('foobarbax'));
8892

0 commit comments

Comments
 (0)