0

CakePHP 2.1 で Translate Behavior を設定するためのマニュアルの指示と、この質問 here on 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 の add 関数と edit 関数は次のとおりです。

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));
        }
    }
}

Console を使用して 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 でテスト済み。Model->saveAssociated()そして、 の代わりに now が必要であることを発見しましたModel->save()

于 2013-01-31T17:39:24.133 に答える
0

save の代わりに saveMany を使用します。それは私にとってはうまくいっています。

于 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 に答える