xref: /plugin/autotranslation/action.php (revision 4616cf2c3afa3a9aabd5decdfb875fc81d1215da)
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     * return some info
32     */
33    function getInfo(){
34        return confToHash(dirname(__FILE__).'/info.txt');
35    }
36
37    /**
38     * Registe the events
39     */
40    function register(&$controller) {
41        // should the lang be applied to UI?
42        if($this->getConf('translateui')){
43            $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'translation_hook');
44        }
45        $controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'AFTER', $this, 'translation_search');
46    }
47
48    /**
49     * Change the UI language in foreign language namespaces
50     */
51    function translation_hook(&$event, $args) {
52        global $ID;
53        global $lang;
54        global $conf;
55        global $ACT;
56
57        // redirect away from start page?
58        if($this->conf['redirectstart'] && $ID == $conf['start'] && $ACT == 'show'){
59            $lc = $this->hlp->getBrowserLang();
60            if(!$lc) $lc = $conf['lang'];
61            header('Location: '.wl($lc.':'.$conf['start'],'',true,'&'));
62            exit;
63        }
64
65        // check if we are in a foreign language namespace
66        $lc = $this->hlp->getLangPart($ID);
67
68        // store language in session
69        if($ACT == 'show') $_SESSION[DOKU_COOKIE]['translationlc'] = $lc;
70        if(!$lc) $lc = $_SESSION[DOKU_COOKIE]['translationlc'];
71        if(!$lc) return;
72
73        if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) {
74          require(DOKU_INC.'inc/lang/'.$lc.'/lang.php');
75        }
76        $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin
77        $conf['lang'] = $lc;
78
79        return true;
80    }
81
82    /**
83     * Resort page match results so that results are ordered by translation, having the
84     * default language first
85     */
86    function translation_search(&$event, $args) {
87        // sort into translation slots
88        $res = array();
89        foreach($event->result as $r){
90            $tr = $this->hlp->getLangPart($r);
91            if(!is_array($res["x$tr"])) $res["x$tr"] = array();
92            $res["x$tr"][] = $r;
93        }
94        // sort by translations
95        ksort($res);
96        // combine
97        $event->result = array();
98        foreach($res as $r){
99            $event->result = array_merge($event->result,$r);
100        }
101    }
102
103}
104
105//Setup VIM: ex: et ts=4 enc=utf-8 :
106