Skip to content

Commit f1388a6

Browse files
committed
minor #8284 [Translation] add how to create custom message formatter. (aitboudad)
This PR was merged into the 3.4 branch. Discussion ---------- [Translation] add how to create custom message formatter. | Q | A | | --- | --- | | Doc fix? | no | | New docs? | yes | | Applies to | 3.4 | | Fixed tickets | symfony/symfony#18314 | Commits ------- 02f31ac [Translation] create custom message formatter.
2 parents 5a3c1f6 + 02f31ac commit f1388a6

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
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
@@ -176,6 +176,7 @@ Configuration
176176
* :ref:`default_path <reference-translator-default_path>`
177177
* :ref:`enabled <reference-translator-enabled>`
178178
* `fallbacks`_
179+
* `formatter`_
179180
* `logging`_
180181
* :ref:`paths <reference-translator-paths>`
181182

@@ -1707,6 +1708,20 @@ for a given key. The logs are made to the ``translation`` channel and at the
17071708
``debug`` for level for keys where there is a translation in the fallback
17081709
locale and the ``warning`` level if there is no translation to use at all.
17091710

1711+
.. _reference-framework-translator-formatter:
1712+
1713+
formatter
1714+
.........
1715+
1716+
**type**: ``string`` **default**: ``translator.formatter.default``
1717+
1718+
The service that is used to format message. The service
1719+
has to implement the :class:`Symfony\\Component\\Translation\\Formatter\\MessageFormatterInterface`.
1720+
1721+
.. seealso::
1722+
1723+
For more details, see :doc:`/components/translation/custom_message_formatter`.
1724+
17101725
.. _reference-translator-paths:
17111726

17121727
paths

0 commit comments

Comments
 (0)