public function onlineTranslate($lang = array('ru', 'en'), $text) { $t = new yandexTranslate(); $response = $t->translate(array($lang[0], $lang[1]), $text); header('Content-Type: application/json'); echo CJSON::encode($response['text']); }
protected function validateAttribute($object, $attribute) { $config = Yii::app()->settings->get('core'); if (isset($object->translateAttributes)) { if (in_array($this->translitAttribute, $object->translateAttributes)) { $p = array(); foreach ($object->translateAttributes as $param) { $p[$param] = $param; } $attr = $p[$this->translitAttribute]; } } else { $attr = $this->translitAttribute; if (!$object->hasAttribute($attr)) { throw new CException(Yii::t('yii', 'Active record "{class}" is trying to select an invalid column "{column}". Note, the column must exist in the table or be an expression with alias.', array('{class}' => get_class($object), '{column}' => $attr))); } } if ($config['translate_object_url']) { Yii::import('mod.core.components.yandexTranslate'); $t = new yandexTranslate(); $tr = $t->translitUrl(array('ru', 'en'), $object->getAttribute($attr)); //die($tr); } else { $tr = CMS::translit($object->getAttribute($attribute)); } if ($object->isNewRecord) { $model = $object::model()->find(array('condition' => '`t`.`' . $attribute . '`=:alias', 'params' => array(':alias' => $tr))); } else { $model = $object::model()->find(array('condition' => '`t`.`' . $attribute . '`=:alias AND `t`.`id`!=' . $object->id, 'params' => array(':alias' => $tr))); } if (isset($model)) { $this->addError($object, $attribute, Yii::t('app', 'ERROR_DUPLICATE_URL', array('{url}' => $tr))); } else { $object->{$attribute} = $tr; } }
public function createFileLanguage($fullpath, $filename, $langs = array(), $content = array()) { $newLangPath = $fullpath . DS . $langs[1]; if (!is_dir($newLangPath)) { mkdir($newLangPath, 0750); } $t = new yandexTranslate(); $fh = fopen($newLangPath . DS . $filename, "w"); if (!is_resource($fh)) { return false; } fclose($fh); $params = array(); $result = array(); $spec = array(); $num = -1; foreach ($content as $key => $val) { $params[] = $val; $num++; $spec[$num] = $key; } $response = $t->translate($langs, $params); foreach ($response['text'] as $k => $v) { $result[$spec[$k]] = $v; } if (!@file_put_contents($newLangPath . DS . $filename, '<?php /** * Message translations. (auto translate) * * Each array element represents the translation (value) of a message (key). * If the value is empty, the message is considered as not translated. * Messages that no longer need translation will have their translations * enclosed between a pair of \'@@\' marks. * * @author Andrew (Panix) Semenov <*****@*****.**> * @package modules.messages.' . $langs[1] . ' */ return ' . var_export($result, true) . ';')) { throw new CException(Yii::t('admin', 'Error write modules setting in {file}...', array('{file}' => $filename))); } // return true; }
<div class="grid5"><?php echo Html::dropDownList('from', 'ru', yandexTranslate::onlineLangs(), array('empty' => Yii::t('app', 'EMPTY_DROPDOWNLIST', 1))); ?> <br/> Введите текст: <br /> <?php echo Html::textArea('text', null, array('class' => 'noresize')); ?> </div> <div class="grid2 textC"><?php echo Html::button('Перевести >>', array('id' => 'submit', 'class' => 'buttonS bGreen', 'style' => 'margin-top:80px')); ?> </div> <div class="grid5"><?php echo Html::dropDownList('to', 'en', yandexTranslate::onlineLangs(), array('empty' => Yii::t('app', 'EMPTY_DROPDOWNLIST', 1))); ?> <br/> Результат: <br /> <?php echo Html::textArea('result', null, array('class' => 'noresize')); ?> </div> <div class="clear"></div> </div> <?php Yii::app()->tpl->closeWidget(); ?>
<a href="javascript:options()" class="buttonS bDefault">Дополнительные параметры</a></div> <div class="clear"></div> </div> <div id="options" class="hidden"> <div class="divider"><span></span></div> <div class="formRow noBorderT"><?php echo Yii::app()->tpl->alert('warning', 'Важно! Если файл выбранного перевода уже существует он будет перезаписан.', false); ?> </div> <div class="formRow noBorderB"> <div class="grid3"><label for="lang">Перевести эти переводы на язык:</label></div> <div class="grid9"> <?php echo Html::dropDownList('lang', null, yandexTranslate::onlineLangs(), array('empty' => Yii::t('core', 'EMPTY_DROPDOWNLIST', 1))); ?> <div class="hint">Данный файл будет переведен с помощью Yandex translate API. После выбора перевода, нажмите сохранить</div> </div> <div class="clear"></div> </div> </div> </div> <?php $this->endWidget(); ?> <script> function options(){ $('#options').toggleClass('hidden');