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 119 if($ACT == 'show') $_SESSION[DOKU_COOKIE]['translationlc'] = $lc; 120 if(!$lc) $lc = $_SESSION[DOKU_COOKIE]['translationlc']; 121 if(!$lc) return; 122 123 if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) { 124 require(DOKU_INC.'inc/lang/'.$lc.'/lang.php'); 125 } 126 $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin 127 $conf['lang'] = $lc; 128 $this->locale = $lc; 129 130 return true; 131 } 132 133 /** 134 * Resort page match results so that results are ordered by translation, having the 135 * default language first 136 */ 137 function translation_search(&$event, $args) { 138 139 if($event->data['has_titles']){ 140 // sort into translation slots 141 $res = array(); 142 foreach($event->result as $r => $t){ 143 $tr = $this->hlp->getLangPart($r); 144 if(!is_array($res["x$tr"])) $res["x$tr"] = array(); 145 $res["x$tr"][] = array($r,$t); 146 } 147 // sort by translations 148 ksort($res); 149 // combine 150 $event->result = array(); 151 foreach($res as $r){ 152 foreach($r as $l){ 153 $event->result[$l[0]] = $l[1]; 154 } 155 } 156 }else{ 157 # legacy support for old DokuWiki hooks 158 159 // sort into translation slots 160 $res = array(); 161 foreach($event->result as $r){ 162 $tr = $this->hlp->getLangPart($r); 163 if(!is_array($res["x$tr"])) $res["x$tr"] = array(); 164 $res["x$tr"][] = $r; 165 } 166 // sort by translations 167 ksort($res); 168 // combine 169 $event->result = array(); 170 foreach($res as $r){ 171 $event->result = array_merge($event->result,$r); 172 } 173 } 174 } 175 176} 177 178//Setup VIM: ex: et ts=4 : 179