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 var $locale; 24 25 /** 26 * Constructor. Load helper plugin 27 */ 28 function action_plugin_translation(){ 29 $this->hlp =& plugin_load('helper', 'translation'); 30 } 31 32 /** 33 * Registe the events 34 */ 35 function register(&$controller) { 36 // should the lang be applied to UI? 37 if($this->getConf('translateui')){ 38 $scriptName = basename($_SERVER['PHP_SELF']); 39 40 switch ($scriptName) { 41 case 'js.php': 42 $controller->register_hook('INIT_LANG_LOAD', 'BEFORE', $this, 'translation_js'); 43 $controller->register_hook('JS_CACHE_USE', 'BEFORE', $this, 'translation_jscache'); 44 break; 45 46 case 'ajax.php': 47 $controller->register_hook('INIT_LANG_LOAD', 'BEFORE', $this, 'translate_media_manager'); 48 break; 49 50 case 'mediamanager.php': 51 $controller->register_hook('MEDIAMANAGER_STARTED', 'BEFORE', $this, 'translation_hook'); 52 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'setJsCacheKey'); 53 break; 54 55 default: 56 $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'translation_hook'); 57 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'setJsCacheKey'); 58 } 59 } 60 $controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'AFTER', $this, 'translation_search'); 61 } 62 63 function setJsCacheKey(&$event, $args) { 64 if (!isset($this->locale)) return false; 65 $count = count($event->data['script']); 66 for ($i = 0; $i<$count; $i++) { 67 if (strpos($event->data['script'][$i]['src'], '/lib/exe/js.php') !== false) { 68 $event->data['script'][$i]['src'] .= '&lang='.hsc($this->locale); 69 } 70 } 71 72 return false; 73 } 74 75 function translation_js(&$event, $args) { 76 global $conf; 77 if(!isset($_GET['lang'])) return; 78 if(!in_array($_GET['lang'],$this->hlp->trans)) return; 79 $lang = $_GET['lang']; 80 $event->data = $lang; 81 $conf['lang'] = $lang; 82 } 83 84 function translation_jscache(&$event, $args) { 85 if (!isset($_GET['lang'])) return; 86 if(!in_array($_GET['lang'],$this->hlp->trans)) return; 87 88 $lang = $_GET['lang']; 89 // reuse the constructor to reinitialize the cache key 90 $event->data->cache( 91 $event->data->key . $lang, 92 $event->data->ext 93 ); 94 } 95 96 function translate_media_manager(&$event, $args) { 97 global $conf; 98 if (isset($_REQUEST['ID'])) { 99 $id = getID(); 100 $lc = $this->hlp->getLangPart($id); 101 } elseif (isset($_SESSION[DOKU_COOKIE]['translationlc'])) { 102 $lc = $_SESSION[DOKU_COOKIE]['translationlc']; 103 } else { 104 return; 105 } 106 $conf['lang'] = $lc; 107 $event->data = $lc; 108 } 109 110 /** 111 * Change the UI language in foreign language namespaces 112 */ 113 function translation_hook(&$event, $args) { 114 global $ID; 115 global $lang; 116 global $conf; 117 global $ACT; 118 // redirect away from start page? 119 if($this->conf['redirectstart'] && $ID == $conf['start'] && $ACT == 'show'){ 120 $lc = $this->hlp->getBrowserLang(); 121 if(!$lc) $lc = $conf['lang']; 122 header('Location: '.wl($lc.':'.$conf['start'],'',true,'&')); 123 exit; 124 } 125 126 // check if we are in a foreign language namespace 127 $lc = $this->hlp->getLangPart($ID); 128 129 // store language in session (for page related views only) 130 if(in_array($ACT,array('show','recent','diff','edit','preview','source','subscribe'))){ 131 $_SESSION[DOKU_COOKIE]['translationlc'] = $lc; 132 } 133 if(!$lc) $lc = $_SESSION[DOKU_COOKIE]['translationlc']; 134 if(!$lc) return; 135 136 if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) { 137 require(DOKU_INC.'inc/lang/'.$lc.'/lang.php'); 138 } 139 $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin 140 $conf['lang'] = $lc; 141 $this->locale = $lc; 142 143 return true; 144 } 145 146 /** 147 * Resort page match results so that results are ordered by translation, having the 148 * default language first 149 */ 150 function translation_search(&$event, $args) { 151 152 if($event->data['has_titles']){ 153 // sort into translation slots 154 $res = array(); 155 foreach($event->result as $r => $t){ 156 $tr = $this->hlp->getLangPart($r); 157 if(!is_array($res["x$tr"])) $res["x$tr"] = array(); 158 $res["x$tr"][] = array($r,$t); 159 } 160 // sort by translations 161 ksort($res); 162 // combine 163 $event->result = array(); 164 foreach($res as $r){ 165 foreach($r as $l){ 166 $event->result[$l[0]] = $l[1]; 167 } 168 } 169 }else{ 170 # legacy support for old DokuWiki hooks 171 172 // sort into translation slots 173 $res = array(); 174 foreach($event->result as $r){ 175 $tr = $this->hlp->getLangPart($r); 176 if(!is_array($res["x$tr"])) $res["x$tr"] = array(); 177 $res["x$tr"][] = $r; 178 } 179 // sort by translations 180 ksort($res); 181 // combine 182 $event->result = array(); 183 foreach($res as $r){ 184 $event->result = array_merge($event->result,$r); 185 } 186 } 187 } 188 189} 190 191//Setup VIM: ex: et ts=4 : 192