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