* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBLog'); /** * Native Lanauge Support: Translator * * * * @version 1.0.4 * @package WB * @subpackage nls */ class WBNLS_Translator extends WBStdClass { /** * Logger * @var WBLog */ protected $log; /** * @var WBNLS_Translator_Service */ 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 * Otherwise translate from lang to clang * @var bool */ private $directionForward = true; /** * Construtor * * @param array configation parameters */ public function __construct($parameter = array()) { $this->log = WBLog::start(__CLASS__); WBClass::create('patI18n'); WBClass::load('patI18n_Importer'); $this->imp = WBClass::create('patI18n_Importer_Wombat'); $this->table = $this->imp->getTable(); } public function startClient($name = '') { /** @var WBConfig */ $config = WBClass::create('WBConfig'); $config->load('locale/translator/translator'); if (empty($name)) { $name = $config->get('service/default', ''); } if (empty($name)) { $eData = array( 'msg' => 'Name to select client missing', 'code' => '1', 'class' => __CLASS__ ); throw WBClass::create('WBException_Config', $eData); return false; } $service = $config->get('service/available/' . $name, array()); if (empty($service)) { $eData = array( 'msg' => 'Client ' . $name . ' is not configured', 'code' => '2', 'class' => __CLASS__ ); throw WBClass::create('WBException_Config', $eData); return false; } $this->client = WBClass::create('WBNLS_Translator_Service_' . $service['name']); // default is empty parameter list if (empty($service['params']) || empty($service['params'])) { $service['params'] = array(); } $this->client->configure($service['params']); } /** * Set C-Language * * @param string */ public function setCLang($lang) { $this->cLang = $lang; } /** * Set Target Language * * @param string */ public function setLang($lang) { $this->lang = $lang; } /** * Set Text Domain * * @param string */ public function setDomain($domain) { $this->domain = $domain; } /** * Translate By Id * * Translate messages selected by id(s) * * @param array list of ids * @param bool forward true is default meaning translation from clang to lang * @return bool true on success */ public function translateIds($ids, $forward = true) { 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 ); $this->directionForward = $forward; $option = $this->tableOpt; $pager = $this->table->getPager('trans:ids:'. $this->lang, patI18n_Module_Wombat::TABLE, null, $clause, $option); return $this->translatePager($pager); } /** * First Translation * * Translate messages that don't have a translation yet * * @return bool true on success */ public function translateEmpty() { $this->directionForward = true; $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); } /** * Reverse Translation * * Translate message from foreign to origin language * * @return bool true on success */ public function translateReverse() { $this->directionForward = false; $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; $pager = $this->table->getPager('trans:reverse:' . $this->domain . ':' . $this->lang, patI18n_Module_Wombat::TABLE, null, $clause, $option); $res = $this->translatePager($pager); return $res; } /** * Translate All Pages * * Walk through pager's pages and translate all lists * * @see translateList() * @param WBDatasource_Pager * @return bool true on succcess */ private function translatePager($pager) { $info = $pager->browse(); if (1 > $info['total']) { return true; } $info['next'] = 1; while (0 < $info['next']) { if (!$this->translateList($pager->get())) { return false; } $info = $pager->browse('__next'); } return true; } /** * Translate List * * @see directionForward * @param array message list from database * @return bool true on success */ private function translateList($list) { if (!is_array($list) || empty($list)) { return false; } $key = 'msg'; $this->client->setSourceLang($this->cLang); $this->client->setTargetLang($this->lang); if (!$this->directionForward) { $key = 'trans_' . $this->lang; $this->client->setSourceLang($this->lang); $this->client->setTargetLang($this->cLang); } if (!$this->client->translateList($list, $key)) { return false; } foreach ($list as $l) { if ($this->directionForward) { $save = array( 'changed_' . $this->lang => $l['changed'], 'trans_' . $this->lang => $l['msg_trans'] ); } else { $now = date('Y-m-d H:i:s'); $save = array( 'changed' => $now, 'changed_' . $this->lang => $now, 'msg' => $l['trans_' . $this->lang . '_trans'] ); } $this->table->save(patI18n_Module_Wombat::TABLE, $l['id'], $save); } return true; } }