Skip to content

Commit 942143c

Browse files
committed
Merge branch '4.3' into 4.4
* 4.3: [Translation] create custom message formatter.
2 parents 8bbfac5 + 5f1554f commit 942143c

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
@@ -231,6 +231,7 @@ Configuration
231231
* :ref:`default_path <reference-translator-default_path>`
232232
* :ref:`enabled <reference-translator-enabled>`
233233
* `fallbacks`_
234+
* `formatter`_
234235
* `logging`_
235236
* :ref:`paths <reference-translator-paths>`
236237

@@ -2141,6 +2142,20 @@ for a given key. The logs are made to the ``translation`` channel and at the
21412142
``debug`` for level for keys where there is a translation in the fallback
21422143
locale and the ``warning`` level if there is no translation to use at all.
21432144

2145+
.. _reference-framework-translator-formatter:
2146+
2147+
formatter
2148+
.........
2149+
2150+
**type**: ``string`` **default**: ``translator.formatter.default``
2151+
2152+
The ID of the service used to format translation messages. The service class
2153+
must implement the :class:`Symfony\\Component\\Translation\\Formatter\\MessageFormatterInterface`.
2154+
2155+
.. seealso::
2156+
2157+
For more details, see :doc:`/components/translation/custom_message_formatter`.
2158+
21442159
.. _reference-translator-paths:
21452160

21462161
paths

0 commit comments

Comments
 (0)