我已经和在CakePHP中与数据库现有的应用程序。 任务是应用转化行为的模型。问题是,i18n.php脚本只是创建_i18n表,但不存在的数据复制到该表。 难道你不知道任何脚本,能做到这一点? 感谢您的帮助。

有帮助吗?

解决方案

据我所知,有没有办法做到这一点。此外,由于国际化表被配置在上班的路上,我想有一个更好的解决方案。前阵子,我写了TranslateBehavior一个补丁,将让您不必到现有的数据复制到国际化表(即感到疯狂多余的我,是一个巨大的障碍,以实现国际化)。如果该模型没有记录在I18N表存在,它将简单地读取模型记录本身作为后备。

不幸的是,蛋糕队似乎已经将所有资料移至新系统,所以我再也找不到任何车票或我提交的补丁。我修补的TranslateBehavior的副本是我Codaset仓库处的 http://codaset.com/robwilkerson/scratchpad/source/master/blob/cakephp/behaviors/translatable.php

正如你所预料的,所有常用的警告适用。该补丁的文件是为1.2.x的开发,适用于我的需求,通过因人而异的。

其他提示

我扩展从Aziz和MarcoB答案并创建了一个更通用的CakeShell出来。

在该方法_execute()只需设置是这样的:

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

和对模型的相关博客文章在语言申列标题将在国际化表中创建的所有条目。

这是CakePHP的2.4兼容!

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

尝试使用它

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

感谢阿兹。我修改你的代码到cakeshell点击内使用 (2.3.8 CakePHP的)

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");
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top