Drupal investigation

DefaultTranslator.php 5.5KB

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