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