2010-09-27 08:45:29 +01:00
< ? php
/*
2011-01-15 13:29:43 +00:00
* This file is part of the Symfony package .
2010-09-27 08:45:29 +01:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-09-27 08:45:29 +01:00
*
2011-01-15 13:29:43 +00: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
*/
2011-01-15 13:29:43 +00:00
namespace Symfony\Component\Translation ;
2011-02-10 15:15:51 +00:00
use Symfony\Component\Config\Resource\ResourceInterface ;
2011-01-15 13:29:43 +00:00
2010-09-27 08:45:29 +01:00
/**
* MessageCatalogue .
*
2011-03-06 11:40:06 +00:00
* @ author Fabien Potencier < fabien @ symfony . com >
2011-03-24 08:07:52 +00:00
*
* @ api
2010-09-27 08:45:29 +01:00
*/
class MessageCatalogue implements MessageCatalogueInterface
{
2011-03-08 19:47:32 +00:00
private $messages = array ();
private $locale ;
private $resources ;
2011-08-22 15:07:39 +01:00
private $fallbackCatalogue ;
2011-11-11 07:29:16 +00:00
private $parent ;
2010-09-27 08:45:29 +01:00
/**
* Constructor .
*
* @ param string $locale The locale
* @ param array $messages An array of messages classified by domain
2011-03-24 08:07:52 +00:00
*
* @ api
2010-09-27 08:45:29 +01:00
*/
public function __construct ( $locale , array $messages = array ())
{
$this -> locale = $locale ;
$this -> messages = $messages ;
$this -> resources = array ();
}
/**
* { @ inheritdoc }
2011-03-24 08:07:52 +00:00
*
* @ api
2010-09-27 08:45:29 +01:00
*/
public function getLocale ()
{
return $this -> locale ;
}
/**
* { @ inheritdoc }
2011-03-24 08:07:52 +00:00
*
* @ api
2010-09-27 08:45:29 +01:00
*/
public function getDomains ()
{
return array_keys ( $this -> messages );
}
/**
* { @ inheritdoc }
2011-03-24 08:07:52 +00:00
*
* @ api
2010-09-27 08:45:29 +01:00
*/
2010-11-23 08:42:19 +00:00
public function all ( $domain = null )
2010-09-27 08:45:29 +01:00
{
if ( null === $domain ) {
return $this -> messages ;
}
return isset ( $this -> messages [ $domain ]) ? $this -> messages [ $domain ] : array ();
}
/**
* { @ inheritdoc }
2011-03-24 08:07:52 +00:00
*
* @ api
2010-09-27 08:45:29 +01:00
*/
2010-11-23 08:42:19 +00:00
public function set ( $id , $translation , $domain = 'messages' )
2010-09-27 08:45:29 +01:00
{
2010-11-23 08:42:19 +00:00
$this -> add ( array ( $id => $translation ), $domain );
2010-09-27 08:45:29 +01:00
}
/**
* { @ inheritdoc }
2011-03-24 08:07:52 +00:00
*
* @ api
2010-09-27 08:45:29 +01:00
*/
2010-11-23 08:42:19 +00:00
public function has ( $id , $domain = 'messages' )
2011-09-14 21:29:44 +01:00
{
if ( isset ( $this -> messages [ $domain ][ $id ])) {
return true ;
}
if ( null !== $this -> fallbackCatalogue ) {
return $this -> fallbackCatalogue -> has ( $id , $domain );
}
return false ;
}
/**
* { @ inheritdoc }
*/
2011-09-14 21:49:04 +01:00
public function defines ( $id , $domain = 'messages' )
2010-09-27 08:45:29 +01:00
{
return isset ( $this -> messages [ $domain ][ $id ]);
}
/**
* { @ inheritdoc }
2011-03-24 08:07:52 +00:00
*
* @ api
2010-09-27 08:45:29 +01:00
*/
2010-11-23 08:42:19 +00:00
public function get ( $id , $domain = 'messages' )
2010-09-27 08:45:29 +01:00
{
2011-08-22 15:07:39 +01:00
if ( isset ( $this -> messages [ $domain ][ $id ])) {
return $this -> messages [ $domain ][ $id ];
}
if ( null !== $this -> fallbackCatalogue ) {
return $this -> fallbackCatalogue -> get ( $id , $domain );
}
return $id ;
2010-09-27 08:45:29 +01:00
}
/**
* { @ inheritdoc }
2011-03-24 08:07:52 +00:00
*
* @ api
2010-09-27 08:45:29 +01:00
*/
2010-11-23 08:42:19 +00:00
public function replace ( $messages , $domain = 'messages' )
2010-09-27 08:45:29 +01:00
{
2010-10-31 21:33:08 +00:00
$this -> messages [ $domain ] = array ();
2010-09-27 08:45:29 +01:00
2010-11-23 08:42:19 +00:00
$this -> add ( $messages , $domain );
2010-09-27 08:45:29 +01:00
}
/**
* { @ inheritdoc }
2011-03-24 08:07:52 +00:00
*
* @ api
2010-09-27 08:45:29 +01:00
*/
2010-11-23 08:42:19 +00:00
public function add ( $messages , $domain = 'messages' )
2010-09-27 08:45:29 +01:00
{
if ( ! isset ( $this -> messages [ $domain ])) {
$this -> messages [ $domain ] = $messages ;
} else {
$this -> messages [ $domain ] = array_replace ( $this -> messages [ $domain ], $messages );
}
}
/**
* { @ inheritdoc }
2011-03-24 08:07:52 +00:00
*
* @ api
2010-09-27 08:45:29 +01:00
*/
public function addCatalogue ( MessageCatalogueInterface $catalogue )
{
2010-10-31 21:33:08 +00:00
if ( $catalogue -> getLocale () !== $this -> locale ) {
throw new \LogicException ( sprintf ( 'Cannot add a catalogue for locale "%s" as the current locale for this catalogue is "%s"' , $catalogue -> getLocale (), $this -> locale ));
}
2010-11-23 08:42:19 +00:00
foreach ( $catalogue -> all () as $domain => $messages ) {
$this -> add ( $messages , $domain );
2010-09-27 08:45:29 +01:00
}
foreach ( $catalogue -> getResources () as $resource ) {
$this -> addResource ( $resource );
}
}
2010-10-31 21:33:08 +00:00
/**
* { @ inheritdoc }
2011-03-24 08:07:52 +00:00
*
* @ api
2010-10-31 21:33:08 +00:00
*/
public function addFallbackCatalogue ( MessageCatalogueInterface $catalogue )
{
2011-11-11 07:29:16 +00:00
// detect circular references
$c = $this ;
do {
if ( $c -> getLocale () === $catalogue -> getLocale ()) {
throw new \LogicException ( sprintf ( 'Circular reference detected when adding a fallback catalogue for locale "%s".' , $catalogue -> getLocale ()));
}
2011-11-11 08:02:31 +00:00
} while ( $c = $c -> parent );
2011-11-11 07:29:16 +00:00
2011-11-11 08:02:31 +00:00
$catalogue -> parent = $this ;
2011-08-22 15:07:39 +01:00
$this -> fallbackCatalogue = $catalogue ;
2010-10-31 21:33:08 +00:00
foreach ( $catalogue -> getResources () as $resource ) {
$this -> addResource ( $resource );
}
}
2010-09-27 08:45:29 +01:00
/**
* { @ inheritdoc }
2011-03-24 08:07:52 +00:00
*
* @ api
2010-09-27 08:45:29 +01:00
*/
public function getResources ()
{
2010-10-31 21:33:08 +00:00
return array_values ( array_unique ( $this -> resources ));
2010-09-27 08:45:29 +01:00
}
/**
* { @ inheritdoc }
2011-03-24 08:07:52 +00:00
*
* @ api
2010-09-27 08:45:29 +01:00
*/
public function addResource ( ResourceInterface $resource )
{
$this -> resources [] = $resource ;
}
}