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
62        if($ACT == 'show') $_SESSION[DOKU_COOKIE]['translationlc'] = $lc;
63        if(!$lc) $lc = $_SESSION[DOKU_COOKIE]['translationlc'];
64        if(!$lc) return;
65
66        if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) {
67          require(DOKU_INC.'inc/lang/'.$lc.'/lang.php');
68        }
69        $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin
70        $conf['lang'] = $lc;
71
72        return true;
73    }
74
75    /**
76     * Resort page match results so that results are ordered by translation, having the
77     * default language first
78     */
79    function translation_search(&$event, $args) {
80
81        if($event->data['has_titles']){
82            // sort into translation slots
83            $res = array();
84            foreach($event->result as $r => $t){
85                $tr = $this->hlp->getLangPart($r);
86                if(!is_array($res["x$tr"])) $res["x$tr"] = array();
87                $res["x$tr"][] = array($r,$t);
88            }
89            // sort by translations
90            ksort($res);
91            // combine
92            $event->result = array();
93            foreach($res as $r){
94                foreach($r as $l){
95                    $event->result[$l[0]] = $l[1];
96                }
97            }
98        }else{
99            # legacy support for old DokuWiki hooks
100
101            // sort into translation slots
102            $res = array();
103            foreach($event->result as $r){
104                $tr = $this->hlp->getLangPart($r);
105                if(!is_array($res["x$tr"])) $res["x$tr"] = array();
106                $res["x$tr"][] = $r;
107            }
108            // sort by translations
109            ksort($res);
110            // combine
111            $event->result = array();
112            foreach($res as $r){
113                $event->result = array_merge($event->result,$r);
114            }
115        }
116    }
117
118}
119
120//Setup VIM: ex: et ts=4 enc=utf-8 :
121