xref: /plugin/autotranslation/action.php (revision f2279247ba9029b1f037c80faa9818e600ebc272)
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    /**
24     * Constructor. Load helper plugin
25     */
26    function action_plugin_translation(){
27        $this->hlp =& plugin_load('helper', 'translation');
28    }
29
30    /**
31     * Registe the events
32     */
33    function register(&$controller) {
34        // should the lang be applied to UI?
35        if($this->getConf('translateui')){
36            $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'translation_hook');
37        }
38        $controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'AFTER', $this, 'translation_search');
39    }
40
41    /**
42     * Change the UI language in foreign language namespaces
43     */
44    function translation_hook(&$event, $args) {
45        global $ID;
46        global $lang;
47        global $conf;
48        global $ACT;
49
50        // redirect away from start page?
51        if($this->conf['redirectstart'] && $ID == $conf['start'] && $ACT == 'show'){
52            $lc = $this->hlp->getBrowserLang();
53            if(!$lc) $lc = $conf['lang'];
54            header('Location: '.wl($lc.':'.$conf['start'],'',true,'&'));
55            exit;
56        }
57
58        // check if we are in a foreign language namespace
59        $lc = $this->hlp->getLangPart($ID);
60
61        // store language in session (for page related views only)
62        if(in_array($ACT,array('show','recent','diff','edit','preview','source','subscribe'))){
63            $_SESSION[DOKU_COOKIE]['translationlc'] = $lc;
64        }
65        if(!$lc) $lc = $_SESSION[DOKU_COOKIE]['translationlc'];
66        if(!$lc) return;
67
68        if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) {
69          require(DOKU_INC.'inc/lang/'.$lc.'/lang.php');
70        }
71        $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin
72        $conf['lang'] = $lc;
73
74        return true;
75    }
76
77    /**
78     * Resort page match results so that results are ordered by translation, having the
79     * default language first
80     */
81    function translation_search(&$event, $args) {
82
83        if($event->data['has_titles']){
84            // sort into translation slots
85            $res = array();
86            foreach($event->result as $r => $t){
87                $tr = $this->hlp->getLangPart($r);
88                if(!is_array($res["x$tr"])) $res["x$tr"] = array();
89                $res["x$tr"][] = array($r,$t);
90            }
91            // sort by translations
92            ksort($res);
93            // combine
94            $event->result = array();
95            foreach($res as $r){
96                foreach($r as $l){
97                    $event->result[$l[0]] = $l[1];
98                }
99            }
100        }else{
101            # legacy support for old DokuWiki hooks
102
103            // sort into translation slots
104            $res = array();
105            foreach($event->result as $r){
106                $tr = $this->hlp->getLangPart($r);
107                if(!is_array($res["x$tr"])) $res["x$tr"] = array();
108                $res["x$tr"][] = $r;
109            }
110            // sort by translations
111            ksort($res);
112            // combine
113            $event->result = array();
114            foreach($res as $r){
115                $event->result = array_merge($event->result,$r);
116            }
117        }
118    }
119
120}
121
122//Setup VIM: ex: et ts=4 enc=utf-8 :
123