IdentityTranslator.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Translation;
  11. /**
  12. * IdentityTranslator does not translate anything.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. *
  16. * @api
  17. */
  18. class IdentityTranslator implements TranslatorInterface
  19. {
  20. private $selector;
  21. private $locale;
  22. /**
  23. * Constructor.
  24. *
  25. * @param MessageSelector $selector The message selector for pluralization
  26. *
  27. * @api
  28. */
  29. public function __construct(MessageSelector $selector)
  30. {
  31. $this->selector = $selector;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. *
  36. * @api
  37. */
  38. public function setLocale($locale)
  39. {
  40. $this->locale = $locale;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. *
  45. * @api
  46. */
  47. public function getLocale()
  48. {
  49. return $this->locale ?: \Locale::getDefault();
  50. }
  51. /**
  52. * {@inheritdoc}
  53. *
  54. * @api
  55. */
  56. public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
  57. {
  58. return strtr((string) $id, $parameters);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. *
  63. * @api
  64. */
  65. public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
  66. {
  67. return strtr($this->selector->choose((string) $id, (int) $number, $locale ?: $this->getLocale()), $parameters);
  68. }
  69. }