4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation
;
14 use Symfony\Component\Translation\Loader\LoaderInterface
;
15 use Symfony\Component\Translation\Exception\NotFoundResourceException
;
20 * @author Fabien Potencier <fabien@symfony.com>
24 class Translator
implements TranslatorInterface
27 * @var MessageCatalogueInterface[]
29 protected $catalogues = array();
39 private $fallbackLocales = array();
42 * @var LoaderInterface[]
44 private $loaders = array();
49 private $resources = array();
52 * @var MessageSelector
59 * @param string $locale The locale
60 * @param MessageSelector|null $selector The message selector for pluralization
64 public function __construct($locale, MessageSelector
$selector = null)
66 $this->locale
= $locale;
67 $this->selector
= $selector ?: new MessageSelector();
73 * @param string $format The name of the loader (@see addResource())
74 * @param LoaderInterface $loader A LoaderInterface instance
78 public function addLoader($format, LoaderInterface
$loader)
80 $this->loaders
[$format] = $loader;
86 * @param string $format The name of the loader (@see addLoader())
87 * @param mixed $resource The resource name
88 * @param string $locale The locale
89 * @param string $domain The domain
93 public function addResource($format, $resource, $locale, $domain = null)
95 if (null === $domain) {
99 $this->resources
[$locale][] = array($format, $resource, $domain);
101 if (in_array($locale, $this->fallbackLocales
)) {
102 $this->catalogues
= array();
104 unset($this->catalogues
[$locale]);
113 public function setLocale($locale)
115 $this->locale
= $locale;
123 public function getLocale()
125 return $this->locale
;
129 * Sets the fallback locale(s).
131 * @param string|array $locales The fallback locale(s)
133 * @deprecated since 2.3, to be removed in 3.0. Use setFallbackLocales() instead.
137 public function setFallbackLocale($locales)
139 $this->setFallbackLocales(is_array($locales) ? $locales : array($locales));
143 * Sets the fallback locales.
145 * @param array $locales The fallback locales
149 public function setFallbackLocales(array $locales)
151 // needed as the fallback locales are linked to the already loaded catalogues
152 $this->catalogues
= array();
154 $this->fallbackLocales
= $locales;
158 * Gets the fallback locales.
160 * @return array $locales The fallback locales
164 public function getFallbackLocales()
166 return $this->fallbackLocales
;
174 public function trans($id, array $parameters = array(), $domain = null, $locale = null)
176 if (null === $locale) {
177 $locale = $this->getLocale();
180 if (null === $domain) {
181 $domain = 'messages';
184 if (!isset($this->catalogues
[$locale])) {
185 $this->loadCatalogue($locale);
188 return strtr($this->catalogues
[$locale]->get((string) $id, $domain), $parameters);
196 public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
198 if (null === $locale) {
199 $locale = $this->getLocale();
202 if (null === $domain) {
203 $domain = 'messages';
206 if (!isset($this->catalogues
[$locale])) {
207 $this->loadCatalogue($locale);
212 $catalogue = $this->catalogues
[$locale];
213 while (!$catalogue->defines($id, $domain)) {
214 if ($cat = $catalogue->getFallbackCatalogue()) {
216 $locale = $catalogue->getLocale();
222 return strtr($this->selector
->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters);
225 protected function loadCatalogue($locale)
228 $this->doLoadCatalogue($locale);
229 } catch (NotFoundResourceException
$e) {
230 if (!$this->computeFallbackLocales($locale)) {
234 $this->loadFallbackCatalogues($locale);
237 private function doLoadCatalogue($locale)
239 $this->catalogues
[$locale] = new MessageCatalogue($locale);
241 if (isset($this->resources
[$locale])) {
242 foreach ($this->resources
[$locale] as $resource) {
243 if (!isset($this->loaders
[$resource[0]])) {
244 throw new \
RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
246 $this->catalogues
[$locale]->addCatalogue($this->loaders
[$resource[0]]->load($resource[1], $locale, $resource[2]));
251 private function loadFallbackCatalogues($locale)
253 $current = $this->catalogues
[$locale];
255 foreach ($this->computeFallbackLocales($locale) as $fallback) {
256 if (!isset($this->catalogues
[$fallback])) {
257 $this->doLoadCatalogue($fallback);
260 $current->addFallbackCatalogue($this->catalogues
[$fallback]);
261 $current = $this->catalogues
[$fallback];
265 protected function computeFallbackLocales($locale)
268 foreach ($this->fallbackLocales
as $fallback) {
269 if ($fallback === $locale) {
273 $locales[] = $fallback;
276 if (strrchr($locale, '_') !== false) {
277 array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_'))));
280 return array_unique($locales);