1

我有 gedmo 可翻译扩展在我的 zend 框架应用程序中工作。我的意思是下面的代码创建了 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);
    }

我在这里想念什么?

4

1 回答 1

1

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

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

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

$listener->setTranslatableLocale($locale);
于 2012-09-18T11:17:09.657 回答