Question

I've implemented CakePHP's Translate Behavior, and all went fairly smooth, but I've now noticed that my translated data from the i18n table doesn't exist when I contain() a model that is supposed to be translated.

Does the Translate Behavior not work for contained models? If so, doesn't that near-completely remove any usefulness of this behavior? (Or maybe it's just me - but I use Containable for just about everything).

Is there a different "CakePHP-way" to do translations fairly easily if I plan to use Containable a lot?

Was it helpful?

Solution

I've encountered similar problem, read dozens of pages from Google, but couldn't find a simple solution to my problem. After some debugging I've created this workaround snippet. Please take in consideration that this is jus a hack. It was mostly written for Croogo, so related models will appear translated on site. But i've browsed Translate behavior and it should work for it as well. Basically paste it in your AppModel Class. It's for 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;
    }

OTHER TIPS

Apparently this is a common issue. The CakePHP Cookbook has some hints about how to handle it:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top