0

我已经按照手册中的说明使用 CakePHP 2.1 设置翻译行为,以及Stack 上的这个问题。我没有收到任何错误,但我翻译的帖子没有保存到我的 i18n 表中。

这是我的 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'
        )
    );

}

这是我在 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));
        }
    }
}

我使用控制台初始化了 i18n 表。我应该放下桌子并尝试重新初始化吗?不知道为什么会有问题。

4

4 回答 4

2

更可重用的解决方案是添加到您的 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;
    }
}

此代码自动将返回 TranslateBehavior 的内容转换为能够创建多语言表单,例如:

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

在 CakePHP 2.3 上测试。我发现 nowModel->saveAssociated()是必需的,而不是Model->save().

于 2013-01-31T17:39:24.133 回答
0

使用 saveMany instedof 保存。它对我来说很好。

于 2014-02-01T10:09:53.653 回答
0

尝试在 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'));
?>

在您的 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'));
?>

假设 data['titleTranslation'][0] 的索引是法语,为了在视图中轻松查看数组,我建议使用 DebugKit https://github.com/cakephp/debug_kit

我希望这个能帮上忙

于 2012-04-17T21:51:58.097 回答
0

对于那些有同样问题的人:正确的变量是$actsAs,不是$actAs

于 2014-03-21T13:55:08.837 回答