PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/comfortablynumb/symfony
PHP | 212 lines | 114 code | 29 blank | 69 comment | 12 complexity | b3028f018fe2bbf6ce5c9e9ebb2db1df 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. /**
  13. * Translator.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @api
  18. */
  19. class Translator implements TranslatorInterface
  20. {
  21. protected $catalogues;
  22. protected $locale;
  23. private $fallbackLocales;
  24. private $loaders;
  25. private $resources;
  26. private $selector;
  27. /**
  28. * Constructor.
  29. *
  30. * @param string $locale The locale
  31. * @param MessageSelector $selector The message selector for pluralization
  32. *
  33. * @api
  34. */
  35. public function __construct($locale, MessageSelector $selector)
  36. {
  37. $this->locale = $locale;
  38. $this->selector = $selector;
  39. $this->loaders = array();
  40. $this->resources = array();
  41. $this->catalogues = array();
  42. $this->fallbackLocales = array();
  43. }
  44. /**
  45. * Adds a Loader.
  46. *
  47. * @param string $format The name of the loader (@see addResource())
  48. * @param LoaderInterface $loader A LoaderInterface instance
  49. *
  50. * @api
  51. */
  52. public function addLoader($format, LoaderInterface $loader)
  53. {
  54. $this->loaders[$format] = $loader;
  55. }
  56. /**
  57. * Adds a Resource.
  58. *
  59. * @param string $format The name of the loader (@see addLoader())
  60. * @param mixed $resource The resource name
  61. * @param string $locale The locale
  62. * @param string $domain The domain
  63. *
  64. * @api
  65. */
  66. public function addResource($format, $resource, $locale, $domain = 'messages')
  67. {
  68. $this->resources[$locale][] = array($format, $resource, $domain);
  69. }
  70. /**
  71. * {@inheritdoc}
  72. *
  73. * @api
  74. */
  75. public function setLocale($locale)
  76. {
  77. $this->locale = $locale;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. *
  82. * @api
  83. */
  84. public function getLocale()
  85. {
  86. return $this->locale;
  87. }
  88. /**
  89. * Sets the fallback locale(s).
  90. *
  91. * @param string|array $locale The fallback locale(s)
  92. *
  93. * @api
  94. */
  95. public function setFallbackLocale($locales)
  96. {
  97. // needed as the fallback locales are linked to the already loaded catalogues
  98. $this->catalogues = array();
  99. $this->fallbackLocales = is_array($locales) ? $locales : array($locales);
  100. }
  101. /**
  102. * {@inheritdoc}
  103. *
  104. * @api
  105. */
  106. public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
  107. {
  108. if (!isset($locale)) {
  109. $locale = $this->getLocale();
  110. }
  111. if (!isset($this->catalogues[$locale])) {
  112. $this->loadCatalogue($locale);
  113. }
  114. return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters);
  115. }
  116. /**
  117. * {@inheritdoc}
  118. *
  119. * @api
  120. */
  121. public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
  122. {
  123. if (!isset($locale)) {
  124. $locale = $this->getLocale();
  125. }
  126. if (!isset($this->catalogues[$locale])) {
  127. $this->loadCatalogue($locale);
  128. }
  129. $id = (string) $id;
  130. $catalogue = $this->catalogues[$locale];
  131. while (!$catalogue->defines($id, $domain)) {
  132. if ($cat = $catalogue->getFallbackCatalogue()) {
  133. $catalogue = $cat;
  134. $locale = $catalogue->getLocale();
  135. } else {
  136. break;
  137. }
  138. }
  139. return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters);
  140. }
  141. protected function loadCatalogue($locale)
  142. {
  143. $this->doLoadCatalogue($locale);
  144. $this->loadFallbackCatalogues($locale);
  145. }
  146. private function doLoadCatalogue($locale)
  147. {
  148. $this->catalogues[$locale] = new MessageCatalogue($locale);
  149. if (isset($this->resources[$locale])) {
  150. foreach ($this->resources[$locale] as $resource) {
  151. if (!isset($this->loaders[$resource[0]])) {
  152. throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
  153. }
  154. $this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
  155. }
  156. }
  157. }
  158. private function loadFallbackCatalogues($locale)
  159. {
  160. $current = $this->catalogues[$locale];
  161. foreach ($this->computeFallbackLocales($locale) as $fallback) {
  162. if (!isset($this->catalogues[$fallback])) {
  163. $this->doLoadCatalogue($fallback);
  164. }
  165. $current->addFallbackCatalogue($this->catalogues[$fallback]);
  166. $current = $this->catalogues[$fallback];
  167. }
  168. }
  169. protected function computeFallbackLocales($locale)
  170. {
  171. $locales = array();
  172. foreach ($this->fallbackLocales as $fallback) {
  173. if ($fallback === $locale) {
  174. continue;
  175. }
  176. $locales[] = $fallback;
  177. }
  178. if (strlen($locale) > 3) {
  179. array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_'))));
  180. }
  181. return array_unique($locales);
  182. }
  183. }