-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Contracts] add TranslatableInterface #38330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser; | ||
use Symfony\Bridge\Twig\TokenParser\TransTokenParser; | ||
use Symfony\Component\Translation\Translatable; | ||
use Symfony\Contracts\Translation\TranslatableInterface; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
use Symfony\Contracts\Translation\TranslatorTrait; | ||
use Twig\Extension\AbstractExtension; | ||
|
@@ -104,17 +105,24 @@ public function getTranslationNodeVisitor(): TranslationNodeVisitor | |
} | ||
|
||
/** | ||
* @param ?string|Translatable $message The message id (may also be an object that can be cast to string) | ||
* @param string|\Stringable|TranslatableInterface|null $message | ||
* @param array|string $arguments Can be the locale as a string when $message is a TranslatableInterface | ||
*/ | ||
public function trans($message, array $arguments = [], string $domain = null, string $locale = null, int $count = null): string | ||
public function trans($message, $arguments = [], string $domain = null, string $locale = null, int $count = null): string | ||
{ | ||
if ($message instanceof Translatable) { | ||
$arguments += $message->getParameters(); | ||
$domain = $message->getDomain(); | ||
$message = $message->getMessage(); | ||
if ($message instanceof TranslatableInterface) { | ||
if ([] !== $arguments && !\is_string($arguments)) { | ||
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be a locale passed as a string when the message is a "%s", "%s" given.', __METHOD__, TranslatableInterface::class, get_debug_type($arguments))); | ||
} | ||
|
||
return $message->trans($this->getTranslator(), $locale ?? (\is_string($arguments) ? $arguments : null)); | ||
} | ||
|
||
if (!\is_array($arguments)) { | ||
throw new \TypeError(sprintf('Unless the message is a "%s", argument 2 passed to "%s()" must be an array of parameters, "%s" given.', TranslatableInterface::class, __METHOD__, get_debug_type($arguments))); | ||
} | ||
|
||
if (null === $message || '' === $message) { | ||
if ('' === $message = (string) $message) { | ||
return ''; | ||
} | ||
|
||
|
@@ -125,8 +133,12 @@ public function trans($message, array $arguments = [], string $domain = null, st | |
return $this->getTranslator()->trans($message, $arguments, $domain, $locale); | ||
} | ||
|
||
public function createTranslatable(string $message, array $parameters = [], string $domain = 'messages'): Translatable | ||
public function createTranslatable(string $message, array $parameters = [], string $domain = null): Translatable | ||
{ | ||
if (!class_exists(Translatable::class)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it could be an anonymous data object, for general compatibility There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it could but I doubt this would provide many benefits. The dependency on the component is quite a loose one for someone using the extension. |
||
throw new \LogicException(sprintf('You cannot use the "%s" as the Translation Component is not installed. Try running "composer require symfony/translation".', __CLASS__)); | ||
} | ||
|
||
return new Translatable($message, $parameters, $domain); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation; | ||
|
||
/** | ||
* @author Nate Wiebe <nate@northern.co> | ||
*/ | ||
function t(string $message, array $parameters = [], string $domain = null): Translatable | ||
{ | ||
return new Translatable($message, $parameters, $domain); | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Contracts\Translation; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
interface TranslatableInterface | ||
{ | ||
public function trans(TranslatorInterface $translator, string $locale = null): string; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ | |
"minimum-stability": "dev", | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "2.2-dev" | ||
"dev-master": "2.3-dev" | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new semantics of the
$arguments
parameter is not covered by a test, is it? I also wonder if this change is really necessary. I find the error message rather confusing that a developer gets when passing an array of arguments to thetrans
filter (which can be an honest C&P mistake).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test case added as
{{ t("Hello")|trans("fr") }}
Not doing this polymorphism means that ppl would be forced to always write
{{ translateble|trans(locale=fr) }}
instead of{{ translateble|trans(fr) }}
which the current changes allows.If the issue is the error message, we could improve it. Suggestions welcome :)