Skip to content

Commit 02f31ac

Browse files
committed
[Translation] create custom message formatter.
1 parent d410c46 commit 02f31ac

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

components/translation.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ The constructor of the ``Translator`` class needs one argument: The locale.
3434
.. code-block:: php
3535
3636
use Symfony\Component\Translation\Translator;
37-
use Symfony\Component\Translation\MessageSelector;
3837
39-
$translator = new Translator('fr_FR', new MessageSelector());
38+
$translator = new Translator('fr_FR');
4039
4140
.. note::
4241

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
.. index::
2+
single: Translation; Create Custom Message formatter
3+
4+
Create Custom Message Formatter
5+
===============================
6+
7+
The default Message Formatter provide a simple and easy way that deals with the most common use-cases
8+
such as message placeholders and pluralization. But in some cases, you may want to use a custom message formatter
9+
that fit to your specific needs, for example, handle nested conditions of pluralization or select sub-messages
10+
via a fixed set of keywords (e.g. gender).
11+
12+
Suppose in your application you want to displays different text depending on arbitrary conditions,
13+
for example upon whether the guest is male or female. To do this, we will use the `ICU Message Format`_
14+
which the most suitable ones you first need to create a `IntlMessageFormatter` and pass it to the `Translator`.
15+
16+
.. _components-translation-message-formatter:
17+
18+
Creating a Custom Message Formatter
19+
-----------------------------------
20+
21+
To define a custom message formatter that is able to read these kinds of rules, you must create a
22+
new class that implements the
23+
:class:`Symfony\\Component\\Translation\\Formatter\\MessageFormatterInterface`::
24+
25+
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
26+
27+
class IntlMessageFormatter implements MessageFormatterInterface
28+
{
29+
public function format($message, $locale, array $parameters = array())
30+
{
31+
$formatter = new \MessageFormatter($locale, $message);
32+
if (null === $formatter) {
33+
throw new \InvalidArgumentException(sprintf('Invalid message format. Reason: %s (error #%d)', intl_get_error_message(), intl_get_error_code()));
34+
}
35+
36+
$message = $formatter->format($parameters);
37+
if ($formatter->getErrorCode() !== U_ZERO_ERROR) {
38+
throw new \InvalidArgumentException(sprintf('Unable to format message. Reason: %s (error #%s)', $formatter->getErrorMessage(), $formatter->getErrorCode()));
39+
}
40+
41+
return $message;
42+
}
43+
}
44+
45+
Once created, simply pass it as the second argument to the `Translator`::
46+
47+
use Symfony\Component\Translation\Translator;
48+
49+
$translator = new Translator('fr_FR', new IntlMessageFormatter());
50+
51+
var_dump($translator->trans('The guest is {gender, select, m {male} f {female}}', [ 'gender' => 'm' ]));
52+
53+
It will print *"The guest is male"*.
54+
55+
.. _`ICU Message Format`: http://userguide.icu-project.org/formatparse/messages

reference/configuration/framework.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Configuration
8585
* :ref:`enabled <reference-translator-enabled>`
8686
* `fallbacks`_
8787
* `logging`_
88+
* `formatter`_
8889
* :ref:`paths <reference-translator-paths>`
8990
* `property_access`_
9091
* `magic_call`_
@@ -1481,6 +1482,20 @@ for a given key. The logs are made to the ``translation`` channel and at the
14811482
``debug`` for level for keys where there is a translation in the fallback
14821483
locale and the ``warning`` level if there is no translation to use at all.
14831484

1485+
.. _reference-framework-translator-formatter:
1486+
1487+
formatter
1488+
.........
1489+
1490+
**type**: ``string`` **default**: ``translator.formatter.default``
1491+
1492+
The service that is used to format message. The service
1493+
has to implement the :class:`Symfony\\Component\\Translation\\Formatter\\MessageFormatterInterface`.
1494+
1495+
.. seealso::
1496+
1497+
For more details, see :doc:`/components/translation/custom_message_formatter`.
1498+
14841499
.. _reference-translator-paths:
14851500

14861501
paths

0 commit comments

Comments
 (0)