xref: /plugin/autotranslation/helper.php (revision 7c54a0a6f4b58f04f321a1c207595d1ed359224f)
1<?php
2/**
3 * Translation Plugin: Simple multilanguage plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12
13class helper_plugin_translation extends DokuWiki_Plugin {
14    var $trans       = array();
15    var $tns         = '';
16    var $defaultlang = '';
17
18    /**
19     * Initialize
20     */
21    function helper_plugin_translation(){
22        global $conf;
23        require_once(DOKU_INC.'inc/pageutils.php');
24        require_once(DOKU_INC.'inc/utf8.php');
25
26        // load wanted translation into array
27        $this->trans = strtolower(str_replace(',',' ',$this->getConf('translations')));
28        $this->trans = array_unique(array_filter(explode(' ',$this->trans)));
29        sort($this->trans);
30
31        // get default translation
32        if(!$conf['lang_before_translation']){
33          $dfl = $conf['lang'];
34        } else {
35          $dfl = $conf['lang_before_translation'];
36        }
37        if(in_array($dfl,$this->trans)){
38            $this->defaultlang = $dfl;
39        }else{
40            $this->defaultlang = '';
41            array_unshift($this->trans,'');
42        }
43
44
45        $this->tns = cleanID($this->getConf('translationns'));
46        if($this->tns) $this->tns .= ':';
47    }
48
49    /**
50     * return some info
51     */
52    function getInfo(){
53        return confToHash(dirname(__FILE__).'/info.txt');
54    }
55
56    /**
57     * Check if the given ID is a translation and return the language code.
58     */
59    function getLangPart($id){
60        $rx = '/^'.$this->tns.'('.join('|',$this->trans).'):/';
61        if(preg_match($rx,$id,$match)){
62            return $match[1];
63        }
64        return '';
65    }
66
67    /**
68     * Returns the browser language if it matches with one of the configured
69     * languages
70     */
71    function getBrowserLang(){
72        $rx = '/(^|,|:|;|-)('.join('|',$this->trans).')($|,|:|;|-)/i';
73        if(preg_match($rx,$_SERVER['HTTP_ACCEPT_LANGUAGE'],$match)){
74            return strtolower($match[2]);
75        }
76        return false;
77    }
78
79
80    /**
81     * Returns the ID and name to the wanted translation, empty
82     * $lng is default lang
83     */
84    function buildTransID($lng,$idpart){
85        global $conf;
86        global $saved_conf;
87        if($lng){
88            $link = ':'.$this->tns.$lng.':'.$idpart;
89            $name = $lng;
90        }else{
91            $link = ':'.$this->tns.$idpart;
92            if(!$conf['lang_before_translation']){
93              $name = $conf['lang'];
94            } else {
95              $name = $conf['lang_before_translation'];
96            }
97        }
98        return array($link,$name);
99    }
100
101    /**
102     * Displays the available and configured translations. Needs to be placed in the template.
103     */
104    function showTranslations(){
105        global $ACT;
106        global $ID;
107        global $conf;
108        global $INFO;
109
110        if($ACT != 'show') return;
111        if($this->tns && strpos($ID,$this->tns) !== 0) return;
112        $skiptrans = trim($this->getConf('skiptrans'));
113        if($skiptrans &&  preg_match('/'.$skiptrans.'/ui',':'.$ID)) return;
114        $meta = p_get_metadata($ID);
115        if($meta['plugin']['translation']['notrans']) return;
116
117        $rx = '/^'.$this->tns.'(('.join('|',$this->trans).'):)?/';
118        $idpart = preg_replace($rx,'',$ID);
119
120        $out  = '<div class="plugin_translation">';
121        $out .= '<span>'.$this->getLang('translations');
122        if($this->getConf('about')){
123            $out .= '<sup>'.html_wikilink($this->getConf('about'),'?').'</sup>';
124        }
125        $out .= ':</span> ';
126
127        if($this->getConf('dropdown')){ // use dropdown
128            if($INFO['exists']){
129                $class = 'wikilink1';
130            }else{
131                $class = 'wikilink2';
132            }
133            $out .= '<form action="'.wl().'" id="translation__dropdown">';
134            $out .= '<select name="id" class="'.$class.'">';
135            foreach($this->trans as $t){
136                list($link,$name) = $this->buildTransID($t,$idpart);
137                $link = cleanID($link);
138                if($ID == $link){
139                    $sel = ' selected="selected"';
140                }else{
141                    $sel = '';
142                }
143                if(page_exists($link,'',false)){
144                    $class = 'wikilink1';
145                }else{
146                    $class = 'wikilink2';
147                }
148                $out .= '<option value="'.$link.'"'.$sel.' class="'.$class.'">'.hsc($name).'</option>';
149            }
150            $out .= '</select>';
151            $out .= '<input name="go" type="submit" value="&rarr;" />';
152            $out .= '</form>';
153        }else{ // use list
154            $out .= '<ul>';
155            foreach($this->trans as $t){
156                list($link,$name) = $this->buildTransID($t,$idpart);
157                $out .= '  <li><div class="li">'.html_wikilink($link,$name).'</div></li>';
158            }
159            $out .= '</ul>';
160        }
161
162        $out .= '</div>';
163
164
165        return $out;
166    }
167
168
169}
170