1

我已经实现了 CakePHP 的 Translate Behavior,一切都相当顺利,但我现在注意到,当我的模型应该被翻译时,我从i18n表中翻译的数据不存在。contain()

翻译行为是否不适用于包含的模型?如果是这样,那不是几乎完全消除了这种行为的任何用处吗?(或者也许只是我 - 但我几乎所有事情都使用 Containable)。

如果我打算大量使用 Containable,是否有不同的“CakePHP 方式”可以相当轻松地进行翻译?

4

2 回答 2

0

显然这是一个普遍的问题。CakePHP Cookbook 有一些关于如何处理它的提示:

http://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html

于 2012-11-06T14:24:40.627 回答
0

我遇到过类似的问题,从谷歌阅读了几十页,但找不到解决我问题的简单方法。经过一些调试后,我创建了这个解决方法片段。请考虑到这只是一个黑客行为。它主要是为 Croogo 编写的,因此相关模型将出现在现场翻译。但我已经浏览了翻译行为,它也应该适用。基本上将其粘贴到您的 AppModel 类中。适用于 Cake 2.x

// DIRTY LITTLE HACKS, FORCING TRANSLATE BEHAVIOR AFTERFIND CALLBACK
    /**
     * Hacking the afterFind so it will call the afterFind() from 
     * behavior
     * Pase this in your AppModel Class
     * 
     * @param array $results
     * @param bool $primary
     * @return array 
     */
    public function afterFind(array $results, $primary = false) {
        parent::afterFind($results, $primary);
        # calling only if not primary model, as they get translated pretty well
        if (!$primary) {
            # iterating behaviors to look for one that has something to do
            # with translations ( Translate for cake general behavior, CroogoTranslate for Croogo based apps )        
            foreach ($this->Behaviors->enabled() as $behavior) {
                if (preg_match('/(.*)[T|t]ranslate(.*)/', $behavior)) {
                    # setting locale, not sure if it gets set on secondary models
                    $this->locale = Configure::read('Config.language');
                    # hacking the result set to match behaviours requirments
                    # so basically creating the result set to look like called from originated model
                    # $k => array('ModelAlias' => array $results)        
                    $results_tmp = array(
                        0 => array(
                            $this->alias => $results,
                        )
                    );
                    # if we find such behavior we force it's afterFind with prepared data
                    $results = $this->Behaviors->{$behavior}->afterFind($this, $results_tmp, true); # forcing true on primary - CroogoTranslate requires that
                    # restoring orginal structure like nothing ever happened     
                    $results = $results[0][$this->alias];
                    # not sure if should break or not ?
                    # on one hand what's the point of having multiple translate behaviors in one app ?
                    # on the other i've seen more weird stuff that multiple translate behaviors
                    break;
                }
            }
        }
        return $results;
    }
于 2013-04-05T10:24:38.947 回答