2014-05-10 23:14:12 +01:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Translation ;
use Psr\Log\LoggerInterface ;
2016-09-21 18:16:29 +01:00
use Symfony\Component\Translation\Exception\InvalidArgumentException ;
2018-12-05 07:43:42 +00:00
use Symfony\Contracts\Translation\LocaleAwareInterface ;
2018-08-16 14:45:57 +01:00
use Symfony\Contracts\Translation\TranslatorInterface ;
2014-05-10 23:14:12 +01:00
/**
* @ author Abdellatif Ait boudad < a . aitboudad @ gmail . com >
*/
2019-06-06 19:16:39 +01:00
class LoggingTranslator implements TranslatorInterface , TranslatorBagInterface , LocaleAwareInterface
2014-05-10 23:14:12 +01:00
{
/**
2015-04-14 21:48:01 +01:00
* @ var TranslatorInterface | TranslatorBagInterface
2014-05-10 23:14:12 +01:00
*/
private $translator ;
private $logger ;
/**
2015-04-14 21:48:01 +01:00
* @ param TranslatorInterface $translator The translator must implement TranslatorBagInterface
2014-05-10 23:14:12 +01:00
*/
2019-06-06 19:16:39 +01:00
public function __construct ( TranslatorInterface $translator , LoggerInterface $logger )
2014-05-10 23:14:12 +01:00
{
2018-12-05 07:43:42 +00:00
if ( ! $translator instanceof TranslatorBagInterface || ! $translator instanceof LocaleAwareInterface ) {
throw new InvalidArgumentException ( sprintf ( 'The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.' , \get_class ( $translator )));
2014-05-10 23:14:12 +01:00
}
$this -> translator = $translator ;
$this -> logger = $logger ;
}
/**
* { @ inheritdoc }
*/
2019-09-07 12:06:32 +01:00
public function trans ( ? string $id , array $parameters = [], string $domain = null , string $locale = null )
2014-05-10 23:14:12 +01:00
{
2019-09-07 12:06:32 +01:00
$trans = $this -> translator -> trans ( $id = ( string ) $id , $parameters , $domain , $locale );
2014-05-10 23:14:12 +01:00
$this -> log ( $id , $domain , $locale );
return $trans ;
}
/**
* { @ inheritdoc }
*/
2019-09-07 12:06:32 +01:00
public function setLocale ( string $locale )
2014-05-10 23:14:12 +01:00
{
2019-04-25 11:40:25 +01:00
$prev = $this -> translator -> getLocale ();
2014-05-10 23:14:12 +01:00
$this -> translator -> setLocale ( $locale );
2019-06-12 15:14:20 +01:00
if ( $prev === $locale ) {
return ;
}
2019-04-25 11:40:25 +01:00
$this -> logger -> debug ( sprintf ( 'The locale of the translator has changed from "%s" to "%s".' , $prev , $locale ));
2014-05-10 23:14:12 +01:00
}
/**
* { @ inheritdoc }
*/
public function getLocale ()
{
return $this -> translator -> getLocale ();
}
2015-02-10 13:48:21 +00:00
/**
* { @ inheritdoc }
*/
2019-06-27 12:49:03 +01:00
public function getCatalogue ( string $locale = null )
2015-02-10 13:48:21 +00:00
{
return $this -> translator -> getCatalogue ( $locale );
}
2016-11-09 20:23:10 +00:00
/**
* Gets the fallback locales .
*
2018-11-18 21:25:02 +00:00
* @ return array The fallback locales
2016-11-09 20:23:10 +00:00
*/
public function getFallbackLocales ()
{
2017-06-21 12:02:23 +01:00
if ( $this -> translator instanceof Translator || method_exists ( $this -> translator , 'getFallbackLocales' )) {
2016-11-09 20:23:10 +00:00
return $this -> translator -> getFallbackLocales ();
}
2019-01-16 20:35:37 +00:00
return [];
2016-11-09 20:23:10 +00:00
}
2014-05-10 23:14:12 +01:00
/**
* Passes through all unknown calls onto the translator object .
*/
2019-08-14 00:26:41 +01:00
public function __call ( string $method , array $args )
2014-05-10 23:14:12 +01:00
{
2018-09-11 08:26:54 +01:00
return $this -> translator -> { $method }( ... $args );
2014-05-10 23:14:12 +01:00
}
/**
* Logs for missing translations .
*/
2019-09-07 12:06:32 +01:00
private function log ( string $id , ? string $domain , ? string $locale )
2014-05-10 23:14:12 +01:00
{
if ( null === $domain ) {
$domain = 'messages' ;
}
$catalogue = $this -> translator -> getCatalogue ( $locale );
if ( $catalogue -> defines ( $id , $domain )) {
return ;
}
if ( $catalogue -> has ( $id , $domain )) {
2019-01-16 20:35:37 +00:00
$this -> logger -> debug ( 'Translation use fallback catalogue.' , [ 'id' => $id , 'domain' => $domain , 'locale' => $catalogue -> getLocale ()]);
2014-05-10 23:14:12 +01:00
} else {
2019-01-16 20:35:37 +00:00
$this -> logger -> warning ( 'Translation not found.' , [ 'id' => $id , 'domain' => $domain , 'locale' => $catalogue -> getLocale ()]);
2014-05-10 23:14:12 +01:00
}
}
}