10a7415d3SAndreas Gohr<?php 20a7415d3SAndreas Gohr/** 3af1904f9SAndreas Gohr * Translation Plugin: Simple multilanguage plugin 40a7415d3SAndreas Gohr * 50a7415d3SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 60a7415d3SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 70a7415d3SAndreas Gohr * @author Guy Brand <gb@isis.u-strasbg.fr> 80a7415d3SAndreas Gohr */ 90a7415d3SAndreas Gohr 100a7415d3SAndreas Gohr// must be run within Dokuwiki 110a7415d3SAndreas Gohrif(!defined('DOKU_INC')) die(); 120a7415d3SAndreas Gohr 130a7415d3SAndreas Gohrif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 140a7415d3SAndreas Gohrrequire_once(DOKU_PLUGIN.'action.php'); 150a7415d3SAndreas Gohr 160a7415d3SAndreas Gohrclass action_plugin_translation extends DokuWiki_Action_Plugin { 170a7415d3SAndreas Gohr 180a7415d3SAndreas Gohr /** 19af1904f9SAndreas Gohr * for th helper plugin 20af1904f9SAndreas Gohr */ 21af1904f9SAndreas Gohr var $hlp = null; 22af1904f9SAndreas Gohr 23af1904f9SAndreas Gohr /** 24af1904f9SAndreas Gohr * Constructor. Load helper plugin 25af1904f9SAndreas Gohr */ 26af1904f9SAndreas Gohr function action_plugin_translation(){ 27af1904f9SAndreas Gohr $this->hlp =& plugin_load('helper', 'translation'); 28af1904f9SAndreas Gohr } 29af1904f9SAndreas Gohr 30af1904f9SAndreas Gohr /** 310a7415d3SAndreas Gohr * Registe the events 320a7415d3SAndreas Gohr */ 330a7415d3SAndreas Gohr function register(&$controller) { 340a7415d3SAndreas Gohr // should the lang be applied to UI? 350a7415d3SAndreas Gohr if($this->getConf('translateui')){ 360a7415d3SAndreas Gohr $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'translation_hook'); 370a7415d3SAndreas Gohr } 38af1904f9SAndreas Gohr $controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'AFTER', $this, 'translation_search'); 390a7415d3SAndreas Gohr } 400a7415d3SAndreas Gohr 410a7415d3SAndreas Gohr /** 420a7415d3SAndreas Gohr * Change the UI language in foreign language namespaces 430a7415d3SAndreas Gohr */ 440a7415d3SAndreas Gohr function translation_hook(&$event, $args) { 450a7415d3SAndreas Gohr global $ID; 460a7415d3SAndreas Gohr global $lang; 470a7415d3SAndreas Gohr global $conf; 487053cd66SAndreas Gohr global $ACT; 497053cd66SAndreas Gohr 507053cd66SAndreas Gohr // redirect away from start page? 517053cd66SAndreas Gohr if($this->conf['redirectstart'] && $ID == $conf['start'] && $ACT == 'show'){ 527053cd66SAndreas Gohr $lc = $this->hlp->getBrowserLang(); 537053cd66SAndreas Gohr if(!$lc) $lc = $conf['lang']; 547053cd66SAndreas Gohr header('Location: '.wl($lc.':'.$conf['start'],'',true,'&')); 557053cd66SAndreas Gohr exit; 567053cd66SAndreas Gohr } 570a7415d3SAndreas Gohr 580a7415d3SAndreas Gohr // check if we are in a foreign language namespace 59af1904f9SAndreas Gohr $lc = $this->hlp->getLangPart($ID); 60a526927fSAndreas Gohr 61a526927fSAndreas Gohr // store language in session 624616cf2cSAndreas Gohr if($ACT == 'show') $_SESSION[DOKU_COOKIE]['translationlc'] = $lc; 63a526927fSAndreas Gohr if(!$lc) $lc = $_SESSION[DOKU_COOKIE]['translationlc']; 64a526927fSAndreas Gohr if(!$lc) return; 650a7415d3SAndreas Gohr 660a7415d3SAndreas Gohr if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) { 670a7415d3SAndreas Gohr require(DOKU_INC.'inc/lang/'.$lc.'/lang.php'); 680a7415d3SAndreas Gohr } 690a7415d3SAndreas Gohr $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin 700a7415d3SAndreas Gohr $conf['lang'] = $lc; 710a7415d3SAndreas Gohr 720a7415d3SAndreas Gohr return true; 730a7415d3SAndreas Gohr } 74af1904f9SAndreas Gohr 75af1904f9SAndreas Gohr /** 76af1904f9SAndreas Gohr * Resort page match results so that results are ordered by translation, having the 77af1904f9SAndreas Gohr * default language first 78af1904f9SAndreas Gohr */ 79af1904f9SAndreas Gohr function translation_search(&$event, $args) { 80*d75e50bcSAndreas Gohr 81*d75e50bcSAndreas Gohr if($event->data['has_titles']){ 82*d75e50bcSAndreas Gohr // sort into translation slots 83*d75e50bcSAndreas Gohr $res = array(); 84*d75e50bcSAndreas Gohr foreach($event->result as $r => $t){ 85*d75e50bcSAndreas Gohr $tr = $this->hlp->getLangPart($r); 86*d75e50bcSAndreas Gohr if(!is_array($res["x$tr"])) $res["x$tr"] = array(); 87*d75e50bcSAndreas Gohr $res["x$tr"][] = array($r,$t); 88*d75e50bcSAndreas Gohr } 89*d75e50bcSAndreas Gohr // sort by translations 90*d75e50bcSAndreas Gohr ksort($res); 91*d75e50bcSAndreas Gohr // combine 92*d75e50bcSAndreas Gohr $event->result = array(); 93*d75e50bcSAndreas Gohr foreach($res as $r){ 94*d75e50bcSAndreas Gohr foreach($r as $l){ 95*d75e50bcSAndreas Gohr $event->result[$l[0]] = $l[1]; 96*d75e50bcSAndreas Gohr } 97*d75e50bcSAndreas Gohr } 98*d75e50bcSAndreas Gohr }else{ 99*d75e50bcSAndreas Gohr # legacy support for old DokuWiki hooks 100*d75e50bcSAndreas Gohr 101af1904f9SAndreas Gohr // sort into translation slots 102af1904f9SAndreas Gohr $res = array(); 103af1904f9SAndreas Gohr foreach($event->result as $r){ 104af1904f9SAndreas Gohr $tr = $this->hlp->getLangPart($r); 105af1904f9SAndreas Gohr if(!is_array($res["x$tr"])) $res["x$tr"] = array(); 106af1904f9SAndreas Gohr $res["x$tr"][] = $r; 107af1904f9SAndreas Gohr } 108af1904f9SAndreas Gohr // sort by translations 109af1904f9SAndreas Gohr ksort($res); 110af1904f9SAndreas Gohr // combine 111af1904f9SAndreas Gohr $event->result = array(); 112af1904f9SAndreas Gohr foreach($res as $r){ 113af1904f9SAndreas Gohr $event->result = array_merge($event->result,$r); 114af1904f9SAndreas Gohr } 115af1904f9SAndreas Gohr } 116*d75e50bcSAndreas Gohr } 117af1904f9SAndreas Gohr 1180a7415d3SAndreas Gohr} 1190a7415d3SAndreas Gohr 1200a7415d3SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 : 121