xref: /plugin/autotranslation/action.php (revision f2279247ba9029b1f037c80faa9818e600ebc272)
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
61*f2279247SAndreas Gohr        // store language in session (for page related views only)
62*f2279247SAndreas Gohr        if(in_array($ACT,array('show','recent','diff','edit','preview','source','subscribe'))){
63*f2279247SAndreas Gohr            $_SESSION[DOKU_COOKIE]['translationlc'] = $lc;
64*f2279247SAndreas Gohr        }
65a526927fSAndreas Gohr        if(!$lc) $lc = $_SESSION[DOKU_COOKIE]['translationlc'];
66a526927fSAndreas Gohr        if(!$lc) return;
670a7415d3SAndreas Gohr
680a7415d3SAndreas Gohr        if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) {
690a7415d3SAndreas Gohr          require(DOKU_INC.'inc/lang/'.$lc.'/lang.php');
700a7415d3SAndreas Gohr        }
710a7415d3SAndreas Gohr        $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin
720a7415d3SAndreas Gohr        $conf['lang'] = $lc;
730a7415d3SAndreas Gohr
740a7415d3SAndreas Gohr        return true;
750a7415d3SAndreas Gohr    }
76af1904f9SAndreas Gohr
77af1904f9SAndreas Gohr    /**
78af1904f9SAndreas Gohr     * Resort page match results so that results are ordered by translation, having the
79af1904f9SAndreas Gohr     * default language first
80af1904f9SAndreas Gohr     */
81af1904f9SAndreas Gohr    function translation_search(&$event, $args) {
82d75e50bcSAndreas Gohr
83d75e50bcSAndreas Gohr        if($event->data['has_titles']){
84d75e50bcSAndreas Gohr            // sort into translation slots
85d75e50bcSAndreas Gohr            $res = array();
86d75e50bcSAndreas Gohr            foreach($event->result as $r => $t){
87d75e50bcSAndreas Gohr                $tr = $this->hlp->getLangPart($r);
88d75e50bcSAndreas Gohr                if(!is_array($res["x$tr"])) $res["x$tr"] = array();
89d75e50bcSAndreas Gohr                $res["x$tr"][] = array($r,$t);
90d75e50bcSAndreas Gohr            }
91d75e50bcSAndreas Gohr            // sort by translations
92d75e50bcSAndreas Gohr            ksort($res);
93d75e50bcSAndreas Gohr            // combine
94d75e50bcSAndreas Gohr            $event->result = array();
95d75e50bcSAndreas Gohr            foreach($res as $r){
96d75e50bcSAndreas Gohr                foreach($r as $l){
97d75e50bcSAndreas Gohr                    $event->result[$l[0]] = $l[1];
98d75e50bcSAndreas Gohr                }
99d75e50bcSAndreas Gohr            }
100d75e50bcSAndreas Gohr        }else{
101d75e50bcSAndreas Gohr            # legacy support for old DokuWiki hooks
102d75e50bcSAndreas Gohr
103af1904f9SAndreas Gohr            // sort into translation slots
104af1904f9SAndreas Gohr            $res = array();
105af1904f9SAndreas Gohr            foreach($event->result as $r){
106af1904f9SAndreas Gohr                $tr = $this->hlp->getLangPart($r);
107af1904f9SAndreas Gohr                if(!is_array($res["x$tr"])) $res["x$tr"] = array();
108af1904f9SAndreas Gohr                $res["x$tr"][] = $r;
109af1904f9SAndreas Gohr            }
110af1904f9SAndreas Gohr            // sort by translations
111af1904f9SAndreas Gohr            ksort($res);
112af1904f9SAndreas Gohr            // combine
113af1904f9SAndreas Gohr            $event->result = array();
114af1904f9SAndreas Gohr            foreach($res as $r){
115af1904f9SAndreas Gohr                $event->result = array_merge($event->result,$r);
116af1904f9SAndreas Gohr            }
117af1904f9SAndreas Gohr        }
118d75e50bcSAndreas Gohr    }
119af1904f9SAndreas Gohr
1200a7415d3SAndreas Gohr}
1210a7415d3SAndreas Gohr
1220a7415d3SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 :
123