2017-04-12 56 views
0

緩存在我的應用程序的翻譯,我有兩類譯文:添加不應該從控制器

  • 靜態字眼是模板佈局(如「上一頁」,「取消」的一部分。 ..)
  • 動態字眼正在從數據庫中提取,並可以爲每個請求改變(如博客文章標題,可以在數據庫中會發生變化)

我用Symfony的翻譯小號請在我的樹枝模板中使用靜態措辭,使用過濾器。它可以很好地處理靜態文字,但動態文字變得越來越困難。

添加我動態字眼這樣,一個控制器動作中:

$trans = $this->get('translator'); 

    $trans->addLoader('array', new ArrayLoader()); 
    $trans->addResource('array', array('BLOG_ARTICLE_TITLE'=>$article->getTitle('french')), 'fr', 'messages'); 

    // ... 

    return new Response(); 

然後,我在前端應用與跨在我的模板或與BazingaJsTranslationBundle使用它。

我想我有一個緩存問題:對於動態措辭,我經常會在頁面上得到舊的字詞或翻譯鍵 - 即使在返回響應之前在控制器上完成添加。

但是,當我清除緩存(應用程序/控制檯清除:緩存)並重新加載頁面時,我得到正確的措辭。

有沒有辦法告訴Symfony不緩存動態添加的措辭? 還是另一種適合更多翻譯動態添加的翻譯方法?

回答

1

Symfony的文檔:

翻譯數據庫內容

的數據庫內容的翻譯應該由楊興華the Translatable Extension處理或的譯行爲 (PHP 5.4+)。有關更多信息,請參閱這些 庫的文檔。

這是用這樣的:

use Gedmo\Translatable\Translatable; 

// [...] 
class Article implements Translatable 
{ 
    /** @ODM\Id */ 
    private $id; 

    /** 
    * @Gedmo\Translatable 
    * @ODM\String 
    */ 
    private $title; 

    /** 
    * @Gedmo\Translatable 
    * @ODM\String 
    */ 
    private $content; 

    /** 
    * @Gedmo\Locale 
    * Used locale to override Translation listener`s locale 
    * this is not a mapped field of entity metadata, just a simple property 
    */ 
    private $locale; 

針對您的具體問題:

您可以刪除緩存文件手動 Lexik翻譯捆綁使用該系統。 see their Translator.php class below

public function removeLocalesCacheFiles(array $locales) 
{ 
    foreach ($locales as $locale) { 
     $this->removeCacheFile($locale); 
    } 
    // also remove database.resources.php cache file 
    $file = sprintf('%s/database.resources.php', $this->options['cache_dir']); 
    if (file_exists($file)) { 
     $this->invalidateSystemCacheForFile($file); 
     unlink($file); 
    } 
    $metadata = $file.'.meta'; 
    if (file_exists($metadata)) { 
     $this->invalidateSystemCacheForFile($metadata); 
     unlink($metadata); 
    } 
} 
/** 
* @param string $path 
* 
* @throws \RuntimeException 
*/ 
protected function invalidateSystemCacheForFile($path) 
{ 
    if (ini_get('apc.enabled')) { 
     if (apc_exists($path) && !apc_delete_file($path)) { 
      throw new \RuntimeException(sprintf('Failed to clear APC Cache for file %s', $path)); 
     } 
    } elseif ('cli' === php_sapi_name() ? ini_get('opcache.enable_cli') : ini_get('opcache.enable')) { 
     if (!opcache_invalidate($path, true)) { 
      throw new \RuntimeException(sprintf('Failed to clear OPCache for file %s', $path)); 
     } 
    } 
}