-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Translator] Deprecated transChoice and moved it away from contracts #28375
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
Conversation
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.
I added some comments that I think will help the review.
UPGRADE-4.2.md
Outdated
@@ -155,6 +155,7 @@ Translation | |||
----------- | |||
|
|||
* The `TranslatorInterface` has been deprecated in favor of `Symfony\Contracts\Translation\TranslatorInterface` | |||
* The `Translator::transChoice()` has been deprecated in favor of using `Translator::trans()` with intl message format |
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.
I choose to use the implementation (Translator
) here. We already say that the full interface is deprecated.
* @param int $number The number of items represented for the message | ||
* @param string $locale The locale to use for choosing | ||
* @param string $message The message being translated | ||
* @param int|float $number The number of items represented for the message |
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.
We actually support floats.
private function getPluralizationRule(int $number, string $locale): int | ||
{ | ||
return PluralizationRules::get($number, $locale, false); | ||
return strtr($this->selector->choose((string) $id, $number, $locale ?: $this->getLocale()), $parameters); |
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.
FYI: In 4.1 we use (int) $number
.
@@ -20,4 +20,193 @@ public function getTranslator() | |||
{ | |||
return new IdentityTranslator(); | |||
} | |||
|
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.
These tests are moved from Contracts.
*/ | ||
interface TranslatorInterface extends BaseTranslatorInterface | ||
{ | ||
/** |
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.
Moved this back from Contracts
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) |
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.
This function was removed. It was a replacement for the deprecated MessageSelector
. I do not think it is needed anymore, right?
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.
right, same for getPluralizationRule below
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.
Open questions:
- what should happen when the intl extension is not installed?
- should be ship a command converting from one format to the other in this PR?
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) |
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.
right, same for getPluralizationRule below
I think we should ship with "
I think that is a good idea. But that is not a requirement for this PR (nor for 4.2). But the sooner the better. |
I think it would be friendly to make migrating to the new format as seamless as possible, running a command to update existing catalogs would be awesome. It should be shipped at the same time as the deprecation to me. If doable of course. We should also deprecate the twig helpers btw! (as highlighted by the CI ;) ) |
Okey. I'll will not do it in this PR. It is a lot of code and hard to review already. I will work on a migration script now and add that in a other PR.
Thank you. I saw those now. 👍 Label: Needs work |
Well, you cannot deprecate the And we also need to figure out the migration path between format. |
@@ -43,15 +41,11 @@ public function __construct(MessageSelector $selector = null) | |||
*/ | |||
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) | |||
{ | |||
if ($this->selector) { | |||
return strtr($this->selector->choose((string) $id, (int) $number, $locale ?: $this->getLocale()), $parameters); | |||
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.1, use the "trans()" method with intl formatted messages instead.', __METHOD__), E_USER_DEPRECATED); |
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.
4.1 ? That's too late for that
1a5de0a
to
868e9bd
Compare
I just rebased+squashed the PR by pushing on your fork. |
868e9bd
to
39fcccf
Compare
if ($this->translator instanceof LegacyTranslatorInterface) { | ||
$trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale); | ||
} else { | ||
$trans = $this->doTransChoice($id, $number, $parameters, $domain, $locale); |
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.
Is this correct to use the trait here and in the DataCollector?
@@ -141,7 +142,7 @@ public function setCause($cause) | |||
*/ | |||
public function addViolation() | |||
{ | |||
if (null === $this->plural) { | |||
if (null === $this->plural || !$this->translator instanceof LegacyTranslatorInterface) { |
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.
This will cause some deprecations. How can I know if I should use transChoice or not?
@stof Thank you for the review.
The migration path would be to run the About supporting both formats: That is a fair point, do you have any suggestions? Status: needs review |
562bdff
to
7cdef58
Compare
I pushed a new approach here, see 2nd commit: I think we should not deprecate the current plural format as this would create a too steep migration path for devs. Instead, I think we should make "trans()" able to resolve plurals. That's already the case when using the INTL message format, so let's make it work with the Symfony format. What is proposed now is to make the For Twig, There is a potential for conflicts with existing messages when these two conditions are met together:
We could choose another special parameter name if we think the risk is unreasonable (to me it's OK). Ready for review. |
7cdef58
to
943f23b
Compare
943f23b
to
54421ed
Compare
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.
I've reviewed this carefully. I think it looks alright.
About %count%
. I was considering to use #
because that is that INTL is using. However, the potential conflicts are larger and we do not need to take this step towards INTL format right now.
👍
$trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale); | ||
$this->collectMessage($locale, $domain, $id, $trans, $parameters, $number); | ||
|
||
$this->collectMessage($locale, $domain, $id, $trans, array('%count%' => $number) + $parameters); |
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.
I've always got problems when adding arrays. Shouldn't we use the more easy-to-read array_merge?
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.
array_merge is a function call, personally I prefer using the language directly.
{ | ||
if (null !== $translator && !$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) { | ||
throw new \TypeError(sprintf('Argument 5 passed to %s() must be an instance of %s, %s given', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator))); |
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.
missing . at the end of the exception message.
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.
fixed
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.
There are many other occurrences.
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.
oups, now all of them are fixed
54421ed
to
211e8af
Compare
211e8af
to
dc5f3bf
Compare
Thank you @Nyholm. |
…from contracts (Nyholm, nicolas-grekas) This PR was merged into the 4.2-dev branch. Discussion ---------- [Translator] Deprecated transChoice and moved it away from contracts | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | Issue: symfony/symfony-docs#10264 This will address comment made here: #27399 (review) This PR moves the `transChoice()` method away from Contracts and back to the component. I also reverted changes in the `IdentityTranslator`. I will still use the deprecated `MessageSelector` but this is not fine since `transChoice()` is also deprecated. Commits ------- dc5f3bf Make trans + %count% parameter resolve plurals d870a85 [Translator] Deprecated transChoice and moved it away from contracts
This will address comment made here: #27399 (review)
This PR moves the
transChoice()
method away from Contracts and back to the component. I also reverted changes in theIdentityTranslator
. I will still use the deprecatedMessageSelector
but this is not fine sincetransChoice()
is also deprecated.