Doctrine扩展(Gedmo)可翻译 - 如何根据当前区域设置显示已翻译的实体

时间:2012-05-22 11:28:13

标签: zend-framework doctrine doctrine-orm zend-translate

我的zend框架应用程序中有gedmo可翻译扩展。我的意思是以下代码创建ext_translations表并将翻译的文章插入表中。

$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 个答案:

答案 0 :(得分:1)

您必须告诉Gedmo默认情况下必须使用哪种语言环境。

当您知道要使用的语言环境时,可以添加以下行:

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

$listener->setTranslatableLocale($locale);