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

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

https://bitbucket.org/tippycracker/autokraitis
PHP | 476 lines | 263 code | 70 blank | 143 comment | 25 complexity | 531678d12776b49e145e54fb2af029fd MD5 | raw file
Possible License(s): BSD-2-Clause, GPL-2.0, GPL-3.0, BSD-3-Clause, Apache-2.0
  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. use Symfony\Component\Config\ConfigCacheInterface;
  14. use Symfony\Component\Config\ConfigCacheFactoryInterface;
  15. use Symfony\Component\Config\ConfigCacheFactory;
  16. /**
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class Translator implements TranslatorInterface, TranslatorBagInterface
  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. * @var string
  47. */
  48. private $cacheDir;
  49. /**
  50. * @var bool
  51. */
  52. private $debug;
  53. /**
  54. * @var ConfigCacheFactoryInterface|null
  55. */
  56. private $configCacheFactory;
  57. /**
  58. * @param string $locale The locale
  59. * @param MessageSelector|null $selector The message selector for pluralization
  60. * @param string|null $cacheDir The directory to use for the cache
  61. * @param bool $debug Use cache in debug mode ?
  62. *
  63. * @throws \InvalidArgumentException If a locale contains invalid characters
  64. */
  65. public function __construct($locale, MessageSelector $selector = null, $cacheDir = null, $debug = false)
  66. {
  67. $this->setLocale($locale);
  68. $this->selector = $selector ?: new MessageSelector();
  69. $this->cacheDir = $cacheDir;
  70. $this->debug = $debug;
  71. }
  72. public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
  73. {
  74. $this->configCacheFactory = $configCacheFactory;
  75. }
  76. /**
  77. * Adds a Loader.
  78. *
  79. * @param string $format The name of the loader (@see addResource())
  80. * @param LoaderInterface $loader A LoaderInterface instance
  81. */
  82. public function addLoader($format, LoaderInterface $loader)
  83. {
  84. $this->loaders[$format] = $loader;
  85. }
  86. /**
  87. * Adds a Resource.
  88. *
  89. * @param string $format The name of the loader (@see addLoader())
  90. * @param mixed $resource The resource name
  91. * @param string $locale The locale
  92. * @param string $domain The domain
  93. *
  94. * @throws \InvalidArgumentException If the locale contains invalid characters
  95. */
  96. public function addResource($format, $resource, $locale, $domain = null)
  97. {
  98. if (null === $domain) {
  99. $domain = 'messages';
  100. }
  101. $this->assertValidLocale($locale);
  102. $this->resources[$locale][] = array($format, $resource, $domain);
  103. if (in_array($locale, $this->fallbackLocales)) {
  104. $this->catalogues = array();
  105. } else {
  106. unset($this->catalogues[$locale]);
  107. }
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function setLocale($locale)
  113. {
  114. $this->assertValidLocale($locale);
  115. $this->locale = $locale;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function getLocale()
  121. {
  122. return $this->locale;
  123. }
  124. /**
  125. * Sets the fallback locale(s).
  126. *
  127. * @param string|array $locales The fallback locale(s)
  128. *
  129. * @throws \InvalidArgumentException If a locale contains invalid characters
  130. *
  131. * @deprecated since version 2.3, to be removed in 3.0. Use setFallbackLocales() instead
  132. */
  133. public function setFallbackLocale($locales)
  134. {
  135. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.3 and will be removed in 3.0. Use the setFallbackLocales() method instead.', E_USER_DEPRECATED);
  136. $this->setFallbackLocales(is_array($locales) ? $locales : array($locales));
  137. }
  138. /**
  139. * Sets the fallback locales.
  140. *
  141. * @param array $locales The fallback locales
  142. *
  143. * @throws \InvalidArgumentException If a locale contains invalid characters
  144. */
  145. public function setFallbackLocales(array $locales)
  146. {
  147. // needed as the fallback locales are linked to the already loaded catalogues
  148. $this->catalogues = array();
  149. foreach ($locales as $locale) {
  150. $this->assertValidLocale($locale);
  151. }
  152. $this->fallbackLocales = $locales;
  153. }
  154. /**
  155. * Gets the fallback locales.
  156. *
  157. * @return array $locales The fallback locales
  158. */
  159. public function getFallbackLocales()
  160. {
  161. return $this->fallbackLocales;
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  167. {
  168. if (null === $domain) {
  169. $domain = 'messages';
  170. }
  171. return strtr($this->getCatalogue($locale)->get((string) $id, $domain), $parameters);
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
  177. {
  178. if (null === $domain) {
  179. $domain = 'messages';
  180. }
  181. $id = (string) $id;
  182. $catalogue = $this->getCatalogue($locale);
  183. $locale = $catalogue->getLocale();
  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. * {@inheritdoc}
  196. */
  197. public function getCatalogue($locale = null)
  198. {
  199. if (null === $locale) {
  200. $locale = $this->getLocale();
  201. } else {
  202. $this->assertValidLocale($locale);
  203. }
  204. if (!isset($this->catalogues[$locale])) {
  205. $this->loadCatalogue($locale);
  206. }
  207. return $this->catalogues[$locale];
  208. }
  209. /**
  210. * Gets the loaders.
  211. *
  212. * @return array LoaderInterface[]
  213. */
  214. protected function getLoaders()
  215. {
  216. return $this->loaders;
  217. }
  218. /**
  219. * Collects all messages for the given locale.
  220. *
  221. * @param string|null $locale Locale of translations, by default is current locale
  222. *
  223. * @return array[array] indexed by catalog
  224. *
  225. * @deprecated since version 2.8, to be removed in 3.0. Use TranslatorBagInterface::getCatalogue() method instead.
  226. */
  227. public function getMessages($locale = null)
  228. {
  229. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Use TranslatorBagInterface::getCatalogue() method instead.', E_USER_DEPRECATED);
  230. $catalogue = $this->getCatalogue($locale);
  231. $messages = $catalogue->all();
  232. while ($catalogue = $catalogue->getFallbackCatalogue()) {
  233. $messages = array_replace_recursive($catalogue->all(), $messages);
  234. }
  235. return $messages;
  236. }
  237. /**
  238. * @param string $locale
  239. */
  240. protected function loadCatalogue($locale)
  241. {
  242. if (null === $this->cacheDir) {
  243. $this->initializeCatalogue($locale);
  244. } else {
  245. $this->initializeCacheCatalogue($locale);
  246. }
  247. }
  248. /**
  249. * @param string $locale
  250. */
  251. protected function initializeCatalogue($locale)
  252. {
  253. $this->assertValidLocale($locale);
  254. try {
  255. $this->doLoadCatalogue($locale);
  256. } catch (NotFoundResourceException $e) {
  257. if (!$this->computeFallbackLocales($locale)) {
  258. throw $e;
  259. }
  260. }
  261. $this->loadFallbackCatalogues($locale);
  262. }
  263. /**
  264. * @param string $locale
  265. */
  266. private function initializeCacheCatalogue($locale)
  267. {
  268. if (isset($this->catalogues[$locale])) {
  269. /* Catalogue already initialized. */
  270. return;
  271. }
  272. $this->assertValidLocale($locale);
  273. $self = $this; // required for PHP 5.3 where "$this" cannot be use()d in anonymous functions. Change in Symfony 3.0.
  274. $cache = $this->getConfigCacheFactory()->cache($this->getCatalogueCachePath($locale),
  275. function (ConfigCacheInterface $cache) use ($self, $locale) {
  276. $self->dumpCatalogue($locale, $cache);
  277. }
  278. );
  279. if (isset($this->catalogues[$locale])) {
  280. /* Catalogue has been initialized as it was written out to cache. */
  281. return;
  282. }
  283. /* Read catalogue from cache. */
  284. $this->catalogues[$locale] = include $cache->getPath();
  285. }
  286. /**
  287. * This method is public because it needs to be callable from a closure in PHP 5.3. It should be made protected (or even private, if possible) in 3.0.
  288. *
  289. * @internal
  290. */
  291. public function dumpCatalogue($locale, ConfigCacheInterface $cache)
  292. {
  293. $this->initializeCatalogue($locale);
  294. $fallbackContent = $this->getFallbackContent($this->catalogues[$locale]);
  295. $content = sprintf(<<<EOF
  296. <?php
  297. use Symfony\Component\Translation\MessageCatalogue;
  298. \$catalogue = new MessageCatalogue('%s', %s);
  299. %s
  300. return \$catalogue;
  301. EOF
  302. ,
  303. $locale,
  304. var_export($this->catalogues[$locale]->all(), true),
  305. $fallbackContent
  306. );
  307. $cache->write($content, $this->catalogues[$locale]->getResources());
  308. }
  309. private function getFallbackContent(MessageCatalogue $catalogue)
  310. {
  311. $fallbackContent = '';
  312. $current = '';
  313. $replacementPattern = '/[^a-z0-9_]/i';
  314. $fallbackCatalogue = $catalogue->getFallbackCatalogue();
  315. while ($fallbackCatalogue) {
  316. $fallback = $fallbackCatalogue->getLocale();
  317. $fallbackSuffix = ucfirst(preg_replace($replacementPattern, '_', $fallback));
  318. $currentSuffix = ucfirst(preg_replace($replacementPattern, '_', $current));
  319. $fallbackContent .= sprintf(<<<'EOF'
  320. $catalogue%s = new MessageCatalogue('%s', %s);
  321. $catalogue%s->addFallbackCatalogue($catalogue%s);
  322. EOF
  323. ,
  324. $fallbackSuffix,
  325. $fallback,
  326. var_export($fallbackCatalogue->all(), true),
  327. $currentSuffix,
  328. $fallbackSuffix
  329. );
  330. $current = $fallbackCatalogue->getLocale();
  331. $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
  332. }
  333. return $fallbackContent;
  334. }
  335. private function getCatalogueCachePath($locale)
  336. {
  337. return $this->cacheDir.'/catalogue.'.$locale.'.'.sha1(serialize($this->fallbackLocales)).'.php';
  338. }
  339. private function doLoadCatalogue($locale)
  340. {
  341. $this->catalogues[$locale] = new MessageCatalogue($locale);
  342. if (isset($this->resources[$locale])) {
  343. foreach ($this->resources[$locale] as $resource) {
  344. if (!isset($this->loaders[$resource[0]])) {
  345. throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
  346. }
  347. $this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
  348. }
  349. }
  350. }
  351. private function loadFallbackCatalogues($locale)
  352. {
  353. $current = $this->catalogues[$locale];
  354. foreach ($this->computeFallbackLocales($locale) as $fallback) {
  355. if (!isset($this->catalogues[$fallback])) {
  356. $this->initializeCatalogue($fallback);
  357. }
  358. $fallbackCatalogue = new MessageCatalogue($fallback, $this->catalogues[$fallback]->all());
  359. foreach ($this->catalogues[$fallback]->getResources() as $resource) {
  360. $fallbackCatalogue->addResource($resource);
  361. }
  362. $current->addFallbackCatalogue($fallbackCatalogue);
  363. $current = $fallbackCatalogue;
  364. }
  365. }
  366. protected function computeFallbackLocales($locale)
  367. {
  368. $locales = array();
  369. foreach ($this->fallbackLocales as $fallback) {
  370. if ($fallback === $locale) {
  371. continue;
  372. }
  373. $locales[] = $fallback;
  374. }
  375. if (false !== strrchr($locale, '_')) {
  376. array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_'))));
  377. }
  378. return array_unique($locales);
  379. }
  380. /**
  381. * Asserts that the locale is valid, throws an Exception if not.
  382. *
  383. * @param string $locale Locale to tests
  384. *
  385. * @throws \InvalidArgumentException If the locale contains invalid characters
  386. */
  387. protected function assertValidLocale($locale)
  388. {
  389. if (1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
  390. throw new \InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
  391. }
  392. }
  393. /**
  394. * Provides the ConfigCache factory implementation, falling back to a
  395. * default implementation if necessary.
  396. *
  397. * @return ConfigCacheFactoryInterface $configCacheFactory
  398. */
  399. private function getConfigCacheFactory()
  400. {
  401. if (!$this->configCacheFactory) {
  402. $this->configCacheFactory = new ConfigCacheFactory($this->debug);
  403. }
  404. return $this->configCacheFactory;
  405. }
  406. }