PageRenderTime 636ms CodeModel.GetById 39ms RepoModel.GetById 3ms app.codeStats 0ms

/vendor/symfony/translation/Symfony/Component/Translation/Translator.php

https://bitbucket.org/davide_grobberio/laravel-angular
PHP | 325 lines | 158 code | 44 blank | 123 comment | 21 complexity | 55d6c6341d932e495b049044540b473a MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  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. * @throws \InvalidArgumentException If a locale contains invalid characters
  53. *
  54. * @api
  55. */
  56. public function __construct($locale, MessageSelector $selector = null)
  57. {
  58. $this->setLocale($locale);
  59. $this->selector = $selector ?: new MessageSelector();
  60. }
  61. /**
  62. * Adds a Loader.
  63. *
  64. * @param string $format The name of the loader (@see addResource())
  65. * @param LoaderInterface $loader A LoaderInterface instance
  66. *
  67. * @api
  68. */
  69. public function addLoader($format, LoaderInterface $loader)
  70. {
  71. $this->loaders[$format] = $loader;
  72. }
  73. /**
  74. * Adds a Resource.
  75. *
  76. * @param string $format The name of the loader (@see addLoader())
  77. * @param mixed $resource The resource name
  78. * @param string $locale The locale
  79. * @param string $domain The domain
  80. *
  81. * @throws \InvalidArgumentException If the locale contains invalid characters
  82. *
  83. * @api
  84. */
  85. public function addResource($format, $resource, $locale, $domain = null)
  86. {
  87. if (null === $domain) {
  88. $domain = 'messages';
  89. }
  90. $this->assertValidLocale($locale);
  91. $this->resources[$locale][] = array($format, $resource, $domain);
  92. if (in_array($locale, $this->fallbackLocales)) {
  93. $this->catalogues = array();
  94. } else {
  95. unset($this->catalogues[$locale]);
  96. }
  97. }
  98. /**
  99. * {@inheritdoc}
  100. *
  101. * @api
  102. */
  103. public function setLocale($locale)
  104. {
  105. $this->assertValidLocale($locale);
  106. $this->locale = $locale;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. *
  111. * @api
  112. */
  113. public function getLocale()
  114. {
  115. return $this->locale;
  116. }
  117. /**
  118. * Sets the fallback locale(s).
  119. *
  120. * @param string|array $locales The fallback locale(s)
  121. *
  122. * @throws \InvalidArgumentException If a locale contains invalid characters
  123. *
  124. * @deprecated since 2.3, to be removed in 3.0. Use setFallbackLocales() instead.
  125. *
  126. * @api
  127. */
  128. public function setFallbackLocale($locales)
  129. {
  130. $this->setFallbackLocales(is_array($locales) ? $locales : array($locales));
  131. }
  132. /**
  133. * Sets the fallback locales.
  134. *
  135. * @param array $locales The fallback locales
  136. *
  137. * @throws \InvalidArgumentException If a locale contains invalid characters
  138. *
  139. * @api
  140. */
  141. public function setFallbackLocales(array $locales)
  142. {
  143. // needed as the fallback locales are linked to the already loaded catalogues
  144. $this->catalogues = array();
  145. foreach ($locales as $locale) {
  146. $this->assertValidLocale($locale);
  147. }
  148. $this->fallbackLocales = $locales;
  149. }
  150. /**
  151. * Gets the fallback locales.
  152. *
  153. * @return array $locales The fallback locales
  154. *
  155. * @api
  156. */
  157. public function getFallbackLocales()
  158. {
  159. return $this->fallbackLocales;
  160. }
  161. /**
  162. * {@inheritdoc}
  163. *
  164. * @api
  165. */
  166. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  167. {
  168. if (null === $locale) {
  169. $locale = $this->getLocale();
  170. } else {
  171. $this->assertValidLocale($locale);
  172. }
  173. if (null === $domain) {
  174. $domain = 'messages';
  175. }
  176. if (!isset($this->catalogues[$locale])) {
  177. $this->loadCatalogue($locale);
  178. }
  179. return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters);
  180. }
  181. /**
  182. * {@inheritdoc}
  183. *
  184. * @api
  185. */
  186. public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
  187. {
  188. if (null === $locale) {
  189. $locale = $this->getLocale();
  190. } else {
  191. $this->assertValidLocale($locale);
  192. }
  193. if (null === $domain) {
  194. $domain = 'messages';
  195. }
  196. if (!isset($this->catalogues[$locale])) {
  197. $this->loadCatalogue($locale);
  198. }
  199. $id = (string) $id;
  200. $catalogue = $this->catalogues[$locale];
  201. while (!$catalogue->defines($id, $domain)) {
  202. if ($cat = $catalogue->getFallbackCatalogue()) {
  203. $catalogue = $cat;
  204. $locale = $catalogue->getLocale();
  205. } else {
  206. break;
  207. }
  208. }
  209. return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters);
  210. }
  211. /**
  212. * Gets the loaders.
  213. *
  214. * @return array LoaderInterface[]
  215. */
  216. protected function getLoaders()
  217. {
  218. return $this->loaders;
  219. }
  220. protected function loadCatalogue($locale)
  221. {
  222. try {
  223. $this->doLoadCatalogue($locale);
  224. } catch (NotFoundResourceException $e) {
  225. if (!$this->computeFallbackLocales($locale)) {
  226. throw $e;
  227. }
  228. }
  229. $this->loadFallbackCatalogues($locale);
  230. }
  231. private function doLoadCatalogue($locale)
  232. {
  233. $this->catalogues[$locale] = new MessageCatalogue($locale);
  234. if (isset($this->resources[$locale])) {
  235. foreach ($this->resources[$locale] as $resource) {
  236. if (!isset($this->loaders[$resource[0]])) {
  237. throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
  238. }
  239. $this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
  240. }
  241. }
  242. }
  243. private function loadFallbackCatalogues($locale)
  244. {
  245. $current = $this->catalogues[$locale];
  246. foreach ($this->computeFallbackLocales($locale) as $fallback) {
  247. if (!isset($this->catalogues[$fallback])) {
  248. $this->doLoadCatalogue($fallback);
  249. }
  250. $current->addFallbackCatalogue($this->catalogues[$fallback]);
  251. $current = $this->catalogues[$fallback];
  252. }
  253. }
  254. protected function computeFallbackLocales($locale)
  255. {
  256. $locales = array();
  257. foreach ($this->fallbackLocales as $fallback) {
  258. if ($fallback === $locale) {
  259. continue;
  260. }
  261. $locales[] = $fallback;
  262. }
  263. if (strrchr($locale, '_') !== false) {
  264. array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_'))));
  265. }
  266. return array_unique($locales);
  267. }
  268. /**
  269. * Asserts that the locale is valid, throws an Exception if not.
  270. *
  271. * @param string $locale Locale to tests
  272. *
  273. * @throws \InvalidArgumentException If the locale contains invalid characters
  274. */
  275. private function assertValidLocale($locale)
  276. {
  277. if (0 !== preg_match('/[^a-z0-9_\\.\\-]+/i', $locale, $match)) {
  278. throw new \InvalidArgumentException(sprintf('Invalid locale: %s.', $locale));
  279. }
  280. }
  281. }