Translator.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. * @api
  53. */
  54. public function __construct($locale, MessageSelector $selector = null)
  55. {
  56. $this->locale = $locale;
  57. $this->selector = $selector ?: new MessageSelector();
  58. }
  59. /**
  60. * Adds a Loader.
  61. *
  62. * @param string $format The name of the loader (@see addResource())
  63. * @param LoaderInterface $loader A LoaderInterface instance
  64. *
  65. * @api
  66. */
  67. public function addLoader($format, LoaderInterface $loader)
  68. {
  69. $this->loaders[$format] = $loader;
  70. }
  71. /**
  72. * Adds a Resource.
  73. *
  74. * @param string $format The name of the loader (@see addLoader())
  75. * @param mixed $resource The resource name
  76. * @param string $locale The locale
  77. * @param string $domain The domain
  78. *
  79. * @api
  80. */
  81. public function addResource($format, $resource, $locale, $domain = null)
  82. {
  83. if (null === $domain) {
  84. $domain = 'messages';
  85. }
  86. $this->resources[$locale][] = array($format, $resource, $domain);
  87. if (in_array($locale, $this->fallbackLocales)) {
  88. $this->catalogues = array();
  89. } else {
  90. unset($this->catalogues[$locale]);
  91. }
  92. }
  93. /**
  94. * {@inheritdoc}
  95. *
  96. * @api
  97. */
  98. public function setLocale($locale)
  99. {
  100. $this->locale = $locale;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. *
  105. * @api
  106. */
  107. public function getLocale()
  108. {
  109. return $this->locale;
  110. }
  111. /**
  112. * Sets the fallback locale(s).
  113. *
  114. * @param string|array $locales The fallback locale(s)
  115. *
  116. * @deprecated since 2.3, to be removed in 3.0. Use setFallbackLocales() instead.
  117. *
  118. * @api
  119. */
  120. public function setFallbackLocale($locales)
  121. {
  122. $this->setFallbackLocales(is_array($locales) ? $locales : array($locales));
  123. }
  124. /**
  125. * Sets the fallback locales.
  126. *
  127. * @param array $locales The fallback locales
  128. *
  129. * @api
  130. */
  131. public function setFallbackLocales(array $locales)
  132. {
  133. // needed as the fallback locales are linked to the already loaded catalogues
  134. $this->catalogues = array();
  135. $this->fallbackLocales = $locales;
  136. }
  137. /**
  138. * Gets the fallback locales.
  139. *
  140. * @return array $locales The fallback locales
  141. *
  142. * @api
  143. */
  144. public function getFallbackLocales()
  145. {
  146. return $this->fallbackLocales;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. *
  151. * @api
  152. */
  153. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  154. {
  155. if (null === $locale) {
  156. $locale = $this->getLocale();
  157. }
  158. if (null === $domain) {
  159. $domain = 'messages';
  160. }
  161. if (!isset($this->catalogues[$locale])) {
  162. $this->loadCatalogue($locale);
  163. }
  164. return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters);
  165. }
  166. /**
  167. * {@inheritdoc}
  168. *
  169. * @api
  170. */
  171. public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
  172. {
  173. if (null === $locale) {
  174. $locale = $this->getLocale();
  175. }
  176. if (null === $domain) {
  177. $domain = 'messages';
  178. }
  179. if (!isset($this->catalogues[$locale])) {
  180. $this->loadCatalogue($locale);
  181. }
  182. $id = (string) $id;
  183. $catalogue = $this->catalogues[$locale];
  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. protected function loadCatalogue($locale)
  195. {
  196. try {
  197. $this->doLoadCatalogue($locale);
  198. } catch (NotFoundResourceException $e) {
  199. if (!$this->computeFallbackLocales($locale)) {
  200. throw $e;
  201. }
  202. }
  203. $this->loadFallbackCatalogues($locale);
  204. }
  205. private function doLoadCatalogue($locale)
  206. {
  207. $this->catalogues[$locale] = new MessageCatalogue($locale);
  208. if (isset($this->resources[$locale])) {
  209. foreach ($this->resources[$locale] as $resource) {
  210. if (!isset($this->loaders[$resource[0]])) {
  211. throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
  212. }
  213. $this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
  214. }
  215. }
  216. }
  217. private function loadFallbackCatalogues($locale)
  218. {
  219. $current = $this->catalogues[$locale];
  220. foreach ($this->computeFallbackLocales($locale) as $fallback) {
  221. if (!isset($this->catalogues[$fallback])) {
  222. $this->doLoadCatalogue($fallback);
  223. }
  224. $current->addFallbackCatalogue($this->catalogues[$fallback]);
  225. $current = $this->catalogues[$fallback];
  226. }
  227. }
  228. protected function computeFallbackLocales($locale)
  229. {
  230. $locales = array();
  231. foreach ($this->fallbackLocales as $fallback) {
  232. if ($fallback === $locale) {
  233. continue;
  234. }
  235. $locales[] = $fallback;
  236. }
  237. if (strrchr($locale, '_') !== false) {
  238. array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_'))));
  239. }
  240. return array_unique($locales);
  241. }
  242. }