1919 * Simple translator implementation that simply replaces the parameters in
2020 * the message IDs.
2121 *
22- * Does not support translation domains or locales.
22+ * Example usage:
23+ *
24+ * $translator = new DefaultTranslator();
25+ *
26+ * echo $translator->trans(
27+ * 'This is a {{ var }}.',
28+ * array('{{ var }}' => 'donkey')
29+ * );
30+ *
31+ * // -> This is a donkey.
32+ *
33+ * echo $translator->transChoice(
34+ * 'This is {{ count }} donkey.|These are {{ count }} donkeys.',
35+ * 3,
36+ * array('{{ count }}' => 'three')
37+ * );
38+ *
39+ * // -> These are three donkeys.
40+ *
41+ * This translator does not support message catalogs, translation domains or
42+ * locales. Instead, it implements a subset of the capabilities of
43+ * {@link \Symfony\Component\Translation\Translator} and can be used in places
44+ * where translation is not required by default but should be optional.
2345 *
2446 * @author Bernhard Schussek <[email protected] > 2547 */
2648class DefaultTranslator implements TranslatorInterface
2749{
2850 /**
29- * {@inheritdoc}
51+ * Interpolates the given message.
52+ *
53+ * Parameters are replaced in the message in the same manner that
54+ * {@link strtr()} uses.
55+ *
56+ * Example usage:
57+ *
58+ * $translator = new DefaultTranslator();
59+ *
60+ * echo $translator->trans(
61+ * 'This is a {{ var }}.',
62+ * array('{{ var }}' => 'donkey')
63+ * );
64+ *
65+ * // -> This is a donkey.
66+ *
67+ * @param string $id The message id
68+ * @param array $parameters An array of parameters for the message
69+ * @param string $domain Ignored
70+ * @param string $locale Ignored
71+ *
72+ * @return string The interpolated string
3073 */
3174 public function trans ($ id , array $ parameters = array (), $ domain = null , $ locale = null )
3275 {
3376 return strtr ($ id , $ parameters );
3477 }
3578
3679 /**
37- * {@inheritdoc}
80+ * Interpolates the given choice message by choosing a variant according to a number.
81+ *
82+ * The variants are passed in the message ID using the format
83+ * "<singular>|<plural>". "<singular>" is chosen if the passed $number is
84+ * exactly 1. "<plural>" is chosen otherwise.
85+ *
86+ * This format is consistent with the format supported by
87+ * {@link \Symfony\Component\Translation\Translator}, but it does not
88+ * have the same expressiveness. While Translator supports intervals in
89+ * message translations, which are needed for languages other than English,
90+ * this translator does not. You should use Translator or a custom
91+ * implementation of {@link TranslatorInterface} if you need this or similar
92+ * functionality.
93+ *
94+ * Example usage:
95+ *
96+ * echo $translator->transChoice(
97+ * 'This is {{ count }} donkey.|These are {{ count }} donkeys.',
98+ * 0,
99+ * array('{{ count }}' => 0)
100+ * );
101+ *
102+ * // -> These are 0 donkeys.
103+ *
104+ * echo $translator->transChoice(
105+ * 'This is {{ count }} donkey.|These are {{ count }} donkeys.',
106+ * 1,
107+ * array('{{ count }}' => 1)
108+ * );
109+ *
110+ * // -> This is 1 donkey.
111+ *
112+ * echo $translator->transChoice(
113+ * 'This is {{ count }} donkey.|These are {{ count }} donkeys.',
114+ * 3,
115+ * array('{{ count }}' => 3)
116+ * );
117+ *
118+ * // -> These are 3 donkeys.
119+ *
120+ * @param string $id The message id
121+ * @param integer $number The number to use to find the index of the message
122+ * @param array $parameters An array of parameters for the message
123+ * @param string $domain Ignored
124+ * @param string $locale Ignored
125+ *
126+ * @return string The translated string
127+ *
128+ * @throws InvalidArgumentException If the message id does not have the format
129+ * "singular|plural".
38130 */
39131 public function transChoice ($ id , $ number , array $ parameters = array (), $ domain = null , $ locale = null )
40132 {
@@ -52,15 +144,21 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
52144 }
53145
54146 /**
55- * {@inheritdoc}
147+ * Not supported.
148+ *
149+ * @param string $locale The locale
150+ *
151+ * @throws BadMethodCallException
56152 */
57153 public function setLocale ($ locale )
58154 {
59155 throw new BadMethodCallException ('Unsupported method. ' );
60156 }
61157
62158 /**
63- * {@inheritdoc}
159+ * Not supported.
160+ *
161+ * @throws BadMethodCallException
64162 */
65163 public function getLocale ()
66164 {
0 commit comments