Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #18314 [Translation] added support for adding custom message …
…formatter (aitboudad) This PR was merged into the 3.4 branch. Discussion ---------- [Translation] added support for adding custom message formatter | Q | A | | --- | --- | | Branch? | master | | Bug fix? | no | | New feature? | yes | | BC breaks? | no | | Deprecations? | yes | | Tests pass? | yes | | Fixed tickets | #6009, #10152, one item in #11742, #11948 | | License | MIT | | Doc PR | ~ | Commits ------- 42183b0 [Translation] Support adding custom message formatter
- Loading branch information
Showing
17 changed files
with
282 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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
30 changes: 30 additions & 0 deletions
30
src/Symfony/Component/Translation/Formatter/ChoiceMessageFormatterInterface.php
This file contains 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,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()); | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Symfony/Component/Translation/Formatter/MessageFormatter.php
This file contains 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,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); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Symfony/Component/Translation/Formatter/MessageFormatterInterface.php
This file contains 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,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()); | ||
} |
Oops, something went wrong.