This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Translation/Translator.php

212 lines
5.4 KiB
PHP
Raw Normal View History

2010-09-27 08:45:29 +01:00
<?php
/*
* This file is part of the Symfony package.
2010-09-27 08:45:29 +01:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-09-27 08:45:29 +01:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2010-09-27 08:45:29 +01:00
*/
namespace Symfony\Component\Translation;
use Symfony\Component\Translation\Loader\LoaderInterface;
2010-09-27 08:45:29 +01:00
/**
* Translator.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @api
2010-09-27 08:45:29 +01:00
*/
class Translator implements TranslatorInterface
{
protected $catalogues;
protected $locale;
private $fallbackLocales;
private $loaders;
private $resources;
private $selector;
2010-09-27 08:45:29 +01:00
/**
* Constructor.
*
* @param string $locale The locale
* @param MessageSelector $selector The message selector for pluralization
*
* @api
2010-09-27 08:45:29 +01:00
*/
public function __construct($locale, MessageSelector $selector)
2010-09-27 08:45:29 +01:00
{
$this->locale = $locale;
$this->selector = $selector;
2010-09-27 08:45:29 +01:00
$this->loaders = array();
$this->resources = array();
$this->catalogues = array();
$this->fallbackLocales = array();
2010-09-27 08:45:29 +01:00
}
/**
* Adds a Loader.
*
* @param string $format The name of the loader (@see addResource())
* @param LoaderInterface $loader A LoaderInterface instance
*
* @api
2010-09-27 08:45:29 +01:00
*/
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
2010-09-27 08:45:29 +01:00
*/
public function addResource($format, $resource, $locale, $domain = 'messages')
{
$this->resources[$locale][] = array($format, $resource, $domain);
}
/**
* {@inheritdoc}
*
* @api
2010-09-27 08:45:29 +01:00
*/
public function setLocale($locale)
{
$this->locale = $locale;
}
2010-10-06 13:18:36 +01:00
/**
* {@inheritdoc}
*
* @api
2010-10-06 13:18:36 +01:00
*/
public function getLocale()
{
return $this->locale;
}
2010-09-27 08:45:29 +01:00
/**
* Sets the fallback locale(s).
2010-09-27 08:45:29 +01:00
*
* @param string|array $locale The fallback locale(s)
*
* @api
2010-09-27 08:45:29 +01:00
*/
public function setFallbackLocale($locales)
2010-09-27 08:45:29 +01:00
{
// needed as the fallback locales are linked to the already loaded catalogues
2010-10-31 21:33:08 +00:00
$this->catalogues = array();
2010-09-27 08:45:29 +01:00
$this->fallbackLocales = is_array($locales) ? $locales : array($locales);
2010-09-27 08:45:29 +01:00
}
/**
* {@inheritdoc}
*
* @api
2010-09-27 08:45:29 +01:00
*/
public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
{
if (!isset($locale)) {
$locale = $this->getLocale();
2010-09-27 08:45:29 +01:00
}
if (!isset($this->catalogues[$locale])) {
$this->loadCatalogue($locale);
}
return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters);
2010-09-27 08:45:29 +01:00
}
/**
* {@inheritdoc}
*
* @api
2010-09-27 08:45:29 +01:00
*/
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
{
if (!isset($locale)) {
$locale = $this->getLocale();
2010-09-27 08:45:29 +01:00
}
if (!isset($this->catalogues[$locale])) {
$this->loadCatalogue($locale);
}
$id = (string) $id;
if (!$this->catalogues[$locale]->defines($id, $domain)) {
// we will use the locale fallback
foreach ($this->computeFallbackLocales($locale) as $fallback) {
if ($this->catalogues[$fallback]->defines($id, $domain)) {
$locale = $fallback;
break;
}
}
}
return strtr($this->selector->choose($this->catalogues[$locale]->get($id, $domain), (int) $number, $locale), $parameters);
2010-09-27 08:45:29 +01:00
}
protected function loadCatalogue($locale)
{
$this->doLoadCatalogue($locale);
2011-09-16 12:27:17 +01:00
$this->loadFallbackCatalogues($locale);
}
protected function doLoadCatalogue($locale)
2010-09-27 08:45:29 +01:00
{
$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]));
}
$this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
2010-09-27 08:45:29 +01:00
}
}
}
2011-09-16 12:27:17 +01:00
private function loadFallbackCatalogues($locale)
2010-09-27 08:45:29 +01:00
{
$current = $this->catalogues[$locale];
foreach ($this->computeFallbackLocales($locale) as $fallback) {
if (!isset($this->catalogues[$fallback])) {
$this->doLoadCatalogue($fallback);
}
2010-10-31 21:33:08 +00:00
$current->addFallbackCatalogue($this->catalogues[$fallback]);
$current = $this->catalogues[$fallback];
2010-09-27 08:45:29 +01:00
}
}
private function computeFallbackLocales($locale)
{
2011-09-20 06:50:47 +01:00
$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 $locales;
}
2010-09-27 08:45:29 +01:00
}