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 if (!isset($_GET['cacheKey'])) return false; 76 77 $key = $_GET['cacheKey']; 78 $event->data = $key; 79 $conf['lang'] = $key; 80 return false; 81 } 82 83 function translate_media_manager(&$event, $args) { 84 global $conf; 85 if (isset($_REQUEST['ID'])) { 86 $id = getID(); 87 $lc = $this->hlp->getLangPart($id); 88 } elseif (isset($_SESSION[DOKU_COOKIE]['translationlc'])) { 89 $lc = $_SESSION[DOKU_COOKIE]['translationlc']; 90 } else { 91 return false; 92 } 93 $conf['lang'] = $lc; 94 $event->data = $lc; 95 return false; 96 } 97 98 /** 99 * Change the UI language in foreign language namespaces 100 */ 101 function translation_hook(&$event, $args) { 102 global $ID; 103 global $lang; 104 global $conf; 105 global $ACT; 106 // redirect away from start page? 107 if($this->conf['redirectstart'] && $ID == $conf['start'] && $ACT == 'show'){ 108 $lc = $this->hlp->getBrowserLang(); 109 if(!$lc) $lc = $conf['lang']; 110 header('Location: '.wl($lc.':'.$conf['start'],'',true,'&')); 111 exit; 112 } 113 114 // check if we are in a foreign language namespace 115 $lc = $this->hlp->getLangPart($ID); 116 117 // store language in session 118 if($ACT == 'show') $_SESSION[DOKU_COOKIE]['translationlc'] = $lc; 119 if(!$lc) $lc = $_SESSION[DOKU_COOKIE]['translationlc']; 120 if(!$lc) return; 121 122 if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) { 123 require(DOKU_INC.'inc/lang/'.$lc.'/lang.php'); 124 } 125 $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin 126 $conf['lang'] = $lc; 127 $this->locale = $lc; 128 129 return true; 130 } 131 132 /** 133 * Resort page match results so that results are ordered by translation, having the 134 * default language first 135 */ 136 function translation_search(&$event, $args) { 137 138 if($event->data['has_titles']){ 139 // sort into translation slots 140 $res = array(); 141 foreach($event->result as $r => $t){ 142 $tr = $this->hlp->getLangPart($r); 143 if(!is_array($res["x$tr"])) $res["x$tr"] = array(); 144 $res["x$tr"][] = array($r,$t); 145 } 146 // sort by translations 147 ksort($res); 148 // combine 149 $event->result = array(); 150 foreach($res as $r){ 151 foreach($r as $l){ 152 $event->result[$l[0]] = $l[1]; 153 } 154 } 155 }else{ 156 # legacy support for old DokuWiki hooks 157 158 // sort into translation slots 159 $res = array(); 160 foreach($event->result as $r){ 161 $tr = $this->hlp->getLangPart($r); 162 if(!is_array($res["x$tr"])) $res["x$tr"] = array(); 163 $res["x$tr"][] = $r; 164 } 165 // sort by translations 166 ksort($res); 167 // combine 168 $event->result = array(); 169 foreach($res as $r){ 170 $event->result = array_merge($event->result,$r); 171 } 172 } 173 } 174 175} 176 177//Setup VIM: ex: et ts=4 : 178