1<?php
2/**
3 * Language by namespace: Set the UI language by page language, or by namespace, or by browser language
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Viktor Söderqvist <viktor@zuiderkwast.se>
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_uilanguage extends DokuWiki_Action_Plugin {
16
17    /**
18     * Register the events
19     */
20    public function register(Doku_Event_Handler $controller) {
21        $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'changeUiLanguage');
22        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handleMetaheaderOutput');
23    }
24
25    /**
26     * Sets the internal language and reloads the language files.
27     */
28    public function changeUiLanguage($event, $args) {
29        global $conf, $INFO, $ID, $lang;
30        $old_language = $conf['lang'];
31
32        do { // search for a usable language; break when found
33
34            // 1. Language metadata of the page - the best choice if set
35            if (!empty($INFO['meta']['language']) && $this->langOK($INFO['meta']['language'])) {
36                $conf['lang'] = $INFO['meta']['language'];
37                break;
38            }
39            // 2. Language by namespace (first part)
40            if (strlen($ID) > 3 && $ID[2] == ':') {
41                $l = substr($ID, 0, 2);
42                if ($this->langOK($l)) {
43                    $conf['lang'] = $l;
44                    break;
45                }
46            }
47            // 3. Set the UI language to one of the browser's accepted languages
48            $languages = explode(',', preg_replace('/\(;q=\d+\.\d+\)/i', '', getenv('HTTP_ACCEPT_LANGUAGE')));
49            foreach ($languages as $l) {
50                if ($this->langOK($l)) {
51                    $conf['lang'] = $l;
52                    break;
53                }
54            }
55        } while(false);
56
57        // Rebuild the language array if necessary
58        if ($old_language != $conf['lang']) {
59            $lang = array();
60            require(DOKU_INC.'inc/lang/en/lang.php');
61            if ($conf['lang'] && $conf['lang'] != 'en') {
62                require(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
63            }
64        }
65    }
66
67    /** Check if a string is a valid language code, using the languages of DokuWiki itself */
68    private function langOK($lang) {
69        return file_exists(DOKU_INC."inc/lang/$lang/lang.php");
70    }
71
72    /**
73     * Intercept the CSS links. Since we might have changed the language direction to RTL, we might
74     * want to include RTL styles in the CSS.
75     */
76    public function handleMetaheaderOutput($event, $param) {
77        global $lang, $conf;
78        if ($lang['direction'] != 'rtl') return;
79        $head = & $event->data;
80        $links = & $head['link'];
81        $pluginname = $this->getPluginName();
82        for ($i=0; $i < count($links); $i++) {
83            $link = & $links[$i];
84            //if ($link['rel']=='stylesheet') msg('Media '.$link['media'].': '.$link['href']);
85            if ($link['rel']=='stylesheet' && $link['type']=='text/css') {
86                $link['href'] = DOKU_BASE.'lib/plugins/'.$pluginname.'/rtlcss.php?s='.$link['media'].'&t='.$conf['template'];
87            }
88        }
89    }
90}
91