* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Translation; use Symfony\Component\Translation\Loader\LoaderInterface; /** * Translator. * * @author Fabien Potencier * * @api */ class Translator implements TranslatorInterface { protected $catalogues; protected $locale; private $fallbackLocales; private $loaders; private $resources; private $selector; private $charset; /** * Constructor. * * @param string $locale The locale * @param MessageSelector $selector The message selector for pluralization * @param string $charset Application charset * * @api */ public function __construct($locale, MessageSelector $selector, $charset = null) { $this->locale = $locale; $this->selector = $selector; $this->loaders = array(); $this->resources = array(); $this->catalogues = array(); $this->fallbackLocales = array(); $this->charset = $charset; } /** * Adds a Loader. * * @param string $format The name of the loader (@see addResource()) * @param LoaderInterface $loader A LoaderInterface instance * * @api */ public function addLoader($format, LoaderInterface $loader) { $this->loaders[$format] = $loader; } /** * Adds a Resource. * * @param string $format The name of the loader (@see addLoader()) * @param mixed $resource The resource name * @param string $locale The locale * @param string $domain The domain * * @api */ public function addResource($format, $resource, $locale, $domain = 'messages') { $this->resources[$locale][] = array($format, $resource, $domain); } /** * {@inheritdoc} * * @api */ public function setLocale($locale) { $this->locale = $locale; } /** * {@inheritdoc} * * @api */ public function getLocale() { return $this->locale; } /** * Sets the fallback locale(s). * * @param string|array $locale The fallback locale(s) * * @api */ public function setFallbackLocale($locales) { // needed as the fallback locales are linked to the already loaded catalogues $this->catalogues = array(); $this->fallbackLocales = is_array($locales) ? $locales : array($locales); } /** * {@inheritdoc} * * @api */ public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null) { if (!isset($locale)) { $locale = $this->getLocale(); } if (!isset($this->catalogues[$locale])) { $this->loadCatalogue($locale); } return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters); } /** * {@inheritdoc} * * @api */ public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null) { if (!isset($locale)) { $locale = $this->getLocale(); } if (!isset($this->catalogues[$locale])) { $this->loadCatalogue($locale); } $id = (string) $id; $catalogue = $this->catalogues[$locale]; while (!$catalogue->defines($id, $domain)) { if ($cat = $catalogue->getFallbackCatalogue()) { $catalogue = $cat; $locale = $catalogue->getLocale(); } else { break; } } return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters); } protected function loadCatalogue($locale) { $this->doLoadCatalogue($locale); $this->loadFallbackCatalogues($locale); } private function doLoadCatalogue($locale) { $this->catalogues[$locale] = new MessageCatalogue($locale); if (isset($this->resources[$locale])) { foreach ($this->resources[$locale] as $resource) { if (!isset($this->loaders[$resource[0]])) { throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0])); } $catalogue = $this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]); if (null !== $this->charset && extension_loaded('mbstring')) { foreach ($catalogue->all() as $domain => $messages) { foreach ($messages as $key => $translation) { $srcCharset = mb_detect_encoding($translation); if ($srcCharset !== $this->charset) { $catalogue->set($key, mb_convert_encoding($translation, $this->charset, $srcCharset), $domain); } } } } $this->catalogues[$locale]->addCatalogue($catalogue); } } } private function loadFallbackCatalogues($locale) { $current = $this->catalogues[$locale]; foreach ($this->computeFallbackLocales($locale) as $fallback) { if (!isset($this->catalogues[$fallback])) { $this->doLoadCatalogue($fallback); } $current->addFallbackCatalogue($this->catalogues[$fallback]); $current = $this->catalogues[$fallback]; } } protected function computeFallbackLocales($locale) { $locales = array(); foreach ($this->fallbackLocales as $fallback) { if ($fallback === $locale) { continue; } $locales[] = $fallback; } if (strlen($locale) > 3) { array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_')))); } return array_unique($locales); } }