4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator;
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);
16 use Symfony\Component\Translation\TranslatorInterface;
17 use Symfony\Component\Validator\Exception\BadMethodCallException;
18 use Symfony\Component\Validator\Exception\InvalidArgumentException;
21 * Simple translator implementation that simply replaces the parameters in
26 * $translator = new DefaultTranslator();
28 * echo $translator->trans(
29 * 'This is a {{ var }}.',
30 * array('{{ var }}' => 'donkey')
33 * // -> This is a donkey.
35 * echo $translator->transChoice(
36 * 'This is {{ count }} donkey.|These are {{ count }} donkeys.',
38 * array('{{ count }}' => 'three')
41 * // -> These are three donkeys.
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.
48 * @deprecated since version 2.7, to be removed in 3.0. Use Symfony\Component\Translation\IdentityTranslator instead.
50 * @author Bernhard Schussek <bschussek@gmail.com>
52 class DefaultTranslator implements TranslatorInterface
55 * Interpolates the given message.
57 * Parameters are replaced in the message in the same manner that
58 * {@link strtr()} uses.
62 * $translator = new DefaultTranslator();
64 * echo $translator->trans(
65 * 'This is a {{ var }}.',
66 * array('{{ var }}' => 'donkey')
69 * // -> This is a donkey.
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
76 * @return string The interpolated string
78 public function trans($id, array $parameters = array(), $domain = null, $locale = null)
80 return strtr($id, $parameters);
84 * Interpolates the given choice message by choosing a variant according to a number.
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.
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
100 * echo $translator->transChoice(
101 * 'This is {{ count }} donkey.|These are {{ count }} donkeys.',
103 * array('{{ count }}' => 0)
106 * // -> These are 0 donkeys.
108 * echo $translator->transChoice(
109 * 'This is {{ count }} donkey.|These are {{ count }} donkeys.',
111 * array('{{ count }}' => 1)
114 * // -> This is 1 donkey.
116 * echo $translator->transChoice(
117 * 'This is {{ count }} donkey.|These are {{ count }} donkeys.',
119 * array('{{ count }}' => 3)
122 * // -> These are 3 donkeys.
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
130 * @return string The translated string
132 * @throws InvalidArgumentException If the message id does not have the format
135 public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
137 $ids = explode('|', $id);
140 return strtr($ids[0], $parameters);
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));
147 return strtr($ids[1], $parameters);
153 * @param string $locale The locale
155 * @throws BadMethodCallException
157 public function setLocale($locale)
159 throw new BadMethodCallException('Unsupported method.');
163 * Returns the locale of the translator.
165 * @return string Always returns 'en'
167 public function getLocale()