xref: /plugin/autotranslation/action.php (revision a526927f1124a536b6505e164980558c204c7344)
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;
557053cd66SAndreas Gohr        global $ACT;
567053cd66SAndreas Gohr
577053cd66SAndreas Gohr        // redirect away from start page?
587053cd66SAndreas Gohr        if($this->conf['redirectstart'] && $ID == $conf['start'] && $ACT == 'show'){
597053cd66SAndreas Gohr            $lc = $this->hlp->getBrowserLang();
607053cd66SAndreas Gohr            if(!$lc) $lc = $conf['lang'];
617053cd66SAndreas Gohr            header('Location: '.wl($lc.':'.$conf['start'],'',true,'&'));
627053cd66SAndreas Gohr            exit;
637053cd66SAndreas Gohr        }
640a7415d3SAndreas Gohr
650a7415d3SAndreas Gohr        // check if we are in a foreign language namespace
66af1904f9SAndreas Gohr        $lc = $this->hlp->getLangPart($ID);
67*a526927fSAndreas Gohr
68*a526927fSAndreas Gohr        // store language in session
69*a526927fSAndreas Gohr        if($lc) $_SESSION[DOKU_COOKIE]['translationlc'] = $lc;
70*a526927fSAndreas Gohr        if(!$lc) $lc = $_SESSION[DOKU_COOKIE]['translationlc'];
71*a526927fSAndreas Gohr        if(!$lc) return;
720a7415d3SAndreas Gohr
730a7415d3SAndreas Gohr        if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) {
740a7415d3SAndreas Gohr          require(DOKU_INC.'inc/lang/'.$lc.'/lang.php');
750a7415d3SAndreas Gohr        }
760a7415d3SAndreas Gohr        $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin
770a7415d3SAndreas Gohr        $conf['lang'] = $lc;
780a7415d3SAndreas Gohr
790a7415d3SAndreas Gohr        return true;
800a7415d3SAndreas Gohr    }
81af1904f9SAndreas Gohr
82af1904f9SAndreas Gohr    /**
83af1904f9SAndreas Gohr     * Resort page match results so that results are ordered by translation, having the
84af1904f9SAndreas Gohr     * default language first
85af1904f9SAndreas Gohr     */
86af1904f9SAndreas Gohr    function translation_search(&$event, $args) {
87af1904f9SAndreas Gohr        // sort into translation slots
88af1904f9SAndreas Gohr        $res = array();
89af1904f9SAndreas Gohr        foreach($event->result as $r){
90af1904f9SAndreas Gohr            $tr = $this->hlp->getLangPart($r);
91af1904f9SAndreas Gohr            if(!is_array($res["x$tr"])) $res["x$tr"] = array();
92af1904f9SAndreas Gohr            $res["x$tr"][] = $r;
93af1904f9SAndreas Gohr        }
94af1904f9SAndreas Gohr        // sort by translations
95af1904f9SAndreas Gohr        ksort($res);
96af1904f9SAndreas Gohr        // combine
97af1904f9SAndreas Gohr        $event->result = array();
98af1904f9SAndreas Gohr        foreach($res as $r){
99af1904f9SAndreas Gohr            $event->result = array_merge($event->result,$r);
100af1904f9SAndreas Gohr        }
101af1904f9SAndreas Gohr    }
102af1904f9SAndreas Gohr
1030a7415d3SAndreas Gohr}
1040a7415d3SAndreas Gohr
1050a7415d3SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 :
106