xref: /plugin/autotranslation/action.php (revision 7053cd66796b13d1f30019dd1d4b008f5d2a9fb8)
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        if(!$lc){
68            return;
69        }
70
71        if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) {
72          require(DOKU_INC.'inc/lang/'.$lc.'/lang.php');
73        }
74        $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin
75        $conf['lang'] = $lc;
76
77        return true;
78    }
79
80    /**
81     * Resort page match results so that results are ordered by translation, having the
82     * default language first
83     */
84    function translation_search(&$event, $args) {
85        // sort into translation slots
86        $res = array();
87        foreach($event->result as $r){
88            $tr = $this->hlp->getLangPart($r);
89            if(!is_array($res["x$tr"])) $res["x$tr"] = array();
90            $res["x$tr"][] = $r;
91        }
92        // sort by translations
93        ksort($res);
94        // combine
95        $event->result = array();
96        foreach($res as $r){
97            $event->result = array_merge($event->result,$r);
98        }
99    }
100
101}
102
103//Setup VIM: ex: et ts=4 enc=utf-8 :
104