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