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

/src/Symfony/Component/Translation/Translator.php

https://github.com/ptheg/symfony
PHP | 233 lines | 111 code | 35 blank | 87 comment | 12 complexity | 85b569c052c2723aecff40b76c12f15a 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\Translation;
  11. use Symfony\Component\Translation\Loader\LoaderInterface;
  12. /**
  13. * Translator.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @api
  18. */
  19. class Translator implements TranslatorInterface
  20. {
  21. /**
  22. * @var MessageCatalogueInterface[]
  23. */
  24. protected $catalogues = array();
  25. /**
  26. * @var string
  27. */
  28. protected $locale;
  29. /**
  30. * @var array
  31. */
  32. private $fallbackLocales = array();
  33. /**
  34. * @var LoaderInterface[]
  35. */
  36. private $loaders = array();
  37. /**
  38. * @var array
  39. */
  40. private $resources = array();
  41. /**
  42. * @var MessageSelector
  43. */
  44. private $selector;
  45. /**
  46. * Constructor.
  47. *
  48. * @param string $locale The locale
  49. * @param MessageSelector $selector The message selector for pluralization
  50. *
  51. * @api
  52. */
  53. public function __construct($locale, MessageSelector $selector = null)
  54. {
  55. $this->locale = $locale;
  56. $this->selector = null === $selector ? new MessageSelector() : $selector;
  57. }
  58. /**
  59. * Adds a Loader.
  60. *
  61. * @param string $format The name of the loader (@see addResource())
  62. * @param LoaderInterface $loader A LoaderInterface instance
  63. *
  64. * @api
  65. */
  66. public function addLoader($format, LoaderInterface $loader)
  67. {
  68. $this->loaders[$format] = $loader;
  69. }
  70. /**
  71. * Adds a Resource.
  72. *
  73. * @param string $format The name of the loader (@see addLoader())
  74. * @param mixed $resource The resource name
  75. * @param string $locale The locale
  76. * @param string $domain The domain
  77. *
  78. * @api
  79. */
  80. public function addResource($format, $resource, $locale, $domain = 'messages')
  81. {
  82. $this->resources[$locale][] = array($format, $resource, $domain);
  83. unset($this->catalogues[$locale]);
  84. }
  85. /**
  86. * {@inheritdoc}
  87. *
  88. * @api
  89. */
  90. public function setLocale($locale)
  91. {
  92. $this->locale = $locale;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. *
  97. * @api
  98. */
  99. public function getLocale()
  100. {
  101. return $this->locale;
  102. }
  103. /**
  104. * Sets the fallback locale(s).
  105. *
  106. * @param string|array $locales The fallback locale(s)
  107. *
  108. * @api
  109. */
  110. public function setFallbackLocale($locales)
  111. {
  112. // needed as the fallback locales are linked to the already loaded catalogues
  113. $this->catalogues = array();
  114. $this->fallbackLocales = is_array($locales) ? $locales : array($locales);
  115. }
  116. /**
  117. * {@inheritdoc}
  118. *
  119. * @api
  120. */
  121. public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
  122. {
  123. if (!isset($locale)) {
  124. $locale = $this->getLocale();
  125. }
  126. if (!isset($this->catalogues[$locale])) {
  127. $this->loadCatalogue($locale);
  128. }
  129. return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters);
  130. }
  131. /**
  132. * {@inheritdoc}
  133. *
  134. * @api
  135. */
  136. public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
  137. {
  138. if (!isset($locale)) {
  139. $locale = $this->getLocale();
  140. }
  141. if (!isset($this->catalogues[$locale])) {
  142. $this->loadCatalogue($locale);
  143. }
  144. $id = (string) $id;
  145. $catalogue = $this->catalogues[$locale];
  146. while (!$catalogue->defines($id, $domain)) {
  147. if ($cat = $catalogue->getFallbackCatalogue()) {
  148. $catalogue = $cat;
  149. $locale = $catalogue->getLocale();
  150. } else {
  151. break;
  152. }
  153. }
  154. return strtr($this->selector->choose($catalogue->get($id, $domain), (float) $number, $locale), $parameters);
  155. }
  156. protected function loadCatalogue($locale)
  157. {
  158. $this->doLoadCatalogue($locale);
  159. $this->loadFallbackCatalogues($locale);
  160. }
  161. private function doLoadCatalogue($locale)
  162. {
  163. $this->catalogues[$locale] = new MessageCatalogue($locale);
  164. if (isset($this->resources[$locale])) {
  165. foreach ($this->resources[$locale] as $resource) {
  166. if (!isset($this->loaders[$resource[0]])) {
  167. throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
  168. }
  169. $this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
  170. }
  171. }
  172. }
  173. private function loadFallbackCatalogues($locale)
  174. {
  175. $current = $this->catalogues[$locale];
  176. foreach ($this->computeFallbackLocales($locale) as $fallback) {
  177. if (!isset($this->catalogues[$fallback])) {
  178. $this->doLoadCatalogue($fallback);
  179. }
  180. $current->addFallbackCatalogue($this->catalogues[$fallback]);
  181. $current = $this->catalogues[$fallback];
  182. }
  183. }
  184. protected function computeFallbackLocales($locale)
  185. {
  186. $locales = array();
  187. foreach ($this->fallbackLocales as $fallback) {
  188. if ($fallback === $locale) {
  189. continue;
  190. }
  191. $locales[] = $fallback;
  192. }
  193. if (strrchr($locale, '_') !== false) {
  194. array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_'))));
  195. }
  196. return array_unique($locales);
  197. }
  198. }