Version 1
[yaffs-website] / vendor / symfony / validator / DefaultTranslator.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator;
13
14 @trigger_error('The class '.__NAMESPACE__.'\DefaultTranslator is deprecated since version 2.7 and will be removed in 3.0. Use Symfony\Component\Translation\IdentityTranslator instead.', E_USER_DEPRECATED);
15
16 use Symfony\Component\Translation\TranslatorInterface;
17 use Symfony\Component\Validator\Exception\BadMethodCallException;
18 use Symfony\Component\Validator\Exception\InvalidArgumentException;
19
20 /**
21  * Simple translator implementation that simply replaces the parameters in
22  * the message IDs.
23  *
24  * Example usage:
25  *
26  *     $translator = new DefaultTranslator();
27  *
28  *     echo $translator->trans(
29  *         'This is a {{ var }}.',
30  *         array('{{ var }}' => 'donkey')
31  *     );
32  *
33  *     // -> This is a donkey.
34  *
35  *     echo $translator->transChoice(
36  *         'This is {{ count }} donkey.|These are {{ count }} donkeys.',
37  *         3,
38  *         array('{{ count }}' => 'three')
39  *     );
40  *
41  *     // -> These are three donkeys.
42  *
43  * This translator does not support message catalogs, translation domains or
44  * locales. Instead, it implements a subset of the capabilities of
45  * {@link \Symfony\Component\Translation\Translator} and can be used in places
46  * where translation is not required by default but should be optional.
47  *
48  * @deprecated since version 2.7, to be removed in 3.0. Use Symfony\Component\Translation\IdentityTranslator instead.
49  *
50  * @author Bernhard Schussek <bschussek@gmail.com>
51  */
52 class DefaultTranslator implements TranslatorInterface
53 {
54     /**
55      * Interpolates the given message.
56      *
57      * Parameters are replaced in the message in the same manner that
58      * {@link strtr()} uses.
59      *
60      * Example usage:
61      *
62      *     $translator = new DefaultTranslator();
63      *
64      *     echo $translator->trans(
65      *         'This is a {{ var }}.',
66      *         array('{{ var }}' => 'donkey')
67      *     );
68      *
69      *     // -> This is a donkey.
70      *
71      * @param string $id         The message id
72      * @param array  $parameters An array of parameters for the message
73      * @param string $domain     Ignored
74      * @param string $locale     Ignored
75      *
76      * @return string The interpolated string
77      */
78     public function trans($id, array $parameters = array(), $domain = null, $locale = null)
79     {
80         return strtr($id, $parameters);
81     }
82
83     /**
84      * Interpolates the given choice message by choosing a variant according to a number.
85      *
86      * The variants are passed in the message ID using the format
87      * "<singular>|<plural>". "<singular>" is chosen if the passed $number is
88      * exactly 1. "<plural>" is chosen otherwise.
89      *
90      * This format is consistent with the format supported by
91      * {@link \Symfony\Component\Translation\Translator}, but it does not
92      * have the same expressiveness. While Translator supports intervals in
93      * message translations, which are needed for languages other than English,
94      * this translator does not. You should use Translator or a custom
95      * implementation of {@link \Symfony\Component\Translation\TranslatorInterface} if you need this or similar
96      * functionality.
97      *
98      * Example usage:
99      *
100      *     echo $translator->transChoice(
101      *         'This is {{ count }} donkey.|These are {{ count }} donkeys.',
102      *         0,
103      *         array('{{ count }}' => 0)
104      *     );
105      *
106      *     // -> These are 0 donkeys.
107      *
108      *     echo $translator->transChoice(
109      *         'This is {{ count }} donkey.|These are {{ count }} donkeys.',
110      *         1,
111      *         array('{{ count }}' => 1)
112      *     );
113      *
114      *     // -> This is 1 donkey.
115      *
116      *     echo $translator->transChoice(
117      *         'This is {{ count }} donkey.|These are {{ count }} donkeys.',
118      *         3,
119      *         array('{{ count }}' => 3)
120      *     );
121      *
122      *     // -> These are 3 donkeys.
123      *
124      * @param string $id         The message id
125      * @param int    $number     The number to use to find the index of the message
126      * @param array  $parameters An array of parameters for the message
127      * @param string $domain     Ignored
128      * @param string $locale     Ignored
129      *
130      * @return string The translated string
131      *
132      * @throws InvalidArgumentException If the message id does not have the format
133      *                                  "singular|plural".
134      */
135     public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
136     {
137         $ids = explode('|', $id);
138
139         if (1 == $number) {
140             return strtr($ids[0], $parameters);
141         }
142
143         if (!isset($ids[1])) {
144             throw new InvalidArgumentException(sprintf('The message "%s" cannot be pluralized, because it is missing a plural (e.g. "There is one apple|There are %%count%% apples").', $id));
145         }
146
147         return strtr($ids[1], $parameters);
148     }
149
150     /**
151      * Not supported.
152      *
153      * @param string $locale The locale
154      *
155      * @throws BadMethodCallException
156      */
157     public function setLocale($locale)
158     {
159         throw new BadMethodCallException('Unsupported method.');
160     }
161
162     /**
163      * Returns the locale of the translator.
164      *
165      * @return string Always returns 'en'
166      */
167     public function getLocale()
168     {
169         return 'en';
170     }
171 }