PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Validator/DefaultTranslator.php

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