* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBLog'); /** * Native Lanauge Support: Translator * * * Utilize Google API Cloud Translation * See https://cloud.google.com/translate/docs/basic/translate-text-basic * * @version 1.0.3 * @package WB * @subpackage nls */ class WBNLS_Translator extends WBStdClass { /** * Logger * @var WBLog */ protected $log; /** * @var WBRest_Client */ private $client; private $cLang = 'en_GB'; private $lang = 'de_DE'; private $domain = 'wombat'; /** * @var patI18n_Importer_Wombat */ private $imp; /** * @var WBDatasource_Table */ private $table; private $tableClause = array( array( 'field' => 'orphan', 'value' => 0 ) ); private $tableOpt = array( 'limit' => 10 ); /** * Translation direction * * Forward: translate form clang to lang * @var bool */ private $directionForward = true; /** * Construtor * * @param array configation parameters */ public function __construct($parameter = array()) { $this->log = WBLog::start(__CLASS__); /** @var WBRest_Client */ $this->client = WBClass::create('WBRest_Client'); $this->client->configure(array( 'server' => 'translation.googleapis.com', 'pathprefix' => 'language/translate/v2' )); $this->client->setHeader('Accept', 'application/json'); $this->client->setHeader('Content-Type', 'application/json'); WBClass::create('patI18n'); WBClass::load('patI18n_Importer'); $this->imp = WBClass::create('patI18n_Importer_Wombat'); $this->table = $this->imp->getTable(); } /** * Set Credentials * * Support for HTTP-Basic only. * Pass username and password or nothing to unset credentials * * @param array credentials data */ public function setCredentials($cred) { if (!empty($cred['apikey'])) { $this->client->setHeader('X-goog-api-key', $cred['apikey']); $this->client->setHeader('Authorization', '$(gcloud auth print-access-token)'); return; } // basic auth support // $this->requestHeader[] = 'Authorization: Basic ' . base64_encode($cred['user'] . ':' . $cred['password']); } public function setCLang($lang) { $this->cLang = $lang; } public function setLang($lang) { $this->lang = $lang; } public function setDomain($domain) { $this->domain = $domain; } /** * Translate messages that don't have a translation yet * * @param array list of ids * @return bool true on success */ public function translateIds($ids) { if (empty($ids)) { return false; } if (!is_array($ids)) { $ids = array($ids); } $clause = $this->tableClause; $clause[] = array( 'field' => $this->table->getIdentifier(patI18n_Module_Wombat::TABLE, true), 'relation' => 'in', 'value' => $ids ); $option = $this->tableOpt; $pager = $this->table->getPager('trans:ids:'. $this->lang, patI18n_Module_Wombat::TABLE, null, $clause, $option); return $this->translatePager($pager); } /** * Translate messages that don't have a translation yet * * @return bool true on success */ public function translateEmpty() { echo "translate empty\n"; $clause = $this->tableClause; $clause[] = array( 'field' => 'domain', 'value' => $this->domain ); $clause[] = array( 'field' => 'trans_' . $this->lang, 'relation' => 'null', 'value' => 1 ); $option = $this->tableOpt; $pager = $this->table->getPager('trans:empty:' . $this->domain . ':' . $this->lang, patI18n_Module_Wombat::TABLE, null, $clause, $option); return $this->translatePager($pager); } /** * Translate messages that don't have a translation yet * * @return bool true on success */ public function translateReverse() { $clause = $this->tableClause; $clause[] = array( 'field' => 'domain', 'value' => $this->domain ); $clause[] = array( 'field' => 'changed_' . $this->lang, 'relation' => 'eq', 'foreign' => 'nlsmsg', 'value' => 'changed', 'valuetype' => 'foreign' ); $option = $this->tableOpt; $this->directionForward = false; $pager = $this->table->getPager('trans:reverse:' . $this->domain . ':' . $this->lang, patI18n_Module_Wombat::TABLE, null, $clause, $option); $res = $this->translatePager($pager); $this->directionForward = true; return $res; } /** * Translate All Pages * * Walk through pager's pages and translate all lists * * @todo fix loop * @see translateList() * @param WBDatasource_Pager * @return bool true on succcess */ private function translatePager($pager) { $info = $pager->browse(); $info['next'] = 1; echo sprintf(" total %d, pages %d\n ", $info['total'], $info['pages']); while (0 < $info['next']) { if (!$this->translateList($pager->get())) { echo "e\n"; return false; } echo "."; $info = $pager->browse('__next'); } echo "\n"; return true; } /** * Translate List * * @see reverse * @param array message list from database * @return bool true on success */ private function translateList($list) { if (!is_array($list) || empty($list)) { return false; } $body = array( 'source' => substr($this->cLang, 0, 2), 'target' => substr($this->lang, 0, 2), 'format' => 'text', 'q' => array() ); $src = 'msg'; if (!$this->directionForward) { $src = 'trans_' . $this->lang; $body['source'] = substr($this->lang, 0, 2); $body['target'] = substr($this->cLang, 0, 2); } foreach ($list as $l) { $body['q'][] = $l[$src]; } if(!$this->client->post('/', $body)) { return false; } $res = $this->client->getRespone(); $trans = $res->getContent(); if (empty($trans['data']) || empty($trans['data']['translations'])) { return false; } $trans = $trans['data']['translations']; if (count($list) != count($trans)) { return false; } for ($i = 0; $i < count($list); ++$i) { if ($this->directionForward) { $save = array( 'changed_' . $this->lang => $list[$i]['changed'], 'trans_' . $this->lang => $trans[$i]['translatedText'] ); } else { $now = date('Y-m-d H:i:s'); $save = array( 'changed' => $now, 'changed_' . $this->lang => $now, 'msg' => $trans[$i]['translatedText'] ); } $this->table->save(patI18n_Module_Wombat::TABLE, $list[$i]['id'], $save); } return true; } }