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 * @var helper_plugin_translation 21 */ 22 var $hlp = null; 23 24 var $locale; 25 26 /** 27 * Constructor. Load helper plugin 28 */ 29 function action_plugin_translation() { 30 $this->hlp = plugin_load('helper', 'translation'); 31 } 32 33 /** 34 * Registe the events 35 */ 36 function register(Doku_Event_Handler $controller) { 37 // should the lang be applied to UI? 38 $scriptName = basename($_SERVER['PHP_SELF']); 39 40 if($this->getConf('translateui')) { 41 switch($scriptName) { 42 case 'js.php': 43 $controller->register_hook('INIT_LANG_LOAD', 'BEFORE', $this, 'translation_js'); 44 $controller->register_hook('JS_CACHE_USE', 'BEFORE', $this, 'translation_jscache'); 45 break; 46 47 case 'ajax.php': 48 $controller->register_hook('INIT_LANG_LOAD', 'BEFORE', $this, 'translate_media_manager'); 49 break; 50 51 case 'mediamanager.php': 52 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'setJsCacheKey'); 53 break; 54 55 default: 56 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'setJsCacheKey'); 57 } 58 } 59 60 if($scriptName !== 'js.php' && $scriptName !== 'ajax.php') { 61 $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'translation_hook'); 62 $controller->register_hook('MEDIAMANAGER_STARTED', 'BEFORE', $this, 'translation_hook'); 63 } 64 65 $controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'AFTER', $this, 'translation_search'); 66 $controller->register_hook('COMMON_PAGETPL_LOAD', 'AFTER', $this, 'page_template_replacement'); 67 } 68 69 /** 70 * Hook Callback. Make current language available as page template placeholder and handle 71 * original language copying 72 * 73 * @param $event 74 * @param $args 75 */ 76 function page_template_replacement(&$event, $args) { 77 global $ID; 78 79 // load orginal content as template? 80 if($this->getConf('copytrans') && $this->hlp->istranslatable($ID, false)) { 81 // look for existing translations 82 $translations = $this->hlp->getAvailableTranslations($ID); 83 if($translations) { 84 // find original language (might've been provided via parameter or use first translation) 85 $orig = (string) $_REQUEST['fromlang']; 86 if(!$orig) $orig = array_shift(array_keys($translations)); 87 88 // load file 89 $origfile = $translations[$orig]; 90 $event->data['tpl'] = io_readFile(wikiFN($origfile)); 91 92 // prefix with warning 93 $warn = io_readFile($this->localFN('totranslate')); 94 if($warn) $warn .= "\n\n"; 95 $event->data['tpl'] = $warn . $event->data['tpl']; 96 97 // show user a choice of translations if any 98 if(count($translations) > 1) { 99 $links = array(); 100 foreach($translations as $t => $l) { 101 $links[] = '<a href="' . wl($ID, array('do' => 'edit', 'fromlang' => $t)) . '">' . $this->hlp->getLocalName($t) . '</a>'; 102 } 103 104 msg( 105 sprintf( 106 $this->getLang('transloaded'), 107 $this->hlp->getLocalName($orig), 108 join(', ', $links) 109 ) 110 ); 111 } 112 113 } 114 } 115 116 // apply placeholders 117 $event->data['tpl'] = str_replace('@LANG@', $this->hlp->realLC(''), $event->data['tpl']); 118 $event->data['tpl'] = str_replace('@TRANS@', $this->hlp->getLangPart($ID), $event->data['tpl']); 119 } 120 121 /** 122 * Hook Callback. Load correct translation when loading JavaScript 123 * 124 * @param $event 125 * @param $args 126 */ 127 function translation_js(&$event, $args) { 128 global $conf; 129 if(!isset($_GET['lang'])) return; 130 if(!in_array($_GET['lang'], $this->hlp->trans)) return; 131 $lang = $_GET['lang']; 132 $event->data = $lang; 133 $conf['lang'] = $lang; 134 } 135 136 /** 137 * Hook Callback. Pass language code to JavaScript dispatcher 138 * 139 * @param $event 140 * @param $args 141 * @return bool 142 */ 143 function setJsCacheKey(&$event, $args) { 144 if(!isset($this->locale)) return false; 145 $count = count($event->data['script']); 146 for($i = 0; $i < $count; $i++) { 147 if(!empty($event->data['script'][$i]['src']) && strpos($event->data['script'][$i]['src'], '/lib/exe/js.php') !== false) { 148 $event->data['script'][$i]['src'] .= '&lang=' . hsc($this->locale); 149 } 150 } 151 152 return false; 153 } 154 155 /** 156 * Hook Callback. Make sure the JavaScript is translation dependent 157 * 158 * @param $event 159 * @param $args 160 */ 161 function translation_jscache(&$event, $args) { 162 if(!isset($_GET['lang'])) return; 163 if(!in_array($_GET['lang'], $this->hlp->trans)) return; 164 165 $lang = $_GET['lang']; 166 // reuse the constructor to reinitialize the cache key 167 $event->data->cache( 168 $event->data->key . $lang, 169 $event->data->ext 170 ); 171 } 172 173 /** 174 * Hook Callback. Translate the AJAX loaded media manager 175 * 176 * @param $event 177 * @param $args 178 */ 179 function translate_media_manager(&$event, $args) { 180 global $conf; 181 if(isset($_REQUEST['ID'])) { 182 $id = getID(); 183 $lc = $this->hlp->getLangPart($id); 184 } elseif(isset($_SESSION[DOKU_COOKIE]['translationlc'])) { 185 $lc = $_SESSION[DOKU_COOKIE]['translationlc']; 186 } else { 187 return; 188 } 189 if(!$lc) return; 190 191 $conf['lang'] = $lc; 192 $event->data = $lc; 193 } 194 195 /** 196 * Hook Callback. Change the UI language in foreign language namespaces 197 */ 198 function translation_hook(&$event, $args) { 199 global $ID; 200 global $lang; 201 global $conf; 202 global $ACT; 203 // redirect away from start page? 204 if($this->conf['redirectstart'] && $ID == $conf['start'] && $ACT == 'show') { 205 $lc = $this->hlp->getBrowserLang(); 206 if(!$lc) $lc = $conf['lang']; 207 $this->_redirect($lc.':'.$conf['start']); 208 exit; 209 } 210 211 // Check if we can redirect 212 if($this->getConf('redirectlocalized')){ 213 $this->translation_redirect_localized(); 214 } 215 216 // check if we are in a foreign language namespace 217 $lc = $this->hlp->getLangPart($ID); 218 219 // store language in session (for page related views only) 220 if(in_array($ACT, array('show', 'recent', 'diff', 'edit', 'preview', 'source', 'subscribe'))) { 221 $_SESSION[DOKU_COOKIE]['translationlc'] = $lc; 222 } 223 if(!$lc) $lc = $_SESSION[DOKU_COOKIE]['translationlc']; 224 if(!$lc) return; 225 $this->locale = $lc; 226 227 if(!$this->getConf('translateui')) { 228 return true; 229 } 230 231 if(file_exists(DOKU_INC . 'inc/lang/' . $lc . '/lang.php')) { 232 require(DOKU_INC . 'inc/lang/' . $lc . '/lang.php'); 233 } 234 $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin 235 $conf['lang'] = $lc; 236 237 return true; 238 } 239 240 /** 241 * Hook Callback. Resort page match results so that results are ordered by translation, having the 242 * default language first 243 */ 244 function translation_search(&$event, $args) { 245 246 if($event->data['has_titles']) { 247 // sort into translation slots 248 $res = array(); 249 foreach($event->result as $r => $t) { 250 $tr = $this->hlp->getLangPart($r); 251 if(!is_array($res["x$tr"])) $res["x$tr"] = array(); 252 $res["x$tr"][] = array($r, $t); 253 } 254 // sort by translations 255 ksort($res); 256 // combine 257 $event->result = array(); 258 foreach($res as $r) { 259 foreach($r as $l) { 260 $event->result[$l[0]] = $l[1]; 261 } 262 } 263 } else { 264 # legacy support for old DokuWiki hooks 265 266 // sort into translation slots 267 $res = array(); 268 foreach($event->result as $r) { 269 $tr = $this->hlp->getLangPart($r); 270 if(!is_array($res["x$tr"])) $res["x$tr"] = array(); 271 $res["x$tr"][] = $r; 272 } 273 // sort by translations 274 ksort($res); 275 // combine 276 $event->result = array(); 277 foreach($res as $r) { 278 $event->result = array_merge($event->result, $r); 279 } 280 } 281 } 282 283 /** 284 * Redirects to the localized version of the page when showing and browser says so and translation was explicitly requested 285 **/ 286 function translation_redirect_localized() { 287 global $ID; 288 global $conf; 289 global $ACT; 290 291 // redirect to localized page? 292 if( $ACT != 'show' ) { return; } 293 294 $override = isset($_REQUEST['tns']); // override enabled - comes from the bottom bar. 295 $lang = !empty($conf['lang_before_translation']) ? $conf['lang_before_translation'] : $conf['lang']; // Check for original language 296 297 // get current page language - if empty then default; 298 $currentSessionLanguage = $_SESSION[DOKU_COOKIE]['translationcur']; 299 $pageLang = $this->hlp->getLangPart($ID); 300 301 if ( empty($pageLang) ) { 302 $pageLang = $lang; 303 } 304 305 // If both match, we're fine. 306 if ( $currentSessionLanguage == $pageLang ) { 307 return; 308 } 309 310 // check current translation 311 if ( empty( $currentSessionLanguage ) && !$override ) { 312 313 // If not set - we must just have entered - set the browser language 314 $currentSessionLanguage = $this->hlp->getBrowserLang(); 315 316 // if no browser Language set, take entered namespace language - empty for default. 317 if ( !$currentSessionLanguage ) { 318 $currentSessionLanguage = $pageLang; 319 } 320 321 // Set new Language 322 $_SESSION[DOKU_COOKIE]['translationcur'] = $currentSessionLanguage; 323 324 // Write Language back 325 $pageLang = $currentSessionLanguage; 326 } 327 328 329 if ( $override && $pageLang != $currentSessionLanguage ) { 330 // Set new Language 331 $currentSessionLanguage = $pageLang; 332 $_SESSION[DOKU_COOKIE]['translationcur'] = $currentSessionLanguage; 333 } else if ( !$override ) { 334 // Write Language back 335 $pageLang = $currentSessionLanguage; 336 } 337 338 // If this is the default language, make empty 339 if ( $pageLang == $lang ) { 340 $pageLang = ''; 341 } 342 343 // Generate new Page ID 344 list($newPage,$name) = $this->hlp->buildTransID($pageLang,$this->hlp->getIDPart($ID)); 345 $newPage = cleanID($newPage); 346 347 // Check if Page exists 348 if ( $newPage != $ID && page_exists($newPage, '', false) ) { 349 // $newPage redirect 350 351 if ( auth_quickaclcheck($newPage) < AUTH_READ ) { return; } 352 353 session_write_close(); 354 $this->_redirect($newPage); 355 } 356 else 357 if ( $override ) { 358 // cleanup redirect 359 session_write_close(); 360 361 if ( auth_quickaclcheck($newPage) < AUTH_READ ) { return; } 362 363 $this->_redirect($ID); 364 } 365 366 // no redirect; 367 } 368 369 370 function _redirect($url) 371 { 372 unset($_GET['id']); 373 $more = array(); 374 375 if ( !empty($_GET) ) { 376 $params = ''; 377 foreach( $_GET as $key => $value ) { 378 // Possible multiple encodings. 379 $more[$key] = $value; 380 } 381 } 382 383 if ( wl( $url, $more, true, '&') != DOKU_URL . substr($_SERVER['REQUEST_URI'], 1) ) { 384 header('Location: ' . wl( $url, $more, true, '&'), 302); 385 exit; 386 } 387 } 388} 389 390//Setup VIM: ex: et ts=4 : 391