xref: /plugin/autotranslation/action.php (revision af1904f938cc047e8f90eb83a2552539e6802414)
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
56        // check if we are in a foreign language namespace
57        $lc = $this->hlp->getLangPart($ID);
58        if(!$lc) return;
59
60        if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) {
61          require(DOKU_INC.'inc/lang/'.$lc.'/lang.php');
62        }
63        $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin
64        $conf['lang'] = $lc;
65
66        return true;
67    }
68
69    /**
70     * Resort page match results so that results are ordered by translation, having the
71     * default language first
72     */
73    function translation_search(&$event, $args) {
74        // sort into translation slots
75        $res = array();
76        foreach($event->result as $r){
77            $tr = $this->hlp->getLangPart($r);
78            if(!is_array($res["x$tr"])) $res["x$tr"] = array();
79            $res["x$tr"][] = $r;
80        }
81        // sort by translations
82        ksort($res);
83        // combine
84        $event->result = array();
85        foreach($res as $r){
86            $event->result = array_merge($event->result,$r);
87        }
88    }
89
90}
91
92//Setup VIM: ex: et ts=4 enc=utf-8 :
93