|
1 | 1 | .. index:: |
2 | 2 | single: Translation; Create Custom Message formatter |
3 | 3 |
|
4 | | -Create Custom Message Formatter |
5 | | -=============================== |
| 4 | +Create a Custom Message Formatter |
| 5 | +================================= |
6 | 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). |
| 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. |
11 | 10 |
|
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 |
23 | 12 | :class:`Symfony\\Component\\Translation\\Formatter\\MessageFormatterInterface`:: |
24 | 13 |
|
| 14 | + |
25 | 15 | use Symfony\Component\Translation\Formatter\MessageFormatterInterface; |
26 | 16 |
|
27 | | - class IntlMessageFormatter implements MessageFormatterInterface |
| 17 | + class MyCustomMessageFormatter implements MessageFormatterInterface |
28 | 18 | { |
29 | | - public function format($message, $locale, array $parameters = array()) |
| 19 | + public function format($message, $locale, array $parameters = []) |
30 | 20 | { |
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 |
40 | 22 |
|
41 | 23 | return $message; |
42 | 24 | } |
43 | 25 | } |
44 | 26 |
|
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:: |
46 | 29 |
|
47 | 30 | use Symfony\Component\Translation\Translator; |
48 | 31 |
|
49 | 32 | $translator = new Translator('fr_FR', new IntlMessageFormatter()); |
| 33 | + $message = $translator->trans($originalMessage, $translationParameters); |
50 | 34 |
|
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. |
0 commit comments