Skip to content

Commit 6a5b2a7

Browse files
committed
Merge branch '3.4' into 4.3
* 3.4: [Translation] create custom message formatter.
2 parents a057cf4 + dabf7b3 commit 6a5b2a7

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.. index::
2+
single: Translation; Create Custom Message formatter
3+
4+
Create a Custom Message Formatter
5+
=================================
6+
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.
10+
11+
Message formatters are PHP classes that implement the
12+
:class:`Symfony\\Component\\Translation\\Formatter\\MessageFormatterInterface`::
13+
14+
15+
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
16+
17+
class MyCustomMessageFormatter implements MessageFormatterInterface
18+
{
19+
public function format($message, $locale, array $parameters = [])
20+
{
21+
// ... format the message according to your needs
22+
23+
return $message;
24+
}
25+
}
26+
27+
Now, pass an instance of this formatter as the second argument of the translator
28+
to use it when translating messages::
29+
30+
use Symfony\Component\Translation\Translator;
31+
32+
$translator = new Translator('fr_FR', new IntlMessageFormatter());
33+
$message = $translator->trans($originalMessage, $translationParameters);
34+
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ Configuration
227227
* :ref:`default_path <reference-translator-default_path>`
228228
* :ref:`enabled <reference-translator-enabled>`
229229
* `fallbacks`_
230+
* `formatter`_
230231
* `logging`_
231232
* :ref:`paths <reference-translator-paths>`
232233

@@ -2090,6 +2091,20 @@ for a given key. The logs are made to the ``translation`` channel and at the
20902091
``debug`` for level for keys where there is a translation in the fallback
20912092
locale and the ``warning`` level if there is no translation to use at all.
20922093

2094+
.. _reference-framework-translator-formatter:
2095+
2096+
formatter
2097+
.........
2098+
2099+
**type**: ``string`` **default**: ``translator.formatter.default``
2100+
2101+
The ID of the service used to format translation messages. The service class
2102+
must implement the :class:`Symfony\\Component\\Translation\\Formatter\\MessageFormatterInterface`.
2103+
2104+
.. seealso::
2105+
2106+
For more details, see :doc:`/components/translation/custom_message_formatter`.
2107+
20932108
.. _reference-translator-paths:
20942109

20952110
paths

0 commit comments

Comments
 (0)