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