IdentityTranslator.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. class IdentityTranslator implements TranslatorInterface
  17. {
  18. private $selector;
  19. private $locale;
  20. /**
  21. * @param MessageSelector|null $selector The message selector for pluralization
  22. */
  23. public function __construct(MessageSelector $selector = null)
  24. {
  25. $this->selector = $selector ?: new MessageSelector();
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function setLocale($locale)
  31. {
  32. $this->locale = $locale;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function getLocale()
  38. {
  39. return $this->locale ?: \Locale::getDefault();
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  45. {
  46. return strtr((string) $id, $parameters);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
  52. {
  53. return strtr($this->selector->choose((string) $id, (int) $number, $locale ?: $this->getLocale()), $parameters);
  54. }
  55. }