2012-05-22 29 views
1

我有gedmo可翻譯擴展在我的zend框架應用程序中工作。我的意思是下面的代碼創建ext_translations表並在表中插入翻譯的文章。原則擴展(Gedmo)可翻譯 - 如何顯示基於當前語言環境的翻譯實體

$article = new \App\Entity\Article; 
$article->setTitle('my title in en'); 
$article->setContent('my content in en'); 
$this->_em->persist($article); 
$this->_em->flush(); 

//// first load the article 
$article = $this->_em->find('App\Entity\Article', 1 /*article id*/); 
$article->setTitle('my title in de'); 
$article->setContent('my content in de'); 
$article->setTranslatableLocale('de_de'); // change locale 
$this->_em->persist($article); 
$this->_em->flush(); 

// first load the article 
$article = $this->_em->find('App\Entity\Article', 1 /*article id*/); 
$article->setTitle('my title in es'); 
$article->setContent('my content in es'); 
$article->setTranslatableLocale('es_es'); // change locale 
$this->_em->persist($article); 
$this->_em->flush(); 

$article = $this->_em->getRepository('App\Entity\Article')->find(1/* id of article */); 
echo $article->getTitle(); 
// prints: "my title in en" 
echo $article->getContent(); 
// prints: "my content in en" 

上述工作和打印我已包含在評論中。但是,如果我將應用程序語言環境更改爲es_ES,它會提供相同的輸出,並且似乎沒有注意到區域設置的更改。

在我的引導,它被設置如下:

public function _initLocale() { 
     $session = new Zend_Session_Namespace('myswaplocalesession'); 
     if ($session->locale) { 
      $locale = new Zend_Locale($session->locale); 
     } 

     $config = $this->getOptions(); 

     if (!isset($locale) || $locale === null) { 
      try { 
       $locale = new Zend_Locale(Zend_Locale::BROWSER); 
      } catch (Zend_Locale_Exception $e) { 
       $locale = new Zend_Locale($config['resources']['locale']['default']); 
      } 

     } 
     Zend_Registry::set('Zend_Locale', $locale); 

     echo $locale; 

     $translator = new Zend_Translate('gettext', APPLICATION_PATH . '/../data/lang/', 
         null, array('scan' => Zend_Translate::LOCALE_FILENAME, 'disableNotices' => 1)); 


     Zend_Registry::set('Zend_Translate', $translator); 
     Zend_Form::setDefaultTranslator($translator); 
    } 

缺少什麼我在這裏?

回答

1

您必須告訴Gedmo缺省情況下必須使用的語言環境。

,當你知道用什麼語言環境,您可以添加這些行:

$listener = $this->getDoctrine()->getEventManager()->getListener(
    'Gedmo\Translatable\TranslatableListener' 
); 

$listener->setTranslatableLocale($locale); 
+0

嗨,我喜歡這個主意,但得到這個錯誤:「致命錯誤:未捕獲的異常‘Zend_Application_Bootstrap_Exception’有消息」無效的方法「getDoctrine 「'在/var/www/testsite/library/Zend/Application/Bootstrap/BootstrapAbstract.php上線605' – dimbo

+0

經過相當大的混亂之後,我設法在bootstrap中獲得了一個eventmanager的實例(參見我的其他問題之一細節),但可轉換的偵聽器在引導程序中不可用,因爲它尚未初始化。我認爲必須有另一種方法,不涉及陷入自舉的陷阱。以後可以翻譯不能從註冊表獲取區域設置值嗎? – dimbo