TranslatorRegistry.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\I18n;
  16. use Aura\Intl\Exception;
  17. use Aura\Intl\TranslatorLocator;
  18. use Cake\Cache\CacheEngine;
  19. /**
  20. * Constructs and stores instances of translators that can be
  21. * retrieved by name and locale.
  22. */
  23. class TranslatorRegistry extends TranslatorLocator
  24. {
  25. /**
  26. * A list of loader functions indexed by domain name. Loaders are
  27. * callables that are invoked as a default for building translation
  28. * packages where none can be found for the combination of translator
  29. * name and locale.
  30. *
  31. * @var array
  32. */
  33. protected $_loaders;
  34. /**
  35. * The name of the default formatter to use for newly created
  36. * translators from the fallback loader
  37. *
  38. * @var string
  39. */
  40. protected $_defaultFormatter = 'default';
  41. /**
  42. * Use fallback-domain for translation loaders.
  43. *
  44. * @var bool
  45. */
  46. protected $_useFallback = true;
  47. /**
  48. * A CacheEngine object that is used to remember translator across
  49. * requests.
  50. *
  51. * @var \Cake\Cache\CacheEngine
  52. */
  53. protected $_cacher;
  54. /**
  55. * Sets the CacheEngine instance used to remember translators across
  56. * requests.
  57. *
  58. * @param \Cake\Cache\CacheEngine $cacher The cacher instance.
  59. * @return void
  60. */
  61. public function setCacher(CacheEngine $cacher)
  62. {
  63. $this->_cacher = $cacher;
  64. }
  65. /**
  66. * Gets a translator from the registry by package for a locale.
  67. *
  68. * @param string $name The translator package to retrieve.
  69. * @param string|null $locale The locale to use; if empty, uses the default
  70. * locale.
  71. * @return \Aura\Intl\TranslatorInterface A translator object.
  72. * @throws \Aura\Intl\Exception If no translator with that name could be found
  73. * for the given locale.
  74. */
  75. public function get($name, $locale = null)
  76. {
  77. if (!$name) {
  78. return null;
  79. }
  80. if ($locale === null) {
  81. $locale = $this->getLocale();
  82. }
  83. if (isset($this->registry[$name][$locale])) {
  84. return $this->registry[$name][$locale];
  85. }
  86. if (!$this->_cacher) {
  87. return $this->registry[$name][$locale] = $this->_getTranslator($name, $locale);
  88. }
  89. $key = "translations.$name.$locale";
  90. $translator = $this->_cacher->read($key);
  91. if (!$translator) {
  92. $translator = $this->_getTranslator($name, $locale);
  93. $this->_cacher->write($key, $translator);
  94. }
  95. return $this->registry[$name][$locale] = $translator;
  96. }
  97. /**
  98. * Gets a translator from the registry by package for a locale.
  99. *
  100. * @param string $name The translator package to retrieve.
  101. * @param string|null $locale The locale to use; if empty, uses the default
  102. * locale.
  103. * @return \Aura\Intl\TranslatorInterface A translator object.
  104. */
  105. protected function _getTranslator($name, $locale)
  106. {
  107. try {
  108. return parent::get($name, $locale);
  109. } catch (Exception $e) {
  110. }
  111. if (!isset($this->_loaders[$name])) {
  112. $this->registerLoader($name, $this->_partialLoader());
  113. }
  114. return $this->_getFromLoader($name, $locale);
  115. }
  116. /**
  117. * Registers a loader function for a package name that will be used as a fallback
  118. * in case no package with that name can be found.
  119. *
  120. * Loader callbacks will get as first argument the package name and the locale as
  121. * the second argument.
  122. *
  123. * @param string $name The name of the translator package to register a loader for
  124. * @param callable $loader A callable object that should return a Package
  125. * @return void
  126. */
  127. public function registerLoader($name, callable $loader)
  128. {
  129. $this->_loaders[$name] = $loader;
  130. }
  131. /**
  132. * Sets the name of the default messages formatter to use for future
  133. * translator instances.
  134. *
  135. * If called with no arguments, it will return the currently configured value.
  136. *
  137. * @param string|null $name The name of the formatter to use.
  138. * @return string The name of the formatter.
  139. */
  140. public function defaultFormatter($name = null)
  141. {
  142. if ($name === null) {
  143. return $this->_defaultFormatter;
  144. }
  145. return $this->_defaultFormatter = $name;
  146. }
  147. /**
  148. * Set if the default domain fallback is used.
  149. *
  150. * @param bool $enable flag to enable or disable fallback
  151. * @return void
  152. */
  153. public function useFallback($enable = true)
  154. {
  155. $this->_useFallback = $enable;
  156. }
  157. /**
  158. * Returns a new translator instance for the given name and locale
  159. * based of conventions.
  160. *
  161. * @param string $name The translation package name.
  162. * @param string $locale The locale to create the translator for.
  163. * @return \Aura\Intl\Translator
  164. */
  165. protected function _fallbackLoader($name, $locale)
  166. {
  167. $chain = new ChainMessagesLoader([
  168. new MessagesFileLoader($name, $locale, 'mo'),
  169. new MessagesFileLoader($name, $locale, 'po')
  170. ]);
  171. // \Aura\Intl\Package by default uses formatter configured with key "basic".
  172. // and we want to make sure the cake domain always uses the default formatter
  173. $formatter = $name === 'cake' ? 'default' : $this->_defaultFormatter;
  174. $chain = function () use ($formatter, $chain) {
  175. $package = $chain();
  176. $package->setFormatter($formatter);
  177. return $package;
  178. };
  179. return $chain;
  180. }
  181. /**
  182. * Returns a function that can be used as a loader for the registerLoaderMethod
  183. *
  184. * @return callable
  185. */
  186. protected function _partialLoader()
  187. {
  188. return function ($name, $locale) {
  189. return $this->_fallbackLoader($name, $locale);
  190. };
  191. }
  192. /**
  193. * Registers a new package by passing the register loaded function for the
  194. * package name.
  195. *
  196. * @param string $name The name of the translator package
  197. * @param string $locale The locale that should be built the package for
  198. * @return \Aura\Intl\TranslatorInterface A translator object.
  199. */
  200. protected function _getFromLoader($name, $locale)
  201. {
  202. $loader = $this->_loaders[$name]($name, $locale);
  203. $package = $loader;
  204. if (!is_callable($loader)) {
  205. $loader = function () use ($package) {
  206. return $package;
  207. };
  208. }
  209. $loader = $this->setLoaderFallback($name, $loader);
  210. $this->packages->set($name, $locale, $loader);
  211. return parent::get($name, $locale);
  212. }
  213. /**
  214. * Set domain fallback for loader.
  215. *
  216. * @param string $name The name of the loader domain
  217. * @param callable $loader invokable loader
  218. * @return callable loader
  219. */
  220. public function setLoaderFallback($name, callable $loader)
  221. {
  222. $fallbackDomain = 'default';
  223. if (!$this->_useFallback || $name === $fallbackDomain) {
  224. return $loader;
  225. }
  226. $loader = function () use ($loader, $fallbackDomain) {
  227. $package = $loader();
  228. if (!$package->getFallback()) {
  229. $package->setFallback($fallbackDomain);
  230. }
  231. return $package;
  232. };
  233. return $loader;
  234. }
  235. }