PageRenderTime 62ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/Laolballs/evotting
PHP | 472 lines | 251 code | 71 blank | 150 comment | 32 complexity | 8fddcb8b7c650343e59697a1c3daa34f 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. use Symfony\Component\Config\ConfigCache;
  14. /**
  15. * Translator.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. *
  19. * @api
  20. */
  21. class Translator implements TranslatorInterface, TranslatorBagInterface
  22. {
  23. /**
  24. * @var MessageCatalogueInterface[]
  25. */
  26. protected $catalogues = array();
  27. /**
  28. * @var string
  29. */
  30. protected $locale;
  31. /**
  32. * @var array
  33. */
  34. private $fallbackLocales = array();
  35. /**
  36. * @var LoaderInterface[]
  37. */
  38. private $loaders = array();
  39. /**
  40. * @var array
  41. */
  42. private $resources = array();
  43. /**
  44. * @var MessageSelector
  45. */
  46. private $selector;
  47. /**
  48. * @var string
  49. */
  50. private $cacheDir;
  51. /**
  52. * @var bool
  53. */
  54. private $debug;
  55. /**
  56. * Constructor.
  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. * @api
  66. */
  67. public function __construct($locale, MessageSelector $selector = null, $cacheDir = null, $debug = false)
  68. {
  69. $this->setLocale($locale);
  70. $this->selector = $selector ?: new MessageSelector();
  71. $this->cacheDir = $cacheDir;
  72. $this->debug = $debug;
  73. }
  74. /**
  75. * Adds a Loader.
  76. *
  77. * @param string $format The name of the loader (@see addResource())
  78. * @param LoaderInterface $loader A LoaderInterface instance
  79. *
  80. * @api
  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. * @api
  97. */
  98. public function addResource($format, $resource, $locale, $domain = null)
  99. {
  100. if (null === $domain) {
  101. $domain = 'messages';
  102. }
  103. $this->assertValidLocale($locale);
  104. $this->resources[$locale][] = array($format, $resource, $domain);
  105. if (in_array($locale, $this->fallbackLocales)) {
  106. $this->catalogues = array();
  107. } else {
  108. unset($this->catalogues[$locale]);
  109. }
  110. }
  111. /**
  112. * {@inheritdoc}
  113. *
  114. * @api
  115. */
  116. public function setLocale($locale)
  117. {
  118. $this->assertValidLocale($locale);
  119. $this->locale = $locale;
  120. }
  121. /**
  122. * {@inheritdoc}
  123. *
  124. * @api
  125. */
  126. public function getLocale()
  127. {
  128. return $this->locale;
  129. }
  130. /**
  131. * Sets the fallback locale(s).
  132. *
  133. * @param string|array $locales The fallback locale(s)
  134. *
  135. * @throws \InvalidArgumentException If a locale contains invalid characters
  136. *
  137. * @deprecated since 2.3, to be removed in 3.0. Use setFallbackLocales() instead.
  138. *
  139. * @api
  140. */
  141. public function setFallbackLocale($locales)
  142. {
  143. $this->setFallbackLocales(is_array($locales) ? $locales : array($locales));
  144. }
  145. /**
  146. * Sets the fallback locales.
  147. *
  148. * @param array $locales The fallback locales
  149. *
  150. * @throws \InvalidArgumentException If a locale contains invalid characters
  151. *
  152. * @api
  153. */
  154. public function setFallbackLocales(array $locales)
  155. {
  156. // needed as the fallback locales are linked to the already loaded catalogues
  157. $this->catalogues = array();
  158. foreach ($locales as $locale) {
  159. $this->assertValidLocale($locale);
  160. }
  161. $this->fallbackLocales = $locales;
  162. }
  163. /**
  164. * Gets the fallback locales.
  165. *
  166. * @return array $locales The fallback locales
  167. *
  168. * @api
  169. */
  170. public function getFallbackLocales()
  171. {
  172. return $this->fallbackLocales;
  173. }
  174. /**
  175. * {@inheritdoc}
  176. *
  177. * @api
  178. */
  179. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  180. {
  181. if (null === $locale) {
  182. $locale = $this->getLocale();
  183. } else {
  184. $this->assertValidLocale($locale);
  185. }
  186. if (null === $domain) {
  187. $domain = 'messages';
  188. }
  189. if (!isset($this->catalogues[$locale])) {
  190. $this->loadCatalogue($locale);
  191. }
  192. return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters);
  193. }
  194. /**
  195. * {@inheritdoc}
  196. *
  197. * @api
  198. */
  199. public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
  200. {
  201. if (null === $locale) {
  202. $locale = $this->getLocale();
  203. } else {
  204. $this->assertValidLocale($locale);
  205. }
  206. if (null === $domain) {
  207. $domain = 'messages';
  208. }
  209. if (!isset($this->catalogues[$locale])) {
  210. $this->loadCatalogue($locale);
  211. }
  212. $id = (string) $id;
  213. $catalogue = $this->catalogues[$locale];
  214. while (!$catalogue->defines($id, $domain)) {
  215. if ($cat = $catalogue->getFallbackCatalogue()) {
  216. $catalogue = $cat;
  217. $locale = $catalogue->getLocale();
  218. } else {
  219. break;
  220. }
  221. }
  222. return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters);
  223. }
  224. /**
  225. * {@inheritdoc}
  226. */
  227. public function getCatalogue($locale = null)
  228. {
  229. if (null === $locale) {
  230. $locale = $this->getLocale();
  231. }
  232. if (!isset($this->catalogues[$locale])) {
  233. $this->loadCatalogue($locale);
  234. }
  235. return $this->catalogues[$locale];
  236. }
  237. /**
  238. * Gets the loaders.
  239. *
  240. * @return array LoaderInterface[]
  241. */
  242. protected function getLoaders()
  243. {
  244. return $this->loaders;
  245. }
  246. /**
  247. * Collects all messages for the given locale.
  248. *
  249. * @param string|null $locale Locale of translations, by default is current locale
  250. *
  251. * @return array[array] indexed by catalog
  252. */
  253. public function getMessages($locale = null)
  254. {
  255. if (null === $locale) {
  256. $locale = $this->getLocale();
  257. }
  258. if (!isset($this->catalogues[$locale])) {
  259. $this->loadCatalogue($locale);
  260. }
  261. $catalogues = array();
  262. $catalogues[] = $catalogue = $this->catalogues[$locale];
  263. while ($catalogue = $catalogue->getFallbackCatalogue()) {
  264. $catalogues[] = $catalogue;
  265. }
  266. $messages = array();
  267. for ($i = count($catalogues) - 1; $i >= 0; $i--) {
  268. $localeMessages = $catalogues[$i]->all();
  269. $messages = array_replace_recursive($messages, $localeMessages);
  270. }
  271. return $messages;
  272. }
  273. /*
  274. * @param string $locale
  275. */
  276. protected function loadCatalogue($locale)
  277. {
  278. if (null === $this->cacheDir) {
  279. $this->initializeCatalogue($locale);
  280. } else {
  281. $this->initializeCacheCatalogue($locale);
  282. }
  283. }
  284. /**
  285. * @param string $locale
  286. */
  287. protected function initializeCatalogue($locale)
  288. {
  289. $this->assertValidLocale($locale);
  290. try {
  291. $this->doLoadCatalogue($locale);
  292. } catch (NotFoundResourceException $e) {
  293. if (!$this->computeFallbackLocales($locale)) {
  294. throw $e;
  295. }
  296. }
  297. $this->loadFallbackCatalogues($locale);
  298. }
  299. /**
  300. * @param string $locale
  301. */
  302. private function initializeCacheCatalogue($locale)
  303. {
  304. if (isset($this->catalogues[$locale])) {
  305. return;
  306. }
  307. if (null === $this->cacheDir) {
  308. $this->initialize();
  309. return $this->loadCatalogue($locale);
  310. }
  311. $this->assertValidLocale($locale);
  312. $cache = new ConfigCache($this->cacheDir.'/catalogue.'.$locale.'.php', $this->debug);
  313. if (!$cache->isFresh()) {
  314. $this->initializeCatalogue($locale);
  315. $fallbackContent = '';
  316. $current = '';
  317. $replacementPattern = '/[^a-z0-9_]/i';
  318. foreach ($this->computeFallbackLocales($locale) as $fallback) {
  319. $fallbackSuffix = ucfirst(preg_replace($replacementPattern, '_', $fallback));
  320. $currentSuffix = ucfirst(preg_replace($replacementPattern, '_', $current));
  321. $fallbackContent .= sprintf(<<<EOF
  322. \$catalogue%s = new MessageCatalogue('%s', %s);
  323. \$catalogue%s->addFallbackCatalogue(\$catalogue%s);
  324. EOF
  325. ,
  326. $fallbackSuffix,
  327. $fallback,
  328. var_export($this->catalogues[$fallback]->all(), true),
  329. $currentSuffix,
  330. $fallbackSuffix
  331. );
  332. $current = $fallback;
  333. }
  334. $content = sprintf(<<<EOF
  335. <?php
  336. use Symfony\Component\Translation\MessageCatalogue;
  337. \$catalogue = new MessageCatalogue('%s', %s);
  338. %s
  339. return \$catalogue;
  340. EOF
  341. ,
  342. $locale,
  343. var_export($this->catalogues[$locale]->all(), true),
  344. $fallbackContent
  345. );
  346. $cache->write($content, $this->catalogues[$locale]->getResources());
  347. return;
  348. }
  349. $this->catalogues[$locale] = include $cache;
  350. }
  351. private function doLoadCatalogue($locale)
  352. {
  353. $this->catalogues[$locale] = new MessageCatalogue($locale);
  354. if (isset($this->resources[$locale])) {
  355. foreach ($this->resources[$locale] as $resource) {
  356. if (!isset($this->loaders[$resource[0]])) {
  357. throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
  358. }
  359. $this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
  360. }
  361. }
  362. }
  363. private function loadFallbackCatalogues($locale)
  364. {
  365. $current = $this->catalogues[$locale];
  366. foreach ($this->computeFallbackLocales($locale) as $fallback) {
  367. if (!isset($this->catalogues[$fallback])) {
  368. $this->doLoadCatalogue($fallback);
  369. }
  370. $current->addFallbackCatalogue($this->catalogues[$fallback]);
  371. $current = $this->catalogues[$fallback];
  372. }
  373. }
  374. protected function computeFallbackLocales($locale)
  375. {
  376. $locales = array();
  377. foreach ($this->fallbackLocales as $fallback) {
  378. if ($fallback === $locale) {
  379. continue;
  380. }
  381. $locales[] = $fallback;
  382. }
  383. if (strrchr($locale, '_') !== false) {
  384. array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_'))));
  385. }
  386. return array_unique($locales);
  387. }
  388. /**
  389. * Asserts that the locale is valid, throws an Exception if not.
  390. *
  391. * @param string $locale Locale to tests
  392. *
  393. * @throws \InvalidArgumentException If the locale contains invalid characters
  394. */
  395. protected function assertValidLocale($locale)
  396. {
  397. if (1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
  398. throw new \InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
  399. }
  400. }
  401. }