PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/techniconline/kmc
PHP | 440 lines | 227 code | 63 blank | 150 comment | 23 complexity | 2ef1dd246a08799be8501fc093e20ebc 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 === $domain) {
  182. $domain = 'messages';
  183. }
  184. return strtr($this->getCatalogue($locale)->get((string)$id, $domain), $parameters);
  185. }
  186. /**
  187. * {@inheritdoc}
  188. *
  189. * @api
  190. */
  191. public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
  192. {
  193. if (null === $domain) {
  194. $domain = 'messages';
  195. }
  196. $id = (string)$id;
  197. $catalogue = $this->getCatalogue($locale);
  198. $locale = $catalogue->getLocale();
  199. while (!$catalogue->defines($id, $domain)) {
  200. if ($cat = $catalogue->getFallbackCatalogue()) {
  201. $catalogue = $cat;
  202. $locale = $catalogue->getLocale();
  203. } else {
  204. break;
  205. }
  206. }
  207. return strtr($this->selector->choose($catalogue->get($id, $domain), (int)$number, $locale), $parameters);
  208. }
  209. /**
  210. * {@inheritdoc}
  211. */
  212. public function getCatalogue($locale = null)
  213. {
  214. if (null === $locale) {
  215. $locale = $this->getLocale();
  216. } else {
  217. $this->assertValidLocale($locale);
  218. }
  219. if (!isset($this->catalogues[$locale])) {
  220. $this->loadCatalogue($locale);
  221. }
  222. return $this->catalogues[$locale];
  223. }
  224. /**
  225. * Gets the loaders.
  226. *
  227. * @return array LoaderInterface[]
  228. */
  229. protected function getLoaders()
  230. {
  231. return $this->loaders;
  232. }
  233. /**
  234. * Collects all messages for the given locale.
  235. *
  236. * @param string|null $locale Locale of translations, by default is current locale
  237. *
  238. * @return array[array] indexed by catalog
  239. */
  240. public function getMessages($locale = null)
  241. {
  242. $catalogue = $this->getCatalogue($locale);
  243. $messages = $catalogue->all();
  244. while ($catalogue = $catalogue->getFallbackCatalogue()) {
  245. $messages = array_replace_recursive($catalogue->all(), $messages);
  246. }
  247. return $messages;
  248. }
  249. /*
  250. * @param string $locale
  251. */
  252. protected function loadCatalogue($locale)
  253. {
  254. if (null === $this->cacheDir) {
  255. $this->initializeCatalogue($locale);
  256. } else {
  257. $this->initializeCacheCatalogue($locale);
  258. }
  259. }
  260. /**
  261. * @param string $locale
  262. */
  263. protected function initializeCatalogue($locale)
  264. {
  265. $this->assertValidLocale($locale);
  266. try {
  267. $this->doLoadCatalogue($locale);
  268. } catch (NotFoundResourceException $e) {
  269. if (!$this->computeFallbackLocales($locale)) {
  270. throw $e;
  271. }
  272. }
  273. $this->loadFallbackCatalogues($locale);
  274. }
  275. /**
  276. * @param string $locale
  277. */
  278. private function initializeCacheCatalogue($locale)
  279. {
  280. if (isset($this->catalogues[$locale])) {
  281. return;
  282. }
  283. $this->assertValidLocale($locale);
  284. $cache = new ConfigCache($this->getCatalogueCachePath($locale), $this->debug);
  285. if (!$cache->isFresh()) {
  286. $this->initializeCatalogue($locale);
  287. $fallbackContent = '';
  288. $current = '';
  289. $replacementPattern = '/[^a-z0-9_]/i';
  290. foreach ($this->computeFallbackLocales($locale) as $fallback) {
  291. $fallbackSuffix = ucfirst(preg_replace($replacementPattern, '_', $fallback));
  292. $currentSuffix = ucfirst(preg_replace($replacementPattern, '_', $current));
  293. $fallbackContent .= sprintf(<<<EOF
  294. \$catalogue%s = new MessageCatalogue('%s', %s);
  295. \$catalogue%s->addFallbackCatalogue(\$catalogue%s);
  296. EOF
  297. ,
  298. $fallbackSuffix,
  299. $fallback,
  300. var_export($this->catalogues[$fallback]->all(), true),
  301. $currentSuffix,
  302. $fallbackSuffix
  303. );
  304. $current = $fallback;
  305. }
  306. $content = sprintf(<<<EOF
  307. <?php
  308. use Symfony\Component\Translation\MessageCatalogue;
  309. \$catalogue = new MessageCatalogue('%s', %s);
  310. %s
  311. return \$catalogue;
  312. EOF
  313. ,
  314. $locale,
  315. var_export($this->catalogues[$locale]->all(), true),
  316. $fallbackContent
  317. );
  318. $cache->write($content, $this->catalogues[$locale]->getResources());
  319. return;
  320. }
  321. $this->catalogues[$locale] = include $cache;
  322. }
  323. private function getCatalogueCachePath($locale)
  324. {
  325. return $this->cacheDir . '/catalogue.' . $locale . '.' . sha1(serialize($this->fallbackLocales)) . '.php';
  326. }
  327. private function doLoadCatalogue($locale)
  328. {
  329. $this->catalogues[$locale] = new MessageCatalogue($locale);
  330. if (isset($this->resources[$locale])) {
  331. foreach ($this->resources[$locale] as $resource) {
  332. if (!isset($this->loaders[$resource[0]])) {
  333. throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
  334. }
  335. $this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
  336. }
  337. }
  338. }
  339. private function loadFallbackCatalogues($locale)
  340. {
  341. $current = $this->catalogues[$locale];
  342. foreach ($this->computeFallbackLocales($locale) as $fallback) {
  343. if (!isset($this->catalogues[$fallback])) {
  344. $this->doLoadCatalogue($fallback);
  345. }
  346. $current->addFallbackCatalogue($this->catalogues[$fallback]);
  347. $current = $this->catalogues[$fallback];
  348. }
  349. }
  350. protected function computeFallbackLocales($locale)
  351. {
  352. $locales = array();
  353. foreach ($this->fallbackLocales as $fallback) {
  354. if ($fallback === $locale) {
  355. continue;
  356. }
  357. $locales[] = $fallback;
  358. }
  359. if (strrchr($locale, '_') !== false) {
  360. array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_'))));
  361. }
  362. return array_unique($locales);
  363. }
  364. /**
  365. * Asserts that the locale is valid, throws an Exception if not.
  366. *
  367. * @param string $locale Locale to tests
  368. *
  369. * @throws \InvalidArgumentException If the locale contains invalid characters
  370. */
  371. protected function assertValidLocale($locale)
  372. {
  373. if (1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
  374. throw new \InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
  375. }
  376. }
  377. }