2012-11-05 93 views
0

我已经实现了CakePHP的翻译行为,并且都相当流畅,但我现在已经注意到我的i18n表的翻译数据不存在,当我假设该模型是contain()待翻译。CakePHP的翻译行为不适用于Containable

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

如果我打算使用Containable很多,是否有不同的“CakePHP方法”可以很容易地进行翻译?

回答

0

我遇到类似的问题,看了几十个来自谷歌的网页,但无法找到一个简单的解决我的问题。经过一些调试后,我创建了这个解决方法片段。请考虑到这是一个黑客攻击。它主要是为Croogo编写的,因此相关模型将在网站上进行翻译。但我浏览过翻译行为,它也应该为它工作。基本上将它粘贴到你的AppModel类中。这是蛋糕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; 
    } 
+0

我目前没有能力测试这个,但已经标记为答案希望它能够帮助其他人找到一种管理方式。谢谢。 – Dave

0

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

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

+1

它在哪里“提示如何处理它”?除了提到我需要使用fields()数组,这似乎没有任何影响。 – Dave

+0

好吧,我发现了另一个关于这个问题的长时间的讨论,关于如何解决这个问题的更多建议:[link](http://cakephp.lighthouseapp.com/projects/42648/tickets/95-afterfind-and -beforefind-callbacks-not-working-on-associated-models-was-translate-behavior-should-translate-associated-model-data) –