Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Translation] added support for adding custom message formatter#18314
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -234,6 +234,27 @@ Translation | ||
| and will be removed in 4.0, use `Symfony\Component\Translation\Writer\TranslationWriter::write` | ||
| instead. | ||
| * Passing a `Symfony\Component\Translation\MessageSelector` to `Translator` has been | ||
Member 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. Should probably be added to the component's changelog file too. And we also need to update the ContributorAuthor 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. done | ||
| deprecated. You should pass a message formatter instead | ||
| Before: | ||
| ```php | ||
| use Symfony\Component\Translation\Translator; | ||
| use Symfony\Component\Translation\MessageSelector; | ||
| $translator = new Translator('fr_FR', new MessageSelector()); | ||
| ``` | ||
| After: | ||
| ```php | ||
| use Symfony\Component\Translation\Translator; | ||
| use Symfony\Component\Translation\Formatter\MessageFormatter; | ||
| $translator = new Translator('fr_FR', new MessageFormatter()); | ||
| ``` | ||
| TwigBridge | ||
| ---------- | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?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\Formatter; | ||
| /** | ||
| * @author Abdellatif Ait boudad <a.aitboudad@gmail.com> | ||
| */ | ||
| interface ChoiceMessageFormatterInterface | ||
| { | ||
| /** | ||
| * Formats a localized message pattern with given arguments. | ||
| * | ||
| * @param string $message The message (may also be an object that can be cast to string) | ||
| * @param int $number The number to use to find the indice of the message | ||
| * @param string $locale The message locale | ||
| * @param array $parameters An array of parameters for the message | ||
| * | ||
| * @return string | ||
| */ | ||
| public function choiceFormat($message, $number, $locale, array $parameters = array()); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?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\Formatter; | ||
| use Symfony\Component\Translation\MessageSelector; | ||
| /** | ||
| * @author Abdellatif Ait boudad <a.aitboudad@gmail.com> | ||
| */ | ||
| class MessageFormatter implements MessageFormatterInterface, ChoiceMessageFormatterInterface | ||
| { | ||
| private $selector; | ||
| /** | ||
| * @param MessageSelector|null $selector The message selector for pluralization | ||
| */ | ||
| public function __construct(MessageSelector $selector = null) | ||
| { | ||
| $this->selector = $selector ?: new MessageSelector(); | ||
| } | ||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function format($message, $locale, array $parameters = array()) | ||
| { | ||
| return strtr($message, $parameters); | ||
| } | ||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function choiceFormat($message, $number, $locale, array $parameters = array()) | ||
| { | ||
| $parameters = array_merge(array('%count%' => $number), $parameters); | ||
| return $this->format($this->selector->choose($message, (int) $number, $locale), $locale, $parameters); | ||
Contributor 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. Same question for the int. ContributorAuthor 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. I prefer to keep this one as it's used before and might break some apps if we remove it. | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?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. | ||
| */ | ||
| namespaceSymfony\Component\Translation\Formatter; | ||
| /** | ||
| * @author Guilherme Blanco <guilhermeblanco@hotmail.com> | ||
| * @author Abdellatif Ait boudad <a.aitboudad@gmail.com> | ||
| */ | ||
| interface MessageFormatterInterface | ||
| { | ||
| /** | ||
| * Formats a localized message pattern with given arguments. | ||
| * | ||
| * @param string $message The message (may also be an object that can be cast to string) | ||
| * @param string $locale The message locale | ||
| * @param array $parameters An array of parameters for the message | ||
| * | ||
| * @return string | ||
| */ | ||
| publicfunctionformat($message,$locale,array$parameters =array()); | ||
Member 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. I fail to understand why we would need $locale here. We do not use it and I can't come up with any valid example were it is needed. ContributorAuthor 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. see the use-caseIntlMessageFormatter (#20007) Member 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. Thank you | ||
| } | ||
Uh oh!
There was an error while loading.Please reload this page.