xref: /plugin/autotranslation/action.php (revision 7053cd66796b13d1f30019dd1d4b008f5d2a9fb8)
10a7415d3SAndreas Gohr<?php
20a7415d3SAndreas Gohr/**
3af1904f9SAndreas Gohr * Translation Plugin: Simple multilanguage plugin
40a7415d3SAndreas Gohr *
50a7415d3SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
60a7415d3SAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
70a7415d3SAndreas Gohr * @author     Guy Brand <gb@isis.u-strasbg.fr>
80a7415d3SAndreas Gohr */
90a7415d3SAndreas Gohr
100a7415d3SAndreas Gohr// must be run within Dokuwiki
110a7415d3SAndreas Gohrif(!defined('DOKU_INC')) die();
120a7415d3SAndreas Gohr
130a7415d3SAndreas Gohrif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
140a7415d3SAndreas Gohrrequire_once(DOKU_PLUGIN.'action.php');
150a7415d3SAndreas Gohr
160a7415d3SAndreas Gohrclass action_plugin_translation extends DokuWiki_Action_Plugin {
170a7415d3SAndreas Gohr
180a7415d3SAndreas Gohr    /**
19af1904f9SAndreas Gohr     * for th helper plugin
20af1904f9SAndreas Gohr     */
21af1904f9SAndreas Gohr    var $hlp = null;
22af1904f9SAndreas Gohr
23af1904f9SAndreas Gohr    /**
24af1904f9SAndreas Gohr     * Constructor. Load helper plugin
25af1904f9SAndreas Gohr     */
26af1904f9SAndreas Gohr    function action_plugin_translation(){
27af1904f9SAndreas Gohr        $this->hlp =& plugin_load('helper', 'translation');
28af1904f9SAndreas Gohr    }
29af1904f9SAndreas Gohr
30af1904f9SAndreas Gohr    /**
310a7415d3SAndreas Gohr     * return some info
320a7415d3SAndreas Gohr     */
330a7415d3SAndreas Gohr    function getInfo(){
340a7415d3SAndreas Gohr        return confToHash(dirname(__FILE__).'/info.txt');
350a7415d3SAndreas Gohr    }
360a7415d3SAndreas Gohr
370a7415d3SAndreas Gohr    /**
380a7415d3SAndreas Gohr     * Registe the events
390a7415d3SAndreas Gohr     */
400a7415d3SAndreas Gohr    function register(&$controller) {
410a7415d3SAndreas Gohr        // should the lang be applied to UI?
420a7415d3SAndreas Gohr        if($this->getConf('translateui')){
430a7415d3SAndreas Gohr            $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'translation_hook');
440a7415d3SAndreas Gohr        }
45af1904f9SAndreas Gohr        $controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'AFTER', $this, 'translation_search');
460a7415d3SAndreas Gohr    }
470a7415d3SAndreas Gohr
480a7415d3SAndreas Gohr    /**
490a7415d3SAndreas Gohr     * Change the UI language in foreign language namespaces
500a7415d3SAndreas Gohr     */
510a7415d3SAndreas Gohr    function translation_hook(&$event, $args) {
520a7415d3SAndreas Gohr        global $ID;
530a7415d3SAndreas Gohr        global $lang;
540a7415d3SAndreas Gohr        global $conf;
55*7053cd66SAndreas Gohr        global $ACT;
56*7053cd66SAndreas Gohr
57*7053cd66SAndreas Gohr        // redirect away from start page?
58*7053cd66SAndreas Gohr        if($this->conf['redirectstart'] && $ID == $conf['start'] && $ACT == 'show'){
59*7053cd66SAndreas Gohr            $lc = $this->hlp->getBrowserLang();
60*7053cd66SAndreas Gohr            if(!$lc) $lc = $conf['lang'];
61*7053cd66SAndreas Gohr            header('Location: '.wl($lc.':'.$conf['start'],'',true,'&'));
62*7053cd66SAndreas Gohr            exit;
63*7053cd66SAndreas Gohr        }
640a7415d3SAndreas Gohr
650a7415d3SAndreas Gohr        // check if we are in a foreign language namespace
66af1904f9SAndreas Gohr        $lc = $this->hlp->getLangPart($ID);
67*7053cd66SAndreas Gohr        if(!$lc){
68*7053cd66SAndreas Gohr            return;
69*7053cd66SAndreas Gohr        }
700a7415d3SAndreas Gohr
710a7415d3SAndreas Gohr        if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) {
720a7415d3SAndreas Gohr          require(DOKU_INC.'inc/lang/'.$lc.'/lang.php');
730a7415d3SAndreas Gohr        }
740a7415d3SAndreas Gohr        $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin
750a7415d3SAndreas Gohr        $conf['lang'] = $lc;
760a7415d3SAndreas Gohr
770a7415d3SAndreas Gohr        return true;
780a7415d3SAndreas Gohr    }
79af1904f9SAndreas Gohr
80af1904f9SAndreas Gohr    /**
81af1904f9SAndreas Gohr     * Resort page match results so that results are ordered by translation, having the
82af1904f9SAndreas Gohr     * default language first
83af1904f9SAndreas Gohr     */
84af1904f9SAndreas Gohr    function translation_search(&$event, $args) {
85af1904f9SAndreas Gohr        // sort into translation slots
86af1904f9SAndreas Gohr        $res = array();
87af1904f9SAndreas Gohr        foreach($event->result as $r){
88af1904f9SAndreas Gohr            $tr = $this->hlp->getLangPart($r);
89af1904f9SAndreas Gohr            if(!is_array($res["x$tr"])) $res["x$tr"] = array();
90af1904f9SAndreas Gohr            $res["x$tr"][] = $r;
91af1904f9SAndreas Gohr        }
92af1904f9SAndreas Gohr        // sort by translations
93af1904f9SAndreas Gohr        ksort($res);
94af1904f9SAndreas Gohr        // combine
95af1904f9SAndreas Gohr        $event->result = array();
96af1904f9SAndreas Gohr        foreach($res as $r){
97af1904f9SAndreas Gohr            $event->result = array_merge($event->result,$r);
98af1904f9SAndreas Gohr        }
99af1904f9SAndreas Gohr    }
100af1904f9SAndreas Gohr
1010a7415d3SAndreas Gohr}
1020a7415d3SAndreas Gohr
1030a7415d3SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 :
104