PageRenderTime 29ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

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