-
-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
[Translator] Deprecated transChoice and moved it away from contracts
- Loading branch information
commit d870a850bd71fd1fc0b340cc291f62868f52d317
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
110 changes: 110 additions & 0 deletions
110
src/Symfony/Component/Translation/LegacyTranslatorTrait.php
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?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; | ||
|
||
use Symfony\Component\Translation\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* | ||
* @deprecated since Symfony 4.2, use IdentityTranslator instead | ||
*/ | ||
trait LegacyTranslatorTrait | ||
{ | ||
/** | ||
* Implementation of Symfony\Component\Translation\TranslationInterface::transChoice | ||
* {@inheritdoc} | ||
*/ | ||
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) | ||
{ | ||
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the "trans()" method with intl formatted messages instead.', __METHOD__), E_USER_DEPRECATED); | ||
|
||
$id = (string) $id; | ||
$number = (float) $number; | ||
$locale = (string) $locale ?: $this->getLocale(); | ||
|
||
$parts = array(); | ||
if (preg_match('/^\|++$/', $id)) { | ||
$parts = explode('|', $id); | ||
} elseif (preg_match_all('/(?:\|\||[^\|])++/', $id, $matches)) { | ||
$parts = $matches[0]; | ||
} | ||
|
||
$intervalRegexp = <<<'EOF' | ||
/^(?P<interval> | ||
({\s* | ||
(\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*) | ||
\s*}) | ||
|
||
| | ||
|
||
(?P<left_delimiter>[\[\]]) | ||
\s* | ||
(?P<left>-Inf|\-?\d+(\.\d+)?) | ||
\s*,\s* | ||
(?P<right>\+?Inf|\-?\d+(\.\d+)?) | ||
\s* | ||
(?P<right_delimiter>[\[\]]) | ||
)\s*(?P<message>.*?)$/xs | ||
EOF; | ||
|
||
$standardRules = array(); | ||
foreach ($parts as $part) { | ||
$part = trim(str_replace('||', '|', $part)); | ||
|
||
// try to match an explicit rule, then fallback to the standard ones | ||
if (preg_match($intervalRegexp, $part, $matches)) { | ||
if ($matches[2]) { | ||
foreach (explode(',', $matches[3]) as $n) { | ||
F987 | if ($number == $n) { | |
return strtr($matches['message'], $parameters); | ||
} | ||
} | ||
} else { | ||
$leftNumber = '-Inf' === $matches['left'] ? -INF : (float) $matches['left']; | ||
$rightNumber = \is_numeric($matches['right']) ? (float) $matches['right'] : INF; | ||
|
||
if (('[' === $matches['left_delimiter'] ? $number >= $leftNumber : $number > $leftNumber) | ||
&& (']' === $matches['right_delimiter'] ? $number <= $rightNumber : $number < $rightNumber) | ||
) { | ||
return strtr($matches['message'], $parameters); | ||
} | ||
} | ||
} elseif (preg_match('/^\w+\:\s*(.*?)$/', $part, $matches)) { | ||
$standardRules[] = $matches[1]; | ||
} else { | ||
$standardRules[] = $part; | ||
} | ||
} | ||
|
||
$position = PluralizationRules::get($number, $locale, false); | ||
|
||
if (!isset($standardRules[$position])) { | ||
// when there's exactly one rule given, and that rule is a standard | ||
// rule, use this rule | ||
if (1 === \count($parts) && isset($standardRules[0])) { | ||
return strtr($standardRules[0], $parameters); | ||
} | ||
|
||
$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); | ||
|
||
if (\class_exists(InvalidArgumentException::class)) { | ||
throw new InvalidArgumentException($message); | ||
} | ||
|
||
throw new \InvalidArgumentException($message); | ||
} | ||
|
||
return strtr($standardRules[$position], $parameters); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -43,9 +43,9 @@ class MessageSelector | |
* The two methods can also be mixed: | ||
* {0} There are no apples|one: There is one apple|more: There are %count% apples | ||
* | ||
* @param string $message The message being translated | ||
* @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 commentThe reason will be displayed to describe this comment to others. Learn more. We actually support floats. |
||
* @param string $locale The locale to use for choosing | ||
* | ||
* @return string | ||
* | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?