| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Norsys\SeoBundle\Services\Translator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Translation\Exception\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Translation\TranslatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Class ConfigurableTranslator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class ConfigurableTranslator implements TranslatorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var TranslatorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $translator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $enableTranslator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $defaultDomain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * ConfigurableTranslator constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param TranslatorInterface $translator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param boolean             $enableTranslator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param string              $defaultDomain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         TranslatorInterface $translator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         bool $enableTranslator = true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         string $defaultDomain = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |         $this->translator = $translator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |         $this->enableTranslator = $enableTranslator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |         $this->defaultDomain = $defaultDomain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     // We start ignore coding standard because we have this error "Type hint "string" missing for $id" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     // but we can't add the type hint without break interface definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     // @codingStandardsIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * Translates the given message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param string      $id         The message id (may also be an object that can be cast to string). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param array       $parameters An array of parameters for the message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param string|null $domain     The domain for the message or null to use the default. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param string|null $locale     The locale or null to use the default. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @return string The translated string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @throws InvalidArgumentException If the locale contains invalid characters. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     public function trans($id, array $parameters = array(), $domain = null, $locale = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 1 |  |         if ($this->enableTranslator === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 1 |  |             return $id; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 1 |  |         return $this->translator->trans($id, $parameters, $this->getDomain($domain), $locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     // @codingStandardsIgnoreEnd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     // We start ignore coding standard because we have this error "Type hint "string" missing for $id" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     // but we can't add the type hint without break interface definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     // @codingStandardsIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * Translates the given choice message by choosing a translation according to a number. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param string      $id         The message id (may also be an object that can be cast to string). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param integer     $number     The number to use to find the indice of the message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param array       $parameters An array of parameters for the message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param string|null $domain     The domain for the message or null to use the default. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param string|null $locale     The locale or null to use the default. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @return string The translated string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @throws InvalidArgumentException If the locale contains invalid characters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |         if ($this->enableTranslator === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |             return $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |         return $this->translator->transChoice($id, $number, $parameters, $this->getDomain($domain), $locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     // @codingStandardsIgnoreEnd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     // We start ignore coding standard because we have this error "Type hint "string" missing for $locale" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     // but we can't add the type hint without break interface definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     // @codingStandardsIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * Sets the current locale. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param string $locale The locale. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @throws InvalidArgumentException If the locale contains invalid characters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function setLocale($locale) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |         $this->translator->setLocale($locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     // @codingStandardsIgnoreEnd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * Returns the current locale. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @return string The locale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     public function getLocale() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 1 |  |         return $this->translator->getLocale(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param string|null $domain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     private function getDomain(string $domain = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  |         if ($domain === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |             return $this->defaultDomain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |         return $domain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |  |