*/ // must be run within Dokuwiki if(!defined('DOKU_INC')) die(); class helper_plugin_translation extends DokuWiki_Plugin { var $trans = array(); var $tns = ''; var $defaultlang = ''; var $LN = array(); // hold native names var $opts = array(); // display options /** * Initialize */ function helper_plugin_translation(){ global $conf; require_once(DOKU_INC.'inc/pageutils.php'); require_once(DOKU_INC.'inc/utf8.php'); // load wanted translation into array $this->trans = strtolower(str_replace(',',' ',$this->getConf('translations'))); $this->trans = array_unique(array_filter(explode(' ',$this->trans))); sort($this->trans); // load language names $this->LN = confToHash(dirname(__FILE__).'/lang/langnames.txt'); // display options $this->opts = $this->getConf('display'); $this->opts = explode(',',$this->opts); $this->opts = array_map('trim',$this->opts); $this->opts = array_fill_keys($this->opts, true); // get default translation if(!$conf['lang_before_translation']){ $dfl = $conf['lang']; } else { $dfl = $conf['lang_before_translation']; } if(in_array($dfl,$this->trans)){ $this->defaultlang = $dfl; }else{ $this->defaultlang = ''; array_unshift($this->trans,''); } $this->tns = cleanID($this->getConf('translationns')); if($this->tns) $this->tns .= ':'; } /** * Check if the given ID is a translation and return the language code. */ function getLangPart($id){ list($lng) = $this->getTransParts($id); return $lng; } /** * Check if the given ID is a translation and return the language code and * the id part. */ function getTransParts($id){ $rx = '/^'.$this->tns.'('.join('|',$this->trans).'):(.*)/'; if(preg_match($rx,$id,$match)){ return array($match[1],$match[2]); } return array('',$id); } /** * Returns the browser language if it matches with one of the configured * languages */ function getBrowserLang(){ $rx = '/(^|,|:|;|-)('.join('|',$this->trans).')($|,|:|;|-)/i'; if(preg_match($rx,$_SERVER['HTTP_ACCEPT_LANGUAGE'],$match)){ return strtolower($match[2]); } return false; } /** * Returns the ID and name to the wanted translation, empty * $lng is default lang */ function buildTransID($lng,$idpart){ global $conf; if($lng){ $link = ':'.$this->tns.$lng.':'.$idpart; $name = $lng; }else{ $link = ':'.$this->tns.$idpart; $name = $this->realLC(''); } return array($link,$name); } /** * Returns the real language code, even when an empty one is given * (eg. resolves th default language) */ function realLC($lc){ global $conf; if($lc){ return $lc; }elseif(!$conf['lang_before_translation']){ return $conf['lang']; } else { return $conf['lang_before_translation']; } } /** * Check if current ID should be translated and any GUI * should be shown */ function istranslatable($id,$checkact=true){ global $ACT; if($checkact && $ACT != 'show') return false; if($this->tns && strpos($id,$this->tns) !== 0) return false; $skiptrans = trim($this->getConf('skiptrans')); if($skiptrans && preg_match('/'.$skiptrans.'/ui',':'.$id)) return false; $meta = p_get_metadata($id); if($meta['plugin']['translation']['notrans']) return false; return true; } /** * Return the (localized) about link */ function showAbout() { global $ID; global $conf; global $INFO; $curlc = getLangPart($ID); $about = $this->getConf('about'); if($this->getConf('localabout')){ list($lc,$idpart) = $this->getTransParts($about); list($about,$name) = $this->buildTransID($curlc,$idpart); $about = cleanID($about); } $out = ''; $out .= ''; $out .= html_wikilink($about,'?'); $out .= ''; return $out; } /** * Displays the available and configured translations. Needs to be placed in the template. */ function showTranslations(){ global $ID; global $conf; global $INFO; if(!$this->istranslatable($ID)) return; $this->checkage(); list($lc,$idpart) = $this->getTransParts($ID); $lang = $this->realLC($lc); $out = '