xref: /plugin/autotranslation/helper.php (revision 7053cd66796b13d1f30019dd1d4b008f5d2a9fb8)
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   = '';
16af1904f9SAndreas Gohr
17af1904f9SAndreas Gohr    /**
18af1904f9SAndreas Gohr     * Initialize
19af1904f9SAndreas Gohr     */
20af1904f9SAndreas Gohr    function helper_plugin_translation(){
21af1904f9SAndreas Gohr        require_once(DOKU_INC.'inc/pageutils.php');
22af1904f9SAndreas Gohr        require_once(DOKU_INC.'inc/utf8.php');
23af1904f9SAndreas Gohr
24af1904f9SAndreas Gohr        // load wanted translation into array
25af1904f9SAndreas Gohr        $this->trans = strtolower(str_replace(',',' ',$this->getConf('translations')));
26af1904f9SAndreas Gohr        $this->trans = array_unique(array_filter(explode(' ',$this->trans)));
27af1904f9SAndreas Gohr        sort($this->trans);
28af1904f9SAndreas Gohr        array_unshift($this->trans,'');
29af1904f9SAndreas Gohr
30af1904f9SAndreas Gohr        $this->tns = cleanID($this->getConf('translationns'));
31af1904f9SAndreas Gohr        if($this->tns) $this->tns .= ':';
32af1904f9SAndreas Gohr    }
33af1904f9SAndreas Gohr
34af1904f9SAndreas Gohr    /**
35af1904f9SAndreas Gohr     * return some info
36af1904f9SAndreas Gohr     */
37af1904f9SAndreas Gohr    function getInfo(){
38af1904f9SAndreas Gohr        return confToHash(dirname(__FILE__).'/info.txt');
39af1904f9SAndreas Gohr    }
40af1904f9SAndreas Gohr
41af1904f9SAndreas Gohr    /**
42af1904f9SAndreas Gohr     * Check if the given ID is a translation and return the language code.
43af1904f9SAndreas Gohr     */
44af1904f9SAndreas Gohr    function getLangPart($id){
45af1904f9SAndreas Gohr        $rx = '/^'.$this->tns.'('.join('|',$this->trans).'):/';
46af1904f9SAndreas Gohr        if(preg_match($rx,$id,$match)){
47af1904f9SAndreas Gohr            return $match[1];
48af1904f9SAndreas Gohr        }
49af1904f9SAndreas Gohr        return '';
50af1904f9SAndreas Gohr    }
51af1904f9SAndreas Gohr
52af1904f9SAndreas Gohr    /**
53*7053cd66SAndreas Gohr     * Returns the browser language if it matches with one of the configured
54*7053cd66SAndreas Gohr     * languages
55*7053cd66SAndreas Gohr     */
56*7053cd66SAndreas Gohr    function getBrowserLang(){
57*7053cd66SAndreas Gohr        $rx = '/(^|,|:|;|-)('.join('|',$this->trans).')($|,|:|;|-)/i';
58*7053cd66SAndreas Gohr        if(preg_match($rx,$_SERVER['HTTP_ACCEPT_LANGUAGE'],$match)){
59*7053cd66SAndreas Gohr            return strtolower($match[2]);
60*7053cd66SAndreas Gohr        }
61*7053cd66SAndreas Gohr        return false;
62*7053cd66SAndreas Gohr    }
63*7053cd66SAndreas Gohr
64*7053cd66SAndreas Gohr
65*7053cd66SAndreas Gohr    /**
66af1904f9SAndreas Gohr     * Returns the ID and name to the wanted translation, empty $lng is default lang
67af1904f9SAndreas Gohr     */
68af1904f9SAndreas Gohr    function buildTransID($lng,$idpart){
69af1904f9SAndreas Gohr        global $conf;
70af1904f9SAndreas Gohr        global $saved_conf;
71af1904f9SAndreas Gohr        if($lng){
72af1904f9SAndreas Gohr            $link = ':'.$this->tns.$lng.':'.$idpart;
73af1904f9SAndreas Gohr            $name = $lng;
74af1904f9SAndreas Gohr        }else{
75af1904f9SAndreas Gohr            $link = ':'.$this->tns.$idpart;
76af1904f9SAndreas Gohr            if(!$conf['lang_before_translation']){
77af1904f9SAndreas Gohr              $name = $conf['lang'];
78af1904f9SAndreas Gohr            } else {
79af1904f9SAndreas Gohr              $name = $conf['lang_before_translation'];
80af1904f9SAndreas Gohr            }
81af1904f9SAndreas Gohr        }
82af1904f9SAndreas Gohr        return array($link,$name);
83af1904f9SAndreas Gohr    }
84af1904f9SAndreas Gohr
85af1904f9SAndreas Gohr
86af1904f9SAndreas Gohr
87af1904f9SAndreas Gohr}
88