array( 'from' => 'ru', 'to' => 'en' ), 'yandex' => array( 'key' => '__YANDEX_KEY___', 'from' => 'ru', 'to' => 'en' ), 'extensions' => array( 'php' ), 'permissions' => array( 'file' => 0644, 'folder' => 0755 ) ); $translator = new CTranslator($params); class CTranslator { /** * @var array */ protected $params = array(); /** * @var array */ protected $errors = array(); /** * @var array, tmp lang files of given lang folder */ protected $langFiles = array(); /** * @var array all added lang files */ protected $addedFiles = array(); /** * @var array all files which must be checked */ protected $checkFiles = array(); /** * Return yandex answer and http response code * * @param string $string * @return array */ public function yandexRequest($string) { $curl = \curl_init('https://translate.yandex.net/api/v1.5/tr.json/translate'); $requestData = array(); $requestData['key'] = $this->params['yandex']['key']; $requestData['text'] = $string; $requestData['lang'] = $this->params['yandex']['from'] . '-' . $this->params['yandex']['to']; $requestData['format'] = 'html'; \curl_setopt($curl, CURLOPT_POST, true); \curl_setopt($curl, CURLOPT_POSTFIELDS, $requestData); \curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = array(); $result['answer'] = \curl_exec($curl); if (empty($result['answer'])) { $result['error'] = \curl_error($curl) . '(' . \curl_errno($curl) . ')'; } $result['status'] = \curl_getinfo($curl, CURLINFO_HTTP_CODE); \curl_close($curl); return $result; } /** * Translate given string * * @param string $string * @return string */ protected function translateString($string) { $translatedString = ''; if (strlen($string) > 0) { $result = $this->yandexRequest($string); if($result['status'] == 200){ $result['answer'] = json_decode($result['answer']); if($result['answer']->code == 200){ $translatedString = $result['answer']->text[0]; } else { $this->errors[] = 'Bad yandex answer'; } } else { $this->errors[] = 'Bad yandex request'; } } return $translatedString; } /** * Translate all lang file messages and return translated result as string * * @param $filePath * @param $savePath * @return string */ protected function translateFile($filePath, $savePath) { $translatedFileContent = " $value) { $translatedFileContent .= '$MESS["' . $key . '"] = "'; $translatedFileContent .= addslashes($this->translateString($value)); $translatedFileContent .= '"; //'; //you can add your own check here if(strpos($value, '#') !== false){ $needToCheck = true; $translatedFileContent .= ' !!!SYMBOL #'; } if(strpos($value, '<') !== false){ $needToCheck = true; $translatedFileContent .= ' !!!HTML'; } $translatedFileContent .= ' | !AUTO! '. $value . "\r\n"; } } } if($needToCheck){ $this->checkFiles[] = $filePath; echo '
'; print_r($this->errors); echo ''; die('interrupted. errors'); } else { $startDir = getcwd(); $dirs = $this->getSubDirs($startDir); foreach ($dirs as $key => $dir) { $info = pathinfo($dir); if ($info['basename'] == 'lang') { $this->translateFolder($dir); unset($dirs[$key]); } } while ($dirs) { if (!empty($this->errors)) { echo '
'; print_r($this->errors); echo ''; die('interrupted. errors'); } $subDirs = $this->getSubDirs(reset($dirs)); foreach ($subDirs as $key => $dir) { $info = pathinfo($dir); if ($info['basename'] == 'lang') { $this->translateFolder($dir); unset($subDirs[$key]); } } $dirs = array_merge($dirs, $subDirs); unset($dirs[0]); echo '
'; print_r($dirs); echo '
'; print_r($this->checkFiles); echo ''; echo '
'; print_r($this->addedFiles); echo ''; } } }