forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make trans + %count% parameter resolve plurals
- Loading branch information
1 parent
d870a85
commit dc5f3bf
Showing
38 changed files
with
888 additions
and
431 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ | |
use Symfony\Bridge\Twig\TokenParser\TransChoiceTokenParser; | ||
use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser; | ||
use Symfony\Bridge\Twig\TokenParser\TransTokenParser; | ||
use Symfony\Component\Translation\LegacyTranslatorTrait; | ||
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
use Symfony\Contracts\Translation\TranslatorTrait; | ||
|
@@ -29,6 +28,8 @@ | |
* Provides integration of the Translation component with Twig. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
* | ||
* @final since Symfony 4.2 | ||
*/ | ||
class TranslationExtension extends AbstractExtension | ||
{ | ||
|
@@ -38,21 +39,28 @@ class TranslationExtension extends AbstractExtension | |
trans as private doTrans; | ||
} | ||
|
||
use LegacyTranslatorTrait { | ||
transChoice as private doTransChoice; | ||
} | ||
|
||
private $translator; | ||
private $translationNodeVisitor; | ||
|
||
public function __construct(TranslatorInterface $translator = null, NodeVisitorInterface $translationNodeVisitor = null) | ||
/** | ||
* @param TranslatorInterface|null $translator | ||
*/ | ||
public function __construct($translator = null, NodeVisitorInterface $translationNodeVisitor = null) | ||
{ | ||
if (null !== $translator && !$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) { | ||
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))); | ||
} | ||
$this->translator = $translator; | ||
$this->translationNodeVisitor = $translationNodeVisitor; | ||
} | ||
|
||
/** | ||
* @deprecated since Symfony 4.2 | ||
*/ | ||
public function getTranslator() | ||
{ | ||
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2.', __METHOD__), E_USER_DEPRECATED); | ||
|
||
return $this->translator; | ||
} | ||
|
||
|
@@ -63,7 +71,7 @@ public function getFilters() | |
{ | ||
return array( | ||
new TwigFilter('trans', array($this, 'trans')), | ||
new TwigFilter('transchoice', array($this, 'transchoice')), | ||
new TwigFilter('transchoice', array($this, 'transchoice'), array('deprecated' => '4.2', 'alternative' => 'trans" with parameter "%count%')), | ||
); | ||
} | ||
|
||
|
@@ -101,19 +109,28 @@ public function getTranslationNodeVisitor() | |
return $this->translationNodeVisitor ?: $this->translationNodeVisitor = new TranslationNodeVisitor(); | ||
} | ||
|
||
public function trans($message, array $arguments = array(), $domain = null, $locale = null) | ||
public function trans($message, array $arguments = array(), $domain = null, $locale = null, $count = null) | ||
{ | ||
if (null !== $count) { | ||
$arguments['%count%'] = $count; | ||
} | ||
if (null === $this->translator) { | ||
return $this->doTrans($message, $arguments, $domain, $locale); | ||
} | ||
|
||
return $this->translator->trans($message, $arguments, $domain, $locale); | ||
} | ||
|
||
/** | ||
* @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter | ||
*/ | ||
public function transchoice($message, $count, array $arguments = array(), $domain = null, $locale = null) | ||
{ | ||
if (null === $this->translator || !$this->translator instanceof LegacyTranslatorInterface) { | ||
return $this->doTransChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale); | ||
if (null === $this->translator) { | ||
return $this->doTrans($message, array_merge(array('%count%' => $count), $arguments), $domain, $locale); | ||
} | ||
if ($this->translator instanceof TranslatorInterface) { | ||
return $this->translator->trans($message, array_merge(array('%count%' => $count), $arguments), $domain, $locale); | ||
} | ||
|
||
return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ | |
* Token Parser for the 'transchoice' tag. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
* | ||
* @deprecated since Symfony 4.2, use the "trans" tag with a "%count%" parameter instead | ||
*/ | ||
class TransChoiceTokenParser extends TransTokenParser | ||
{ | ||
|
@@ -38,6 +40,8 @@ public function parse(Token $token) | |
$lineno = $token->getLine(); | ||
$stream = $this->parser->getStream(); | ||
|
||
@trigger_error(sprintf('The "transchoice" tag is deprecated since Symfony 4.2, use the "trans" one instead with a "%count%" parameter in %s line %d.', $stream->getSourceContext()->getName(), $lineno), E_USER_DEPRECATED); | ||
|
||
$vars = new ArrayExpression(array(), $lineno); | ||
|
||
$count = $this->parser->getExpressionParser()->parseExpression(); | ||
|
Oops, something went wrong.