1<?php
2/**
3 * Translation2 Plugin
4 *
5 * @license    GPLv3 (http://www.gnu.org/licenses/gpl.html)
6 * @link       http://www.dokuwiki.org/plugin:translation2
7 * @author     Markus Birth <markus@birth-online.de>
8 */
9
10if(!defined('DOKU_INC')) die();
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'action.php');
13
14class action_plugin_translation2 extends DokuWiki_Action_Plugin {
15
16    /**
17     * return some info
18     */
19    function getInfo(){
20        return confToHash(dirname(__FILE__).'/INFO.txt');
21    }
22
23    /*
24     * plugin should use this method to register its handlers with the dokuwiki's event controller
25     */
26    function register(&$controller) {
27        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, '_prepare');
28        $controller->register_hook('JQUERY_READY', 'BEFORE', $this, '_addjs');
29    }
30
31    function _prepare(&$event, $param) {
32        global $conf, $ID;
33        $this->loadConfig();
34        // Configuration
35
36        // Autodetect all languages your dokuwiki installation supports
37        $supportedLanguages = glob(DOKU_INC . 'inc/lang/*', GLOB_ONLYDIR);
38
39        foreach ($supportedLanguages as $idx=>$lang) $supportedLanguages[$idx] = basename($lang);
40
41        if (strlen(trim($this->getConf('languages'))) == 0) {
42            $this->conf['languages'] = implode(',', $supportedLanguages);
43        }
44
45        // make sure, default lang is valid
46        if (strpos($this->getConf('languages'), $this->getConf('default_language')) === false) $this->conf['default_language'] = trim(reset(explode(',', $this->getConf('languages'))));
47
48
49        // TODO: Check whether this code still works!
50        // Set default language to the user agent's most favorite one
51        $browserLanguages = split(',', preg_replace('/(;q=\d+.\d+)/i', '', $_SERVER['HTTP_ACCEPT_LANGUAGE']));
52        foreach ($browserLanguages as $blang) {
53            if (strpos($this->getConf('languages'), $blang) !== false) {
54                $conf['lang'] = $blang;
55                break;
56            }
57        }
58
59        // Check, if language is set by namespace and overwrite chosen language
60        $lang = preg_replace('/^(..+?)[:\/].*/i', '$1', $ID);
61        if (strpos($this->getConf('languages'), $lang) === false) $lang = preg_replace('/^(..+?)[:\/].*/i', '$1', $_REQUEST['ns']);
62          else $conf['lang'] = $lang;
63
64        // prepend default start page with language namespace, if this page already exists.
65        //if (file_exists($conf['savedir'].'/pages/'.str_replace(':','/',$conf['lang'].':'.$conf['start']).'.txt')) $conf['start'] = $conf['lang'].':'.$conf['start'];
66    }
67
68    function _addjs(&$event, $param) {
69        global $ID;
70        $event->data[] = 'jQuery(\'DIV#bar__topleft\').append(\'' . $this->html_langswitcher() . '\');';
71
72        if ($ID{2} == ':') {
73            // translated page ~~> remove first youarehere()
74            // TODO: Remove the "&raquo;" #text node from DOM (might not be in all templates!)
75            $event->data[] = 'jQuery(\'DIV.breadcrumbs:first A:first\').remove();';
76        }
77    }
78
79    function flagimg($lang) {
80        // translate language-code to country-code (where the lang is mostly spoken/written)
81        switch ($lang) {
82            case 'da': $lang = 'dk'; break;  // Danish -> Denmark
83            case 'el': $lang = 'gr'; break;  // Greek -> Greece
84            case 'en': $lang = 'en-all'; break; // English -> combined GB/US flag
85            case 'he': $lang = 'il'; break;  // Hebrew -> Israel
86            case 'ja': $lang = 'jp'; break;  // Japanese -> Japan
87            case 'ko': $lang = 'kr'; break;  // Korean -> Korea
88            case 'ku': $lang = 'iq'; break;  // Kurdish -> Iraq
89            case 'pt-br': $lang = 'br'; break;  // Braz. Portuguese -> Brazil
90            case 'uk': $lang = 'ua'; break;  // Ukrainian -> Ukraine
91            case 'zh': $lang = 'cn'; break;  // Simpl. Chinese -> China
92            case 'zh-tw': $lang = 'tw'; break;  // Trad. Chinese -> Taiwan
93            default: break;
94        }
95        $img = DOKU_BASE . 'lib/plugins/' . basename(dirname(__FILE__)) . '/flags/' . $lang . '.gif';
96        return $img;
97    }
98
99    // assumes Flag icons (.gif) from http://www.famfamfam.com/lab/icons/flags/ in flags folder (rename us.gif or gb.gif to en-all.gif)
100    function html_langswitcher() {
101        global $ID, $conf;
102        $result  = ' <div id="langswitcher" class="no">';
103        $result .= '<ul style="list-style: none; display: inline;">';
104        $languages = explode(',', $this->getConf('languages'));
105        array_walk($languages, 'trim');
106        foreach ($languages as $lang) {
107            $thislang = ($conf['lang']==$lang);
108            $newID    = (($lang!=$this->getConf('default_language'))?$lang.':':'') . (($ID{2}==':')?substr($ID, 3):$ID);
109            $newFile  = wikiFN($newID);
110            $img      = '<img src="' . $this->flagimg($lang) . '" alt="' . hsc($lang) . '" title="' . hsc($lang) . '" />';
111            $class = '';
112            if (!$thislang) {
113                $class .= ' lang';
114                if (file_exists($newFile)) $class .= ' translated';
115            }
116            $result .= '<li style="display: inline; padding: 2px;"' . ((!empty($class))?' class="'.trim($class).'"':'') . '>';
117            if (!$thislang) {
118                $result .= '<a href="' . wl($newID) . '">' . $img . '</a>';
119            } else {
120                $result .= $img;
121            }
122            $result .= '</li>';
123        }
124        $result .= '</ul>';
125        $result .= '</div>';
126        return $result;
127    }
128}