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