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 * @author Guy Brand <gb@isis.u-strasbg.fr> 8 */ 9 10// must be run within Dokuwiki 11if(!defined('DOKU_INC')) die(); 12 13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 14require_once(DOKU_PLUGIN.'action.php'); 15 16class action_plugin_translation extends DokuWiki_Action_Plugin { 17 18 /** 19 * for th helper plugin 20 */ 21 var $hlp = null; 22 23 /** 24 * Constructor. Load helper plugin 25 */ 26 function action_plugin_translation(){ 27 $this->hlp =& plugin_load('helper', 'translation'); 28 } 29 30 /** 31 * Registe the events 32 */ 33 function register(&$controller) { 34 // should the lang be applied to UI? 35 if($this->getConf('translateui')){ 36 $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'translation_hook'); 37 } 38 if($this->getConf('redirectstart')){ 39 $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'translation_redirect_start'); 40 } 41 if($this->getConf('redirectlocalized')){ 42 $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'translation_redirect_localized'); 43 } 44 $controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'AFTER', $this, 'translation_search'); 45 } 46 47 function translation_redirect_start(&$event, $args) { 48 global $ID; 49 50 // redirect away from start page? 51 if( $ID == $conf['start'] && $ACT == 'show' ){ 52 $lc = $this->hlp->getBrowserLang(); 53 if(!$lc) $lc = $conf['lang']; 54 header('Location: '.wl($lc.':'.$conf['start'],'',true,'&')); 55 exit; 56 } 57 } 58 59 function translation_redirect_localized(&$event, $args) { 60 global $ID; 61 global $conf; 62 global $ACT; 63 64 // redirect to localized page? 65 if( $ACT != 'show' ) { return; } 66 67 $override = (!empty($_REQUEST['s']) && $_REQUEST['s'] == 'translation_true'); // override enabled - comes from the bottom bar. 68 $lang = $conf['lang_before_translation'] ? $conf['lang_before_translation'] : $conf['lang']; // Check for original language 69 70 // get current page language - if empty then default; 71 $currentSessionLanguage = $_SESSION[DOKU_COOKIE]['translationcur']; 72 $pageLang = $this->hlp->getLangPart($ID); 73 74 if ( empty($pageLang) ) { 75 $pageLang = $lang; 76 } 77 78 // If both match, we're fine. 79 if ( $currentSessionLanguage == $pageLang ) { 80 return; 81 } 82 83 // check current translation 84 if ( empty( $currentSessionLanguage ) && !$override ) { 85 86 // If not set - we must just have entered - set the browser language 87 $currentSessionLanguage = $this->hlp->getBrowserLang(); 88 89 // if no browser Language set, take entered namespace language - empty for default. 90 if ( !$currentSessionLanguage ) { 91 $currentSessionLanguage = $pageLang; 92 } 93 94 // Set new Language 95 $_SESSION[DOKU_COOKIE]['translationcur'] = $currentSessionLanguage; 96 97 // Write Language back 98 $pageLang = $currentSessionLanguage; 99 } 100 101 102 if ( $override && $pageLang != $currentSessionLanguage ) { 103 // Set new Language 104 $currentSessionLanguage = $pageLang; 105 $_SESSION[DOKU_COOKIE]['translationcur'] = $currentSessionLanguage; 106 } else if ( !$override ) { 107 // Write Language back 108 $pageLang = $currentSessionLanguage; 109 } 110 111 // If this is the default language, make empty 112 if ( $pageLang == $lang ) { 113 $pageLang = ''; 114 } 115 116 // Generate new Page ID 117 $rx = '/^'.$this->hlp->tns.'('.join('|',$this->hlp->trans).'):/'; 118 $idpart = preg_replace($rx,'',$ID); 119 list($newPage,$name) = $this->hlp->buildTransID($pageLang,$idpart); 120 $newPage = cleanID($newPage); 121 122 // Check if Page exists 123 if ( $newPage != $ID && page_exists($newPage, '', false) ) { 124 // $newPage redirect 125 126 if ( auth_quickaclcheck($newPage) < AUTH_READ ) { return; } 127 128 session_write_close(); 129 header('Location: '.wl($newPage,'',true,'&')); 130 exit; 131 } 132 else 133 if ( $override ) { 134 // cleanup redirect 135 session_write_close(); 136 137 if ( auth_quickaclcheck($newPage) < AUTH_READ ) { return; } 138 139 header('Location: '.wl($ID,'',true,'&'), 301); 140 exit; 141 } 142 143 // no redirect; 144 } 145 146 /** 147 * Change the UI language in foreign language namespaces 148 */ 149 function translation_hook(&$event, $args) { 150 global $ID; 151 global $lang; 152 global $conf; 153 global $ACT; 154 155 // check if we are in a foreign language namespace 156 $lc = $this->hlp->getLangPart($ID); 157 158 // store language in session 159 if($ACT == 'show') $_SESSION[DOKU_COOKIE]['translationlc'] = $lc; 160 if(!$lc) $lc = $_SESSION[DOKU_COOKIE]['translationlc']; 161 if(!$lc) return; 162 163 if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) { 164 require(DOKU_INC.'inc/lang/'.$lc.'/lang.php'); 165 } 166 $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin 167 $conf['lang'] = $lc; 168 169 return true; 170 } 171 172 /** 173 * Resort page match results so that results are ordered by translation, having the 174 * default language first 175 */ 176 function translation_search(&$event, $args) { 177 178 if($event->data['has_titles']){ 179 // sort into translation slots 180 $res = array(); 181 foreach($event->result as $r => $t){ 182 $tr = $this->hlp->getLangPart($r); 183 if(!is_array($res["x$tr"])) $res["x$tr"] = array(); 184 $res["x$tr"][] = array($r,$t); 185 } 186 // sort by translations 187 ksort($res); 188 // combine 189 $event->result = array(); 190 foreach($res as $r){ 191 foreach($r as $l){ 192 $event->result[$l[0]] = $l[1]; 193 } 194 } 195 }else{ 196 # legacy support for old DokuWiki hooks 197 198 // sort into translation slots 199 $res = array(); 200 foreach($event->result as $r){ 201 $tr = $this->hlp->getLangPart($r); 202 if(!is_array($res["x$tr"])) $res["x$tr"] = array(); 203 $res["x$tr"][] = $r; 204 } 205 // sort by translations 206 ksort($res); 207 // combine 208 $event->result = array(); 209 foreach($res as $r){ 210 $event->result = array_merge($event->result,$r); 211 } 212 } 213 } 214 215} 216 217//Setup VIM: ex: et ts=4 enc=utf-8 : 218