开发者

CakePHP - how to apply translate behavior to existing database?

I have and existing application in CakePHP with a database. The task is to apply translate behavior to its models. The problem is that i开发者_JAVA百科18n.php script just creates _i18n table but doesn't copy existing data to this table. Don't you know any script that could do that? Thanks for any help.


I extended the answers from Aziz and MarcoB and created a even more generic CakeShell out of it.

In the method _execute() simply set something like:

$this->_regenerateI18n('BlogPosts', array('title'), 'deu');

And all entries for the Model BlogPosts for the column title in the language deu will be create in the i18n table.

This is CakePHP 2.4 compatible!

<?php 

class SetuptranslationsShell extends AppShell {

    public function main() {
        $selection = $this->in('Start to create translated entries?', array('y', 'n', 'q'), 'y');
        if (strtolower($selection) === 'y') {
            $this->out('Creating entries in i18n table...');
            $this->_execute();
        }
    }

    function _execute() {
        $this->_regenerateI18n('BlogPosts', array('title'), 'deu');
        $this->_regenerateI18n('BlogTags',  array('name'),  'deu');
    }

    /**
     * See http://stackoverflow.com/q/2024407/22470
     * 
     */
    function _regenerateI18n($Model, $fields = array(), $targetLocale) {

        $this->out('Create entries for "'.$Model.'":');

        if (!isset($this->$Model)) {
            $this->{$Model} = ClassRegistry::init($Model);
        }

        $this->{$Model}->Behaviors->disable('Translate'); 
        $out = $this->{$Model}->find('all', array(
                                                    'recursive' => -1, 
                                                    'order'     => $this->{$Model}->primaryKey, 
                                                    'fields'    => array_merge(array($this->{$Model}->primaryKey), $fields))
                                                    );

        $this->I18nModel = ClassRegistry::init('I18nModel');

        $t = 0;
        foreach ($out as $v) {

            foreach ($fields as $field) {
                $data = array(
                    'locale'        => $targetLocale,
                    'model'         => $this->{$Model}->name,
                    'foreign_key'   => $v[$Model][$this->{$Model}->primaryKey],
                    'field'         => $field,
                    'content'       => $v[$Model][$field],
                );


                $check_data = $data;
                unset($check_data['content']);
                if (!$this->I18nModel->find('first', array('conditions' => $check_data))) {
                    if ($this->I18nModel->create($data) AND $this->I18nModel->save($data)) {
                        echo '.';
                        $t++;
                    }
                }
            }
        }
        $this->out($t." entries written");
    }
}


As far as I know, there's no way to do this. Moreover, because of the way the i18n table is configured to work, I think there's a better solution. A while back, I wrote a patch for the TranslateBehavior that will keep you from having to copy existing data into the i18n table (that felt insanely redundant to me and was a huge barrier to implementing i18n). If no record for that model exists in the i18n table, it will simply read the model record itself as a fallback.

Unfortunately, the Cake team appears to have moved everything to new systems, so I can no longer find either the ticket or the patch that I submitted. My patched copy of the TranslateBehavior is in my Codaset repository at http://codaset.com/robwilkerson/scratchpad/source/master/blob/cakephp/behaviors/translatable.php.

As you might expect, all of the usual warnings apply. The patched file was developed for 1.2.x and works for my needs, by YMMV.


try to use it

function regenerate()
{
    $this->Article->Behaviors->disable('Translate'); 
    $out = $this->Article->find('all', array('recursive'=>-1, 'order'=>'id'));
    $t = $b = 0;
    foreach($out as $v){
        $title['locale'] = 'aze';
        $title['model'] = 'Article';
        $title['foreign_key'] = $v['Article']['id'];
        $title['field'] = 'title';
        $title['content'] = $v['Article']['title'];
        if($this->Article->I18n->create($title) && $this->Article->I18n->save($title)){
            $t++;
        }
        $body['locale'] = 'aze';
        $body['model'] = 'Article';
        $body['foreign_key'] = $v['Article']['id'];
        $body['field'] = 'body';
        $body['content'] = $v['Article']['body'];
        if($this->Article->I18n->create($body) && $this->Article->I18n->save($body)){
            $b++;
        }
    }
}


Thanks Aziz. I modified your code to use it within the cakeshell
(CakePHP 2.3.8)

function execute() {
    $this->out('CORE_PATH: '. CORE_PATH. "\n");
    $this->out('CAKEPHP_SHELL: '. CAKEPHP_SHELL. "\n");

    $this->out('Migrate BlogPosts');
    $this->regenerateI18n('BlogPost', 'title', 'BlogPostI18n');
}
/**
 * @param string $Model 
 * @param string $Field
 * @param string $ModelI18n
 */
function regenerateI18n($Model = null, $Field = null, $ModelI18n = null)
{
    if(!isset($this->$Model))
        $this->$Model = ClassRegistry::init($Model);
    if(!isset($this->$ModelI18n))
        $this->$ModelI18n = ClassRegistry::init($ModelI18n);

    $this->$Model->Behaviors->disable('Translate'); 
    $out = $this->$Model->find('all', array('recursive'=>-1, 'order'=>'id'));
    $t = 0;
    foreach($out as $v){
        $data = array(
            'locale' => 'deu',
            'model' => $this->$Model->name,
            'foreign_key' => $v[$Model]['id'],
            'field' => $Field,
            'content' => $v[$Model][$Field],
        );
        if($this->$ModelI18n->create($data) && $this->$ModelI18n->save($data)){
            echo '.';
            $t++;
        }
    }
    $this->out($t." Entries written");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜