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

169 lines
4.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>
2010-09-27 08:45:29 +01:00
*/
class Translator implements TranslatorInterface
{
protected $catalogues;
protected $locale;
private $fallbackLocale;
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
2010-09-27 08:45:29 +01:00
*/
public function __construct($locale = null, 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();
}
/**
* Adds a Loader.
*
* @param string $format The name of the loader (@see addResource())
* @param LoaderInterface $loader A LoaderInterface instance
*/
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
*/
public function addResource($format, $resource, $locale, $domain = 'messages')
{
if (!isset($this->resources[$locale])) {
$this->resources[$locale] = array();
}
$this->resources[$locale][] = array($format, $resource, $domain);
}
/**
* {@inheritdoc}
*/
public function setLocale($locale)
{
$this->locale = $locale;
}
2010-10-06 13:18:36 +01:00
/**
* {@inheritdoc}
*/
public function getLocale()
{
return $this->locale;
}
2010-09-27 08:45:29 +01:00
/**
* Sets the fallback locale.
*
* @param string $locale The fallback locale
*/
public function setFallbackLocale($locale)
{
2010-10-31 21:33:08 +00:00
// needed as the fallback locale is used to fill-in non-yet translated messages
$this->catalogues = array();
2010-09-27 08:45:29 +01:00
$this->fallbackLocale = $locale;
}
/**
* {@inheritdoc}
*/
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);
}
made some method name changes to have a better coherence throughout the framework When an object has a "main" many relation with related "things" (objects, parameters, ...), the method names are normalized: * get() * set() * all() * replace() * remove() * clear() * isEmpty() * add() * register() * count() * keys() The classes below follow this method naming convention: * BrowserKit\CookieJar -> Cookie * BrowserKit\History -> Request * Console\Application -> Command * Console\Application\Helper\HelperSet -> HelperInterface * DependencyInjection\Container -> services * DependencyInjection\ContainerBuilder -> services * DependencyInjection\ParameterBag\ParameterBag -> parameters * DependencyInjection\ParameterBag\FrozenParameterBag -> parameters * DomCrawler\Form -> FormField * EventDispatcher\Event -> parameters * Form\FieldGroup -> Field * HttpFoundation\HeaderBag -> headers * HttpFoundation\ParameterBag -> parameters * HttpFoundation\Session -> attributes * HttpKernel\Profiler\Profiler -> DataCollectorInterface * Routing\RouteCollection -> Route * Security\Authentication\AuthenticationProviderManager -> AuthenticationProviderInterface * Templating\Engine -> HelperInterface * Translation\MessageCatalogue -> messages The usage of these methods are only allowed when it is clear that there is a main relation: * a CookieJar has many Cookies; * a Container has many services and many parameters (as services is the main relation, we use the naming convention for this relation); * a Console Input has many arguments and many options. There is no "main" relation, and so the naming convention does not apply. For many relations where the convention does not apply, the following methods must be used instead (where XXX is the name of the related thing): * get() -> getXXX() * set() -> setXXX() * all() -> getXXXs() * replace() -> setXXXs() * remove() -> removeXXX() * clear() -> clearXXX() * isEmpty() -> isEmptyXXX() * add() -> addXXX() * register() -> registerXXX() * count() -> countXXX() * keys()
2010-11-23 08:42:19 +00:00
return strtr($this->catalogues[$locale]->get($id, $domain), $parameters);
2010-09-27 08:45:29 +01:00
}
/**
* {@inheritdoc}
*/
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);
}
made some method name changes to have a better coherence throughout the framework When an object has a "main" many relation with related "things" (objects, parameters, ...), the method names are normalized: * get() * set() * all() * replace() * remove() * clear() * isEmpty() * add() * register() * count() * keys() The classes below follow this method naming convention: * BrowserKit\CookieJar -> Cookie * BrowserKit\History -> Request * Console\Application -> Command * Console\Application\Helper\HelperSet -> HelperInterface * DependencyInjection\Container -> services * DependencyInjection\ContainerBuilder -> services * DependencyInjection\ParameterBag\ParameterBag -> parameters * DependencyInjection\ParameterBag\FrozenParameterBag -> parameters * DomCrawler\Form -> FormField * EventDispatcher\Event -> parameters * Form\FieldGroup -> Field * HttpFoundation\HeaderBag -> headers * HttpFoundation\ParameterBag -> parameters * HttpFoundation\Session -> attributes * HttpKernel\Profiler\Profiler -> DataCollectorInterface * Routing\RouteCollection -> Route * Security\Authentication\AuthenticationProviderManager -> AuthenticationProviderInterface * Templating\Engine -> HelperInterface * Translation\MessageCatalogue -> messages The usage of these methods are only allowed when it is clear that there is a main relation: * a CookieJar has many Cookies; * a Container has many services and many parameters (as services is the main relation, we use the naming convention for this relation); * a Console Input has many arguments and many options. There is no "main" relation, and so the naming convention does not apply. For many relations where the convention does not apply, the following methods must be used instead (where XXX is the name of the related thing): * get() -> getXXX() * set() -> setXXX() * all() -> getXXXs() * replace() -> setXXXs() * remove() -> removeXXX() * clear() -> clearXXX() * isEmpty() -> isEmptyXXX() * add() -> addXXX() * register() -> registerXXX() * count() -> countXXX() * keys()
2010-11-23 08:42:19 +00:00
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)
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
}
}
$this->optimizeCatalogue($locale);
}
private function optimizeCatalogue($locale)
2010-09-27 08:45:29 +01:00
{
if (strlen($locale) > 3) {
$fallback = substr($locale, 0, -strlen(strrchr($locale, '_')));
} else {
$fallback = $this->fallbackLocale;
}
2010-10-31 21:33:08 +00:00
if (!$fallback) {
return;
}
2010-09-27 08:45:29 +01:00
if (!isset($this->catalogues[$fallback])) {
$this->loadCatalogue($fallback);
}
2010-10-31 21:33:08 +00:00
$this->catalogues[$locale]->addFallbackCatalogue($this->catalogues[$fallback]);
2010-09-27 08:45:29 +01:00
}
}