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 $controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'AFTER', $this, 'translation_search'); 39 } 40 41 /** 42 * Change the UI language in foreign language namespaces 43 */ 44 function translation_hook(&$event, $args) { 45 global $ID; 46 global $lang; 47 global $conf; 48 global $ACT; 49 50 // redirect away from start page? 51 if($this->conf['redirectstart'] && $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 // check if we are in a foreign language namespace 59 $lc = $this->hlp->getLangPart($ID); 60 61 // store language in session 62 if($ACT == 'show') $_SESSION[DOKU_COOKIE]['translationlc'] = $lc; 63 if(!$lc) $lc = $_SESSION[DOKU_COOKIE]['translationlc']; 64 if(!$lc) return; 65 66 if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) { 67 require(DOKU_INC.'inc/lang/'.$lc.'/lang.php'); 68 } 69 $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin 70 $conf['lang'] = $lc; 71 72 return true; 73 } 74 75 /** 76 * Resort page match results so that results are ordered by translation, having the 77 * default language first 78 */ 79 function translation_search(&$event, $args) { 80 // sort into translation slots 81 $res = array(); 82 foreach($event->result as $r){ 83 $tr = $this->hlp->getLangPart($r); 84 if(!is_array($res["x$tr"])) $res["x$tr"] = array(); 85 $res["x$tr"][] = $r; 86 } 87 // sort by translations 88 ksort($res); 89 // combine 90 $event->result = array(); 91 foreach($res as $r){ 92 $event->result = array_merge($event->result,$r); 93 } 94 } 95 96} 97 98//Setup VIM: ex: et ts=4 enc=utf-8 : 99