【问题标题】:CakePHP's Translate Behavior not working with ContainableCakePHP 的翻译行为不适用于 Containable
【发布时间】:2013-04-05 18:24:38
【问题描述】:

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

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

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

【问题讨论】:

    标签: cakephp behavior translate containable cakephp-2.3


    【解决方案1】:

    我遇到过类似的问题,从 Google 阅读了几十页,但找不到解决问题的简单方法。经过一些调试后,我创建了这个解决方法 sn-p。请考虑到这只是一个黑客行为。它主要是为 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;
        }
    

    【讨论】:

    • 我目前无法对此进行测试,但已将其标记为答案,希望它可以帮助其他人找到管理方法。谢谢。
    【解决方案2】:

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

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

    【讨论】:

    • 它在哪里有“关于如何处理它的提示”?除了提到我需要使用 fields() 数组,这似乎没有任何影响。
    • 好吧,我发现了另一个关于这个问题的长期讨论,其中提供了更多关于如何解决这个问题的建议:link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多