2017-03-10 7 views
0

私のプロジェクトでは、翻訳翻訳会社を賢明にseprateする必要がありますので、Translatorクラスを作成してください。 「FatalErrorException:エラー:134217728バイトのメモリサイズが枯渇しました」というエラーが発生しました。私のクラスは続いているSymfony2翻訳ロード社は賢明ですが100社作成エラー:許容メモリサイズ

----- Added this code in service file ------ 
#<your project name>/src/Raghuvir/UserBundle/Resources/config/services.yml 
services: 
    translate: 
     class: Raghuvir\UserBundle\Translation\RaghuvirTranslator 
     arguments: ["@service_container", "@logger"] 

翻訳クラス

<?php 
namespace Raghuvir\UserBundle\Translation; 

use Symfony\Bundle\FrameworkBundle\Translation\Translator; 

class RaghuvirTranslator extends Translator 
{ 

    /** 
    * {@inheritdoc} 
    * 
    * @api 
    */ 
    public function trans($id, array $parameters = array(), $domain = null, $locale = null) 
    { 
     $snCompanyId = $this->container->get('session')->get('id_clinic'); 

     if (null === $locale) { 
      $locale = $this->getLocale(); 
     } 

     if ($snCompanyId == "") { 
      $domain = 'messages'; 
     }else{ 
      $domain = $snCompanyId.'_messages'; 
     } 

     $referef = $this->container->get('request')->server->get('REQUEST_URI'); 

     if(preg_match('/super/', $referef)) { 
      $domain = 'messages'; 
     } else if(preg_match('/admin/', $referef)) { 
      $adminLangFilePath = $this->container->get('kernel')->getRootDir() . '/../src/RaghuvirClinicAdmin/DefaultBundle/Resources/translations/'.$domain; 
      if($this->container->getParameter('admin_translation') == 0 || !file_exists($adminLangFilePath.'.'.$locale.'.yml')) { 
       $domain = 'messages'; 
      } 
     } else if(preg_match('/api/', $referef) || preg_match('/rest/', $referef)) { 
      $apiLangFilePath = $this->container->get('kernel')->getRootDir() . '/../src/RaghuvirApi/ApiBundle/Resources/translations/'.$domain; 
      if($this->container->getParameter('api_translation') == 0 || !file_exists($apiLangFilePath.'.'.$locale.'.yml')) { 
       $domain = 'messages'; 
      } 
     } else { 
      $domain = 'messages'; 
     } 

     if (!isset($this->catalogues[$locale])) { 
      $this->loadCatalogue($locale); 
     } 

     // Change translation domain to 'messages' if a translation can't be found in the 
     // current domain 
     if ('messages' !== $domain && false === $this->catalogues[$locale]->has((string) $id, $domain)) { 
      $domain = 'messages'; 
     } 

     return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters); 
    } 
} 

答えて

0

はい、あなたがエラーを取得します「FatalErrorExceptionエラー:使い果たさ134217728バイトの許可メモリサイズを」あなたは会社の賢明な翻訳ファイルを持っているであるため。ですから、twig/controller/serviceでtranslatorを呼び出すと、すべてのファイルにマージとキャッシュファイルの作成が含まれています。ファイルの場所は "app/cache/dev/translations/*"です。トランスレータを呼び出すキャッシュファイルを作成すると、キャッシュファイルからロードされ、ファイルに負荷の高いデータが格納されます。フォローコードチェック翻訳キャッシュフォルダを使用する前に、 "catalogue.fi.php and catalogue.fi.php.meta"のような言語的なファイルを取得します - コードの後に​​使用すると、 "6_catalogue.fi"のような別個の会社IDが作成されます。 PHPと6_catalogue.fi.php.meta」 だけ

<?php 
namespace Raghuvir\UserBundle\Translation; 

use Symfony\Bundle\FrameworkBundle\Translation\Translator; 
use Symfony\Component\Config\ConfigCache; 
use Symfony\Component\Translation\Loader\LoaderInterface; 
use Symfony\Component\Translation\MessageCatalogue; 

class RaghuvirTranslator extends Translator 
{ 
    /** 
    * @var LoaderInterface[] 
    */ 
    private $loaders = array(); 

    /** 
    * @var array 
    */ 
    private $resources = array(); 


    /** 
    * {@inheritdoc} 
    * 
    * @api 
    */ 
    public function trans($id, array $parameters = array(), $domain = null, $locale = null) 
    { 
     $snCompanyId = $this->container->get('session')->get('id_clinic'); 

     if (null === $locale) { 
      $locale = $this->getLocale(); 
     } 

     if ($snCompanyId == "") { 
      $domain = 'messages'; 
     }else{ 
      $domain = $snCompanyId.'_messages'; 
     } 

     $referef = $this->container->get('request')->server->get('REQUEST_URI'); 

     if(preg_match('/super/', $referef)) { 
      $domain = 'messages'; 
     } else if(preg_match('/admin/', $referef)) { 
      $adminLangFilePath = $this->container->get('kernel')->getRootDir() . '/../src/RaghuvirClinicAdmin/DefaultBundle/Resources/translations/'.$domain; 
      if($this->container->getParameter('admin_translation') == 0 || !file_exists($adminLangFilePath.'.'.$locale.'.yml')) { 
       $domain = 'messages'; 
      } 
     } else if(preg_match('/api/', $referef) || preg_match('/rest/', $referef)) { 
      $apiLangFilePath = $this->container->get('kernel')->getRootDir() . '/../src/RaghuvirApi/ApiBundle/Resources/translations/'.$domain; 
      if($this->container->getParameter('api_translation') == 0 || !file_exists($apiLangFilePath.'.'.$locale.'.yml')) { 
       $domain = 'messages'; 
      } 
     } else { 
      $domain = 'messages'; 
     } 

     if (!isset($this->catalogues[$locale])) { 
      $this->loadCatalogue($locale); 
     } 

     // Change translation domain to 'messages' if a translation can't be found in the 
     // current domain 
     if ('messages' !== $domain && false === $this->catalogues[$locale]->has((string) $id, $domain)) { 
      $domain = 'messages'; 
     } 

     return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    protected function loadCatalogue($locale) 
    { 
     if (isset($this->catalogues[$locale])) { 
      return; 
     } 

     if (null === $this->options['cache_dir']) { 
      $this->initialize(); 

      return $this->customeLoadCatalogue($locale); 
      //return parent::loadCatalogue($locale); 
     } 

     $snCompanyId = $this->container->get('session')->get('id_clinic'); 

     $domain = ($snCompanyId != "")? $snCompanyId.'_' : ''; 

     $cache = new ConfigCache($this->options['cache_dir'].'/'.$domain.'catalogue.'.$locale.'.php', $this->options['debug']); 

     if (!$cache->isFresh()) { 

      $this->initialize(); 

      //parent::loadCatalogue($locale); 
      $this->customeLoadCatalogue($locale); 

      $fallbackContent = ''; 
      $current = ''; 
      foreach ($this->computeFallbackLocales($locale) as $fallback) { 
       $fallbackContent .= sprintf(<<<EOF 
\$catalogue%s = new MessageCatalogue('%s', %s); 
\$catalogue%s->addFallbackCatalogue(\$catalogue%s); 


EOF 
        , 
        ucfirst($fallback), 
        $fallback, 
        var_export($this->catalogues[$fallback]->all(), true), 
        ucfirst($current), 
        ucfirst($fallback) 
       ); 
       $current = $fallback; 
      } 

      $content = sprintf(<<<EOF 
<?php 

use Symfony\Component\Translation\MessageCatalogue; 

\$catalogue = new MessageCatalogue('%s', %s); 

%s 
return \$catalogue; 

EOF 
       , 
       $locale, 
       var_export($this->catalogues[$locale]->all(), true), 
       $fallbackContent 
      ); 

      $cache->write($content, $this->catalogues[$locale]->getResources()); 

      return; 
     } 

     $this->catalogues[$locale] = include $cache; 
    } 


    protected function customeLoadCatalogue($locale) 
    { 
     $this->doLoadCatalogue($locale); 
     $this->loadFallbackCatalogues($locale); 
    } 


    private function doLoadCatalogue($locale) 
    { 
     $this->catalogues[$locale] = new MessageCatalogue($locale); 

     $snCompanyId = $this->container->get('session')->get('id_clinic'); 
     $domain = ($snCompanyId != "")? $snCompanyId.'_' : ''; 

     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])); 
       } 
       if($resource[2] == 'validators' || $resource[2] == 'messages' || 
        $resource[2] == $domain.'validators' || $resource[2] == $domain.'messages') { 
         $this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2])); 
       } 
      } 
     } 

    } 

    public function addLoader($format, LoaderInterface $loader) 
    { 
     $this->loaders[$format] = $loader; 
    } 

    public function addResource($format, $resource, $locale, $domain = 'messages') 
    { 

     $this->resources[$locale][] = array($format, $resource, $domain); 

     // unset($this->catalogues[$locale]); 
    } 

    private function loadFallbackCatalogues($locale) 
    { 
     $current = $this->catalogues[$locale]; 

     foreach ($this->computeFallbackLocales($locale) as $fallback) { 
      if (!isset($this->catalogues[$fallback])) { 
       $this->doLoadCatalogue($fallback); 
      } 

      $current->addFallbackCatalogue($this->catalogues[$fallback]); 
      $current = $this->catalogues[$fallback]; 
     } 
    } 
} 

はあなたの問題の解決策を得ることを願っています以下のクラスを使用して...

関連する問題