1af1904f9SAndreas Gohr<?php 2af1904f9SAndreas Gohr/** 3af1904f9SAndreas Gohr * Translation Plugin: Simple multilanguage plugin 4af1904f9SAndreas Gohr * 5af1904f9SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6af1904f9SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 7af1904f9SAndreas Gohr */ 8af1904f9SAndreas Gohr 9af1904f9SAndreas Gohr// must be run within Dokuwiki 10af1904f9SAndreas Gohrif(!defined('DOKU_INC')) die(); 11af1904f9SAndreas Gohr 12af1904f9SAndreas Gohr 13af1904f9SAndreas Gohrclass helper_plugin_translation extends DokuWiki_Plugin { 14af1904f9SAndreas Gohr var $trans = array(); 15af1904f9SAndreas Gohr var $tns = ''; 16af1904f9SAndreas Gohr 17af1904f9SAndreas Gohr /** 18af1904f9SAndreas Gohr * Initialize 19af1904f9SAndreas Gohr */ 20af1904f9SAndreas Gohr function helper_plugin_translation(){ 21af1904f9SAndreas Gohr require_once(DOKU_INC.'inc/pageutils.php'); 22af1904f9SAndreas Gohr require_once(DOKU_INC.'inc/utf8.php'); 23af1904f9SAndreas Gohr 24af1904f9SAndreas Gohr // load wanted translation into array 25af1904f9SAndreas Gohr $this->trans = strtolower(str_replace(',',' ',$this->getConf('translations'))); 26af1904f9SAndreas Gohr $this->trans = array_unique(array_filter(explode(' ',$this->trans))); 27af1904f9SAndreas Gohr sort($this->trans); 28af1904f9SAndreas Gohr array_unshift($this->trans,''); 29af1904f9SAndreas Gohr 30af1904f9SAndreas Gohr $this->tns = cleanID($this->getConf('translationns')); 31af1904f9SAndreas Gohr if($this->tns) $this->tns .= ':'; 32af1904f9SAndreas Gohr } 33af1904f9SAndreas Gohr 34af1904f9SAndreas Gohr /** 35af1904f9SAndreas Gohr * return some info 36af1904f9SAndreas Gohr */ 37af1904f9SAndreas Gohr function getInfo(){ 38af1904f9SAndreas Gohr return confToHash(dirname(__FILE__).'/info.txt'); 39af1904f9SAndreas Gohr } 40af1904f9SAndreas Gohr 41af1904f9SAndreas Gohr /** 42af1904f9SAndreas Gohr * Check if the given ID is a translation and return the language code. 43af1904f9SAndreas Gohr */ 44af1904f9SAndreas Gohr function getLangPart($id){ 45af1904f9SAndreas Gohr $rx = '/^'.$this->tns.'('.join('|',$this->trans).'):/'; 46af1904f9SAndreas Gohr if(preg_match($rx,$id,$match)){ 47af1904f9SAndreas Gohr return $match[1]; 48af1904f9SAndreas Gohr } 49af1904f9SAndreas Gohr return ''; 50af1904f9SAndreas Gohr } 51af1904f9SAndreas Gohr 52af1904f9SAndreas Gohr /** 537053cd66SAndreas Gohr * Returns the browser language if it matches with one of the configured 547053cd66SAndreas Gohr * languages 557053cd66SAndreas Gohr */ 567053cd66SAndreas Gohr function getBrowserLang(){ 577053cd66SAndreas Gohr $rx = '/(^|,|:|;|-)('.join('|',$this->trans).')($|,|:|;|-)/i'; 587053cd66SAndreas Gohr if(preg_match($rx,$_SERVER['HTTP_ACCEPT_LANGUAGE'],$match)){ 597053cd66SAndreas Gohr return strtolower($match[2]); 607053cd66SAndreas Gohr } 617053cd66SAndreas Gohr return false; 627053cd66SAndreas Gohr } 637053cd66SAndreas Gohr 647053cd66SAndreas Gohr 657053cd66SAndreas Gohr /** 66af1904f9SAndreas Gohr * Returns the ID and name to the wanted translation, empty $lng is default lang 67af1904f9SAndreas Gohr */ 68af1904f9SAndreas Gohr function buildTransID($lng,$idpart){ 69af1904f9SAndreas Gohr global $conf; 70af1904f9SAndreas Gohr global $saved_conf; 71af1904f9SAndreas Gohr if($lng){ 72af1904f9SAndreas Gohr $link = ':'.$this->tns.$lng.':'.$idpart; 73af1904f9SAndreas Gohr $name = $lng; 74af1904f9SAndreas Gohr }else{ 75af1904f9SAndreas Gohr $link = ':'.$this->tns.$idpart; 76af1904f9SAndreas Gohr if(!$conf['lang_before_translation']){ 77af1904f9SAndreas Gohr $name = $conf['lang']; 78af1904f9SAndreas Gohr } else { 79af1904f9SAndreas Gohr $name = $conf['lang_before_translation']; 80af1904f9SAndreas Gohr } 81af1904f9SAndreas Gohr } 82af1904f9SAndreas Gohr return array($link,$name); 83af1904f9SAndreas Gohr } 84af1904f9SAndreas Gohr 85*1469199dSAndreas Gohr /** 86*1469199dSAndreas Gohr * Displays the available and configured translations. Needs to be placed in the template. 87*1469199dSAndreas Gohr */ 88*1469199dSAndreas Gohr function showTranslations(){ 89*1469199dSAndreas Gohr global $ACT; 90*1469199dSAndreas Gohr global $ID; 91*1469199dSAndreas Gohr global $conf; 92*1469199dSAndreas Gohr global $INFO; 93*1469199dSAndreas Gohr 94*1469199dSAndreas Gohr if($ACT != 'show') return; 95*1469199dSAndreas Gohr if($this->tns && strpos($ID,$this->tns) !== 0) return; 96*1469199dSAndreas Gohr $skiptrans = trim($this->getConf('skiptrans')); 97*1469199dSAndreas Gohr if($skiptrans && preg_match('/'.$skiptrans.'/ui',':'.$ID)) return; 98*1469199dSAndreas Gohr $meta = p_get_metadata($ID); 99*1469199dSAndreas Gohr if($meta['plugin']['translation']['notrans']) return; 100*1469199dSAndreas Gohr 101*1469199dSAndreas Gohr $rx = '/^'.$this->tns.'(('.join('|',$this->trans).'):)?/'; 102*1469199dSAndreas Gohr $idpart = preg_replace($rx,'',$ID); 103*1469199dSAndreas Gohr 104*1469199dSAndreas Gohr $out = '<div class="plugin_translation">'; 105*1469199dSAndreas Gohr $out .= '<span>'.$this->getLang('translations'); 106*1469199dSAndreas Gohr if($this->getConf('about')){ 107*1469199dSAndreas Gohr $out .= '<sup>'.html_wikilink($this->getConf('about'),'?').'</sup>'; 108*1469199dSAndreas Gohr } 109*1469199dSAndreas Gohr $out .= ':</span> '; 110*1469199dSAndreas Gohr 111*1469199dSAndreas Gohr if($this->getConf('dropdown')){ // use dropdown 112*1469199dSAndreas Gohr if($INFO['exists']){ 113*1469199dSAndreas Gohr $class = 'wikilink1'; 114*1469199dSAndreas Gohr }else{ 115*1469199dSAndreas Gohr $class = 'wikilink2'; 116*1469199dSAndreas Gohr } 117*1469199dSAndreas Gohr $out .= '<form action="'.wl().'" id="translation__dropdown">'; 118*1469199dSAndreas Gohr $out .= '<select name="id" class="'.$class.'">'; 119*1469199dSAndreas Gohr foreach($this->trans as $t){ 120*1469199dSAndreas Gohr list($link,$name) = $this->buildTransID($t,$idpart); 121*1469199dSAndreas Gohr $link = cleanID($link); 122*1469199dSAndreas Gohr if($ID == $link){ 123*1469199dSAndreas Gohr $sel = ' selected="selected"'; 124*1469199dSAndreas Gohr }else{ 125*1469199dSAndreas Gohr $sel = ''; 126*1469199dSAndreas Gohr } 127*1469199dSAndreas Gohr if(page_exists($link,'',false)){ 128*1469199dSAndreas Gohr $class = 'wikilink1'; 129*1469199dSAndreas Gohr }else{ 130*1469199dSAndreas Gohr $class = 'wikilink2'; 131*1469199dSAndreas Gohr } 132*1469199dSAndreas Gohr $out .= '<option value="'.$link.'"'.$sel.' class="'.$class.'">'.hsc($name).'</option>'; 133*1469199dSAndreas Gohr } 134*1469199dSAndreas Gohr $out .= '</select>'; 135*1469199dSAndreas Gohr $out .= '<input name="go" type="submit" value="→" />'; 136*1469199dSAndreas Gohr $out .= '</form>'; 137*1469199dSAndreas Gohr }else{ // use list 138*1469199dSAndreas Gohr $out .= '<ul>'; 139*1469199dSAndreas Gohr foreach($this->trans as $t){ 140*1469199dSAndreas Gohr list($link,$name) = $this->buildTransID($t,$idpart); 141*1469199dSAndreas Gohr $out .= ' <li><div class="li">'.html_wikilink($link,$name).'</div></li>'; 142*1469199dSAndreas Gohr } 143*1469199dSAndreas Gohr $out .= '</ul>'; 144*1469199dSAndreas Gohr } 145*1469199dSAndreas Gohr 146*1469199dSAndreas Gohr $out .= '</div>'; 147*1469199dSAndreas Gohr 148*1469199dSAndreas Gohr 149*1469199dSAndreas Gohr return $out; 150*1469199dSAndreas Gohr } 151af1904f9SAndreas Gohr 152af1904f9SAndreas Gohr 153af1904f9SAndreas Gohr} 154