0

I've followed the instructions in the manual for setting up Translate Behavior with CakePHP 2.1, as well as this question here on Stack. I am not getting any errors, but my translated posts are not saving to my i18n table.

Here is my PostModel.php:

class Post extends AppModel {

    public $title = 'Post';
    public $name = 'Post';
    public $body = 'Post';

    public $actAs = array(
        'Translate' => array(
            'title' => 'titleTranslation', 
            'body' => 'bodyTranslation'
        )
    );

    public $validate = array(
        'title' => array(
            'rule' => 'notEmpty'
        ),
        'body' => array(
            'rule' => 'notEmpty'
        )
    );

}

And here are my add and edit functions in PostsController.php:

public function add() {
        if ($this->request->is('post')) {
            $this->Post->locale = 'fre';
            $this->Post->create();

        if ($this->Post->save($this->request->data)) {
            $this->Session->setFlash(__('Your post has been saved.', true));
            $this->redirect(array('action' => 'admin'));
        } else {
            $this->Session->setFlash(__('Unable to add your post.', true));
        }
    }
}

public function edit($id = null) {
    $this->Post->id = $id;
    if ($this->request->is('get')) 
    {
        $this->Post->locale = 'fre';
        $this->request->data = $this->Post->read();
    } 
    else 
    {
        if ($this->Post->save($this->request->data)) 
        {
            $this->Post->locale = 'fre';
            $this->Session->setFlash(__('Your post has been updated.', true));
            $this->redirect(array('action' => 'admin'));
        } 
        else 
        {
            $this->Session->setFlash(__('Unable to update your post.', true));
        }
    }
}

I initialized the i18n table using Console. Should I drop the table and try reinitializing? Not sure why there would be a problem there.

Community
  • 1
  • 1
deewilcox
  • 852
  • 2
  • 12
  • 24

4 Answers4

2

More reusable solution is to add to your AppModel:

class AppModel extends Model {
    public $actsAs = array('Containable');

    /**
     * Converts structure of translated content by TranslateBehavior to be compatible
     * when saving model
     *
     * @link http://rafal-filipek.blogspot.com/2009/01/translatebehavior-i-formularze-w.html
     */
    public function afterFind($results, $primary = false) {
        if (isset($this->Behaviors->Translate)) {
            foreach ($this->Behaviors->Translate->settings[$this->alias] as $value) {
                foreach ($results as $index => $row) {
                    if (array_key_exists($value, $row)) {
                        foreach($row[$value] as $locale) {
                            if (isset($results[$index][$this->alias][$locale['field']])) {
                                if (!is_array($results[$index][$this->alias][$locale['field']])) {
                                    $results[$index][$this->alias][$locale['field']] = array();
                                }
                                $results[$index][$this->alias][$locale['field']][$locale['locale']] = $locale['content'];
                            }
                        }
                    }
                }
            }
        }
        return $results;
    }
}

This code automatically converts what returns TranslateBehavior to be able to create multi-language form like:

echo $this->Form->input('Category.name.eng');
echo $this->Form->input('Category.name.deu');
echo $this->Form->input('Category.name.pol');

Tested on CakePHP 2.3. And I discovered that now Model->saveAssociated() is required instead of Model->save().

redd
  • 260
  • 2
  • 8
  • Thanks, redd. I haven't worked on this project recently, but I will test out your answer and see if it works. – deewilcox Feb 14 '13 at 18:11
0

Try to add this in your form on add.ctp:

<?php
     echo $this->Form->create('Post');
     echo $this->Form->input('Post.title.fre');
     echo $this->Form->input('Post.body.fre');
     //Something more...
     echo $this->Form->end(__('Submit'));
?>

in your edit.ctp:

<?php
     echo $this->Form->create('Post');
     echo $this->Form->input('id');
     echo $this->Form->input('Post.title.fre', array('value'=>$this->request->data['titleTranslation'][0]['content'));
     echo $this->Form->input('Post.body.fre', array('value'=>$this->request->data['bodyTranslation'][0]['content'));
     //Something more...
     echo $this->Form->end(__('Submit'));
?>

assuming index of data['titleTranslation'][0] is in French, to easily see an array in the view I recommend to use DebugKit https://github.com/cakephp/debug_kit

I hope this will help

CROWD
  • 170
  • 2
  • 8
0

Use saveMany instedof save. it's working fine for me.

Piyush Mittal
  • 159
  • 2
  • 14
0

For those who have the same problem: the correct variable is $actsAs, not $actAs.

bancer
  • 7,475
  • 7
  • 39
  • 58