1<?php
2/**
3 * Multilingual Plugin: Simple multilanguage plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Daniel Stonier <d.stonier@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'action.php');
14
15class action_plugin_multilingual extends DokuWiki_Action_Plugin {
16
17    /**
18     * for th helper plugin
19     */
20    var $hlp = null;
21
22    /**
23     * Constructor. Load helper plugin
24     */
25    function action_plugin_multilingual(){
26        $this->hlp =& plugin_load('helper', 'multilingual');
27    }
28
29    /**
30     * return some info
31     */
32    function getInfo(){
33        return confToHash(dirname(__FILE__).'/info.txt');
34    }
35
36    /**
37     * Register the events
38     */
39    function register(Doku_Event_Handler $controller) {
40        if($this->getConf('start_redirect')) {
41            $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'multilingual_start');
42	}
43        if($this->getConf('use_browser_lang')) {
44            $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'multilingual_ui');
45        }
46    }
47
48    function multilingual_start($event, $args) {
49        global $conf;
50        global $ACT;
51
52        if ( $ACT == 'login' ) {
53            if (($_SERVER['REMOTE_USER']!=null)&&($_REQUEST['do']=='login')) {
54                header('Location: doku.php?id='.$conf['lang'].':'.$conf['start']);
55	        die();
56	    }
57        }
58    }
59    /**
60     * Change the UI language depending on your browser's current selection.
61     */
62    function multilingual_ui(Doku_Event $event, $args) {
63        global $ID;
64        global $lang;
65        global $conf;
66
67	$enabled_languages = preg_split("/ /", $this->getConf('enabled_langs') );
68	$languages = preg_split("/,/", preg_replace('/\(;q=\d+\.\d+\)/i', '', getenv('HTTP_ACCEPT_LANGUAGE')));
69	// Could use a check here against the dokuwiki's supported languages
70	$old_language = $conf['lang'];
71	foreach ($languages as $language) {
72	    if (in_array($language, $enabled_languages)) {
73	        $conf['lang'] = $language;
74	        break;
75	    }
76	}
77	// Rebuild language array if necessary
78	if ( $old_language != $conf['lang'] ) {
79	    $lang = array();
80	    require_once(DOKU_INC.'inc/lang/en/lang.php');
81	    if ( $conf['lang'] && $conf['lang'] != 'en' ) {
82	        require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
83	    }
84	}
85        return true;
86    }
87}
88
89//Setup VIM: ex: et ts=4 enc=utf-8 :
90