Created
June 4, 2014 13:02
-
-
Save grandmanitou/12ff1e0d0167f79224fa to your computer and use it in GitHub Desktop.
CakePHP : shell task allowing to import your data in translatable model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class TranslateShell 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() { | |
| // Experience | |
| /* | |
| $this->_regenerateI18n('Experience', array('title'), 'fra'); | |
| $this->_regenerateI18n('Experience', array('subtitle'), 'fra'); | |
| $this->_regenerateI18n('Experience', array('description'), 'fra'); | |
| $this->_regenerateI18n('Experience', array('block_1'), 'fra'); | |
| $this->_regenerateI18n('Experience', array('block_2'), 'fra'); | |
| $this->_regenerateI18n('Experience', array('meeting_point'), 'fra'); | |
| $this->_regenerateI18n('Experience', array('location_description'), 'fra'); | |
| $this->_regenerateI18n('Experience', array('additional_info'), 'fra'); | |
| $this->_regenerateI18n('Experience', array('additional_info_after_order'), 'fra'); | |
| $this->_regenerateI18n('Experience', array('meta_keywords'), 'fra'); | |
| $this->_regenerateI18n('Experience', array('meta_description'), 'fra'); | |
| $this->_regenerateI18n('Experience', array('opengraph_title'), 'fra'); | |
| $this->_regenerateI18n('Experience', array('opengraph_description'), 'fra'); | |
| */ | |
| // Pass | |
| /* | |
| $this->_regenerateI18n('Pass', array('title'), 'fra'); | |
| $this->_regenerateI18n('Pass', array('subtitle'), 'fra'); | |
| $this->_regenerateI18n('Pass', array('meta_keywords'), 'fra'); | |
| $this->_regenerateI18n('Pass', array('meta_description'), 'fra'); | |
| $this->_regenerateI18n('Pass', array('opengraph_title'), 'fra'); | |
| $this->_regenerateI18n('Pass', array('opengraph_description'), 'fra'); | |
| */ | |
| // Hôtes | |
| /* | |
| $this->_regenerateI18n('Host', array('title'), 'fra'); | |
| $this->_regenerateI18n('Host', array('description'), 'fra'); | |
| $this->_regenerateI18n('Host', array('biography'), 'fra'); | |
| */ | |
| // Content | |
| /* | |
| $this->_regenerateI18n('Content', array('title'), 'fra'); | |
| $this->_regenerateI18n('Content', array('description'), 'fra'); | |
| $this->_regenerateI18n('Content', array('opengraph_title'), 'fra'); | |
| $this->_regenerateI18n('Content', array('opengraph_description'), 'fra'); | |
| $this->_regenerateI18n('Content', array('meta_keywords'), 'fra'); | |
| $this->_regenerateI18n('Content', array('meta_description'), 'fra'); | |
| */ | |
| // | |
| // Slider | |
| $this->_regenerateI18n('Slider', array('title'), 'fra'); | |
| $this->_regenerateI18n('Slider', array('description'), 'fra'); | |
| $this->_regenerateI18n('Slider', array('alt'), 'fra'); | |
| } | |
| /** | |
| * 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'); | |
| $this->I18nModel = ClassRegistry::init($this->{$Model}->translateModel); | |
| $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"); | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment