xref: /plugin/autotranslation/action.php (revision af1904f938cc047e8f90eb83a2552539e6802414)
10a7415d3SAndreas Gohr<?php
20a7415d3SAndreas Gohr/**
3*af1904f9SAndreas 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    /**
19*af1904f9SAndreas Gohr     * for th helper plugin
20*af1904f9SAndreas Gohr     */
21*af1904f9SAndreas Gohr    var $hlp = null;
22*af1904f9SAndreas Gohr
23*af1904f9SAndreas Gohr    /**
24*af1904f9SAndreas Gohr     * Constructor. Load helper plugin
25*af1904f9SAndreas Gohr     */
26*af1904f9SAndreas Gohr    function action_plugin_translation(){
27*af1904f9SAndreas Gohr        $this->hlp =& plugin_load('helper', 'translation');
28*af1904f9SAndreas Gohr    }
29*af1904f9SAndreas Gohr
30*af1904f9SAndreas 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        }
45*af1904f9SAndreas 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;
550a7415d3SAndreas Gohr
560a7415d3SAndreas Gohr        // check if we are in a foreign language namespace
57*af1904f9SAndreas Gohr        $lc = $this->hlp->getLangPart($ID);
580a7415d3SAndreas Gohr        if(!$lc) return;
590a7415d3SAndreas Gohr
600a7415d3SAndreas Gohr        if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) {
610a7415d3SAndreas Gohr          require(DOKU_INC.'inc/lang/'.$lc.'/lang.php');
620a7415d3SAndreas Gohr        }
630a7415d3SAndreas Gohr        $conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin
640a7415d3SAndreas Gohr        $conf['lang'] = $lc;
650a7415d3SAndreas Gohr
660a7415d3SAndreas Gohr        return true;
670a7415d3SAndreas Gohr    }
68*af1904f9SAndreas Gohr
69*af1904f9SAndreas Gohr    /**
70*af1904f9SAndreas Gohr     * Resort page match results so that results are ordered by translation, having the
71*af1904f9SAndreas Gohr     * default language first
72*af1904f9SAndreas Gohr     */
73*af1904f9SAndreas Gohr    function translation_search(&$event, $args) {
74*af1904f9SAndreas Gohr        // sort into translation slots
75*af1904f9SAndreas Gohr        $res = array();
76*af1904f9SAndreas Gohr        foreach($event->result as $r){
77*af1904f9SAndreas Gohr            $tr = $this->hlp->getLangPart($r);
78*af1904f9SAndreas Gohr            if(!is_array($res["x$tr"])) $res["x$tr"] = array();
79*af1904f9SAndreas Gohr            $res["x$tr"][] = $r;
80*af1904f9SAndreas Gohr        }
81*af1904f9SAndreas Gohr        // sort by translations
82*af1904f9SAndreas Gohr        ksort($res);
83*af1904f9SAndreas Gohr        // combine
84*af1904f9SAndreas Gohr        $event->result = array();
85*af1904f9SAndreas Gohr        foreach($res as $r){
86*af1904f9SAndreas Gohr            $event->result = array_merge($event->result,$r);
87*af1904f9SAndreas Gohr        }
88*af1904f9SAndreas Gohr    }
89*af1904f9SAndreas Gohr
900a7415d3SAndreas Gohr}
910a7415d3SAndreas Gohr
920a7415d3SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 :
93