xref: /plugin/autotranslation/helper.php (revision 7c54a0a6f4b58f04f321a1c207595d1ed359224f)
1af1904f9SAndreas Gohr<?php
2af1904f9SAndreas Gohr/**
3af1904f9SAndreas Gohr * Translation Plugin: Simple multilanguage plugin
4af1904f9SAndreas Gohr *
5af1904f9SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6af1904f9SAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
7af1904f9SAndreas Gohr */
8af1904f9SAndreas Gohr
9af1904f9SAndreas Gohr// must be run within Dokuwiki
10af1904f9SAndreas Gohrif(!defined('DOKU_INC')) die();
11af1904f9SAndreas Gohr
12af1904f9SAndreas Gohr
13af1904f9SAndreas Gohrclass helper_plugin_translation extends DokuWiki_Plugin {
14af1904f9SAndreas Gohr    var $trans       = array();
15af1904f9SAndreas Gohr    var $tns         = '';
16*7c54a0a6SAndreas Gohr    var $defaultlang = '';
17af1904f9SAndreas Gohr
18af1904f9SAndreas Gohr    /**
19af1904f9SAndreas Gohr     * Initialize
20af1904f9SAndreas Gohr     */
21af1904f9SAndreas Gohr    function helper_plugin_translation(){
22*7c54a0a6SAndreas Gohr        global $conf;
23af1904f9SAndreas Gohr        require_once(DOKU_INC.'inc/pageutils.php');
24af1904f9SAndreas Gohr        require_once(DOKU_INC.'inc/utf8.php');
25af1904f9SAndreas Gohr
26af1904f9SAndreas Gohr        // load wanted translation into array
27af1904f9SAndreas Gohr        $this->trans = strtolower(str_replace(',',' ',$this->getConf('translations')));
28af1904f9SAndreas Gohr        $this->trans = array_unique(array_filter(explode(' ',$this->trans)));
29af1904f9SAndreas Gohr        sort($this->trans);
30*7c54a0a6SAndreas Gohr
31*7c54a0a6SAndreas Gohr        // get default translation
32*7c54a0a6SAndreas Gohr        if(!$conf['lang_before_translation']){
33*7c54a0a6SAndreas Gohr          $dfl = $conf['lang'];
34*7c54a0a6SAndreas Gohr        } else {
35*7c54a0a6SAndreas Gohr          $dfl = $conf['lang_before_translation'];
36*7c54a0a6SAndreas Gohr        }
37*7c54a0a6SAndreas Gohr        if(in_array($dfl,$this->trans)){
38*7c54a0a6SAndreas Gohr            $this->defaultlang = $dfl;
39*7c54a0a6SAndreas Gohr        }else{
40*7c54a0a6SAndreas Gohr            $this->defaultlang = '';
41af1904f9SAndreas Gohr            array_unshift($this->trans,'');
42*7c54a0a6SAndreas Gohr        }
43*7c54a0a6SAndreas Gohr
44af1904f9SAndreas Gohr
45af1904f9SAndreas Gohr        $this->tns = cleanID($this->getConf('translationns'));
46af1904f9SAndreas Gohr        if($this->tns) $this->tns .= ':';
47af1904f9SAndreas Gohr    }
48af1904f9SAndreas Gohr
49af1904f9SAndreas Gohr    /**
50af1904f9SAndreas Gohr     * return some info
51af1904f9SAndreas Gohr     */
52af1904f9SAndreas Gohr    function getInfo(){
53af1904f9SAndreas Gohr        return confToHash(dirname(__FILE__).'/info.txt');
54af1904f9SAndreas Gohr    }
55af1904f9SAndreas Gohr
56af1904f9SAndreas Gohr    /**
57af1904f9SAndreas Gohr     * Check if the given ID is a translation and return the language code.
58af1904f9SAndreas Gohr     */
59af1904f9SAndreas Gohr    function getLangPart($id){
60af1904f9SAndreas Gohr        $rx = '/^'.$this->tns.'('.join('|',$this->trans).'):/';
61af1904f9SAndreas Gohr        if(preg_match($rx,$id,$match)){
62af1904f9SAndreas Gohr            return $match[1];
63af1904f9SAndreas Gohr        }
64af1904f9SAndreas Gohr        return '';
65af1904f9SAndreas Gohr    }
66af1904f9SAndreas Gohr
67af1904f9SAndreas Gohr    /**
687053cd66SAndreas Gohr     * Returns the browser language if it matches with one of the configured
697053cd66SAndreas Gohr     * languages
707053cd66SAndreas Gohr     */
717053cd66SAndreas Gohr    function getBrowserLang(){
727053cd66SAndreas Gohr        $rx = '/(^|,|:|;|-)('.join('|',$this->trans).')($|,|:|;|-)/i';
737053cd66SAndreas Gohr        if(preg_match($rx,$_SERVER['HTTP_ACCEPT_LANGUAGE'],$match)){
747053cd66SAndreas Gohr            return strtolower($match[2]);
757053cd66SAndreas Gohr        }
767053cd66SAndreas Gohr        return false;
777053cd66SAndreas Gohr    }
787053cd66SAndreas Gohr
797053cd66SAndreas Gohr
807053cd66SAndreas Gohr    /**
81*7c54a0a6SAndreas Gohr     * Returns the ID and name to the wanted translation, empty
82*7c54a0a6SAndreas Gohr     * $lng is default lang
83af1904f9SAndreas Gohr     */
84af1904f9SAndreas Gohr    function buildTransID($lng,$idpart){
85af1904f9SAndreas Gohr        global $conf;
86af1904f9SAndreas Gohr        global $saved_conf;
87af1904f9SAndreas Gohr        if($lng){
88af1904f9SAndreas Gohr            $link = ':'.$this->tns.$lng.':'.$idpart;
89af1904f9SAndreas Gohr            $name = $lng;
90af1904f9SAndreas Gohr        }else{
91af1904f9SAndreas Gohr            $link = ':'.$this->tns.$idpart;
92af1904f9SAndreas Gohr            if(!$conf['lang_before_translation']){
93af1904f9SAndreas Gohr              $name = $conf['lang'];
94af1904f9SAndreas Gohr            } else {
95af1904f9SAndreas Gohr              $name = $conf['lang_before_translation'];
96af1904f9SAndreas Gohr            }
97af1904f9SAndreas Gohr        }
98af1904f9SAndreas Gohr        return array($link,$name);
99af1904f9SAndreas Gohr    }
100af1904f9SAndreas Gohr
1011469199dSAndreas Gohr    /**
1021469199dSAndreas Gohr     * Displays the available and configured translations. Needs to be placed in the template.
1031469199dSAndreas Gohr     */
1041469199dSAndreas Gohr    function showTranslations(){
1051469199dSAndreas Gohr        global $ACT;
1061469199dSAndreas Gohr        global $ID;
1071469199dSAndreas Gohr        global $conf;
1081469199dSAndreas Gohr        global $INFO;
1091469199dSAndreas Gohr
1101469199dSAndreas Gohr        if($ACT != 'show') return;
1111469199dSAndreas Gohr        if($this->tns && strpos($ID,$this->tns) !== 0) return;
1121469199dSAndreas Gohr        $skiptrans = trim($this->getConf('skiptrans'));
1131469199dSAndreas Gohr        if($skiptrans &&  preg_match('/'.$skiptrans.'/ui',':'.$ID)) return;
1141469199dSAndreas Gohr        $meta = p_get_metadata($ID);
1151469199dSAndreas Gohr        if($meta['plugin']['translation']['notrans']) return;
1161469199dSAndreas Gohr
1171469199dSAndreas Gohr        $rx = '/^'.$this->tns.'(('.join('|',$this->trans).'):)?/';
1181469199dSAndreas Gohr        $idpart = preg_replace($rx,'',$ID);
1191469199dSAndreas Gohr
1201469199dSAndreas Gohr        $out  = '<div class="plugin_translation">';
1211469199dSAndreas Gohr        $out .= '<span>'.$this->getLang('translations');
1221469199dSAndreas Gohr        if($this->getConf('about')){
1231469199dSAndreas Gohr            $out .= '<sup>'.html_wikilink($this->getConf('about'),'?').'</sup>';
1241469199dSAndreas Gohr        }
1251469199dSAndreas Gohr        $out .= ':</span> ';
1261469199dSAndreas Gohr
1271469199dSAndreas Gohr        if($this->getConf('dropdown')){ // use dropdown
1281469199dSAndreas Gohr            if($INFO['exists']){
1291469199dSAndreas Gohr                $class = 'wikilink1';
1301469199dSAndreas Gohr            }else{
1311469199dSAndreas Gohr                $class = 'wikilink2';
1321469199dSAndreas Gohr            }
1331469199dSAndreas Gohr            $out .= '<form action="'.wl().'" id="translation__dropdown">';
1341469199dSAndreas Gohr            $out .= '<select name="id" class="'.$class.'">';
1351469199dSAndreas Gohr            foreach($this->trans as $t){
1361469199dSAndreas Gohr                list($link,$name) = $this->buildTransID($t,$idpart);
1371469199dSAndreas Gohr                $link = cleanID($link);
1381469199dSAndreas Gohr                if($ID == $link){
1391469199dSAndreas Gohr                    $sel = ' selected="selected"';
1401469199dSAndreas Gohr                }else{
1411469199dSAndreas Gohr                    $sel = '';
1421469199dSAndreas Gohr                }
1431469199dSAndreas Gohr                if(page_exists($link,'',false)){
1441469199dSAndreas Gohr                    $class = 'wikilink1';
1451469199dSAndreas Gohr                }else{
1461469199dSAndreas Gohr                    $class = 'wikilink2';
1471469199dSAndreas Gohr                }
1481469199dSAndreas Gohr                $out .= '<option value="'.$link.'"'.$sel.' class="'.$class.'">'.hsc($name).'</option>';
1491469199dSAndreas Gohr            }
1501469199dSAndreas Gohr            $out .= '</select>';
1511469199dSAndreas Gohr            $out .= '<input name="go" type="submit" value="&rarr;" />';
1521469199dSAndreas Gohr            $out .= '</form>';
1531469199dSAndreas Gohr        }else{ // use list
1541469199dSAndreas Gohr            $out .= '<ul>';
1551469199dSAndreas Gohr            foreach($this->trans as $t){
1561469199dSAndreas Gohr                list($link,$name) = $this->buildTransID($t,$idpart);
1571469199dSAndreas Gohr                $out .= '  <li><div class="li">'.html_wikilink($link,$name).'</div></li>';
1581469199dSAndreas Gohr            }
1591469199dSAndreas Gohr            $out .= '</ul>';
1601469199dSAndreas Gohr        }
1611469199dSAndreas Gohr
1621469199dSAndreas Gohr        $out .= '</div>';
1631469199dSAndreas Gohr
1641469199dSAndreas Gohr
1651469199dSAndreas Gohr        return $out;
1661469199dSAndreas Gohr    }
167af1904f9SAndreas Gohr
168af1904f9SAndreas Gohr
169af1904f9SAndreas Gohr}
170