php - Override translator Symfony2 -


i trying override symfony translator have translations database. first checking if translation not in catalogue , if not load database

<?php  namespace competitive\translationbundle\translation;  use competitive\translationbundle\entity\translationmanager; use symfony\component\translation\messageselector; use symfony\component\translation\translator basetranslator;  class translator extends basetranslator {     /**      * @var translationmanager      */     private $translationmanager;      public function __construct($locale, translationmanager $translationmanager, messageselector $selector = null, $cachedir = null, $debug = false)     {         parent::__construct($locale, $selector, $cachedir, $debug);         $this->translationmanager = $translationmanager;     }      /**      * {@inheritdoc}      */     public function trans($id, array $parameters = array(), $domain = null, $locale = null)     {         $cataloguedomain = $domain;         if (null === $cataloguedomain) {             $cataloguedomain = 'messages';         }          $locale = $locale === null ? $this->getlocale() : $locale;          if ($this->getcatalogue($locale)->has($id, $domain)) {             return parent::trans($id, $parameters, $cataloguedomain, $locale);         }          $translations = $this->translationmanager->findtranslationsby([             'key' => $id,             'locale' => $locale         ]);          if (empty($translations)) {             $translation = $this->translationmanager->create($id, $id, $locale);         } elseif (null === $domain) {             $translation = $translations[0];         } else {             foreach ($translations $trans) {                 if ($trans->getdomain() == $domain) {                     $translation = $trans;                     break;                 }             }              if (!isset($translation)) {                 $translation = $translations[0];             }         }          return strtr($translation->getvalue(), $parameters);     }      /**      * {@inheritdoc}      */     public function transchoice($id, $number, array $parameters = array(), $domain = null, $locale = null)     {         return $this->trans($id, $parameters, $domain, $locale);     } } 

translation.yml

services:     translator:         class: competitive\translationbundle\translation\translator         arguments:             - %locale%             - @competitive_translation.translation_manager 

the translation loaded database without problem.

but $this->getcatalogue($locale)->has($id, $domain) returns false (the translations catalogue working before override)

and cache translation folder app/cache/dev/translations not generated

you can use compiler pass override default translator class , use setter inject translation manager.

also should extend \symfony\bundle\frameworkbundle\translation\translator, not \symfony\component\translation\translator

translatorpass.php:

<?php  namespace competitive\translationbundle\dependencyinjection\compiler;  use competitive\translationbundle\translation\translator; use symfony\component\dependencyinjection\compiler\compilerpassinterface; use symfony\component\dependencyinjection\containerbuilder; use symfony\component\dependencyinjection\reference;  class translatorpass implements compilerpassinterface {     public function process(containerbuilder $container)     {         if (!$container->hasdefinition('translator.default')) {             return;         }          $definition = $container->getdefinition('translator.default');         $definition->setclass(translator::class);         $definition->addmethodcall('settranslationmanager', array(new reference('competitive_translation.translation_manager')));     } } 

translationbundle.php:

<?php  namespace competitive\translationbundle;  use competitive\translationbundle\dependencyinjection\compiler\translatorpass; use symfony\component\dependencyinjection\containerbuilder; use symfony\component\httpkernel\bundle\bundle;  class translationbundle extends bundle {     public function build(containerbuilder $container)     {         parent::build($container);          $container->addcompilerpass(new translatorpass());     } } 

translator.php:

<?php  namespace competitive\translationbundle\translation;  use competitive\translationbundle\entity\translationmanager; use symfony\bundle\frameworkbundle\translation\translator basetranslator;  class translator extends basetranslator {     /**      * @var translationmanager      */     private $translationmanager;       public function settranslationmanager(translationmanager $translationmanager){         $this->translationmanager = $translationmanager;     }      public function trans($id, array $parameters = array(), $domain = null, $locale = null){         // custom logic          return parent::trans($id, $parameters, $domain, $locale);     } } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -