Skip to content

Commit dabf7b3

Browse files
committed
Reword
1 parent f1388a6 commit dabf7b3

File tree

2 files changed

+19
-36
lines changed

2 files changed

+19
-36
lines changed
Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,38 @@
11
.. index::
22
single: Translation; Create Custom Message formatter
33

4-
Create Custom Message Formatter
5-
===============================
4+
Create a Custom Message Formatter
5+
=================================
66

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).
7+
The default message formatter provided by Symfony solves the most common needs
8+
when translating messages, such as using variables and pluralization. However,
9+
if your needs are different, you can create your own message formatter.
1110

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
11+
Message formatters are PHP classes that implement the
2312
:class:`Symfony\\Component\\Translation\\Formatter\\MessageFormatterInterface`::
2413

14+
2515
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
2616

27-
class IntlMessageFormatter implements MessageFormatterInterface
17+
class MyCustomMessageFormatter implements MessageFormatterInterface
2818
{
29-
public function format($message, $locale, array $parameters = array())
19+
public function format($message, $locale, array $parameters = [])
3020
{
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-
}
21+
// ... format the message according to your needs
4022

4123
return $message;
4224
}
4325
}
4426

45-
Once created, simply pass it as the second argument to the `Translator`::
27+
Now, pass an instance of this formatter as the second argument of the translator
28+
to use it when translating messages::
4629

4730
use Symfony\Component\Translation\Translator;
4831

4932
$translator = new Translator('fr_FR', new IntlMessageFormatter());
33+
$message = $translator->trans($originalMessage, $translationParameters);
5034

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
35+
If you want to use this formatter to translate all messages in your Symfony
36+
application, define a service for the formatter and use the
37+
:ref:`translator.formatter <reference-framework-translator-formatter>` option
38+
to set that service as the default formatter.

reference/configuration/framework.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,8 +1715,8 @@ formatter
17151715

17161716
**type**: ``string`` **default**: ``translator.formatter.default``
17171717

1718-
The service that is used to format message. The service
1719-
has to implement the :class:`Symfony\\Component\\Translation\\Formatter\\MessageFormatterInterface`.
1718+
The ID of the service used to format translation messages. The service class
1719+
must implement the :class:`Symfony\\Component\\Translation\\Formatter\\MessageFormatterInterface`.
17201720

17211721
.. seealso::
17221722

0 commit comments

Comments
 (0)