No Description

IdentityTranslator.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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|null $selector The message selector for pluralization
  26. *
  27. * @api
  28. */
  29. public function __construct(MessageSelector $selector = null)
  30. {
  31. $this->selector = $selector ?: new MessageSelector();
  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 = null, $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 = null, $locale = null)
  66. {
  67. return strtr($this->selector->choose((string) $id, (int) $number, $locale ?: $this->getLocale()), $parameters);
  68. }
  69. }