-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 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 |
---|---|---|
@@ -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); | ||
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. 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. | ||
*/ | ||
|
||
namespace Symfony\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 | ||
*/ | ||
public function format($message, $locale, array $parameters = array()); | ||
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. 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-case IntlMessageFormatter (#20007) 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 |
||
} |
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.
Should probably be added to the component's changelog file too. And we also need to update the
UPGRADE-4.0.md
file accordingly.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.
done