xref: /plugin/autotranslation/helper.php (revision af1904f938cc047e8f90eb83a2552539e6802414)
1*af1904f9SAndreas Gohr<?php
2*af1904f9SAndreas Gohr/**
3*af1904f9SAndreas Gohr * Translation Plugin: Simple multilanguage plugin
4*af1904f9SAndreas Gohr *
5*af1904f9SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6*af1904f9SAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
7*af1904f9SAndreas Gohr */
8*af1904f9SAndreas Gohr
9*af1904f9SAndreas Gohr// must be run within Dokuwiki
10*af1904f9SAndreas Gohrif(!defined('DOKU_INC')) die();
11*af1904f9SAndreas Gohr
12*af1904f9SAndreas Gohr
13*af1904f9SAndreas Gohrclass helper_plugin_translation extends DokuWiki_Plugin {
14*af1904f9SAndreas Gohr    var $trans = array();
15*af1904f9SAndreas Gohr    var $tns   = '';
16*af1904f9SAndreas Gohr
17*af1904f9SAndreas Gohr    /**
18*af1904f9SAndreas Gohr     * Initialize
19*af1904f9SAndreas Gohr     */
20*af1904f9SAndreas Gohr    function helper_plugin_translation(){
21*af1904f9SAndreas Gohr        require_once(DOKU_INC.'inc/pageutils.php');
22*af1904f9SAndreas Gohr        require_once(DOKU_INC.'inc/utf8.php');
23*af1904f9SAndreas Gohr
24*af1904f9SAndreas Gohr        // load wanted translation into array
25*af1904f9SAndreas Gohr        $this->trans = strtolower(str_replace(',',' ',$this->getConf('translations')));
26*af1904f9SAndreas Gohr        $this->trans = array_unique(array_filter(explode(' ',$this->trans)));
27*af1904f9SAndreas Gohr        sort($this->trans);
28*af1904f9SAndreas Gohr        array_unshift($this->trans,'');
29*af1904f9SAndreas Gohr
30*af1904f9SAndreas Gohr        $this->tns = cleanID($this->getConf('translationns'));
31*af1904f9SAndreas Gohr        if($this->tns) $this->tns .= ':';
32*af1904f9SAndreas Gohr    }
33*af1904f9SAndreas Gohr
34*af1904f9SAndreas Gohr    /**
35*af1904f9SAndreas Gohr     * return some info
36*af1904f9SAndreas Gohr     */
37*af1904f9SAndreas Gohr    function getInfo(){
38*af1904f9SAndreas Gohr        return confToHash(dirname(__FILE__).'/info.txt');
39*af1904f9SAndreas Gohr    }
40*af1904f9SAndreas Gohr
41*af1904f9SAndreas Gohr    /**
42*af1904f9SAndreas Gohr     * Check if the given ID is a translation and return the language code.
43*af1904f9SAndreas Gohr     */
44*af1904f9SAndreas Gohr    function getLangPart($id){
45*af1904f9SAndreas Gohr        $rx = '/^'.$this->tns.'('.join('|',$this->trans).'):/';
46*af1904f9SAndreas Gohr        if(preg_match($rx,$id,$match)){
47*af1904f9SAndreas Gohr            return $match[1];
48*af1904f9SAndreas Gohr        }
49*af1904f9SAndreas Gohr        return '';
50*af1904f9SAndreas Gohr    }
51*af1904f9SAndreas Gohr
52*af1904f9SAndreas Gohr    /**
53*af1904f9SAndreas Gohr     * Returns the ID and name to the wanted translation, empty $lng is default lang
54*af1904f9SAndreas Gohr     */
55*af1904f9SAndreas Gohr    function buildTransID($lng,$idpart){
56*af1904f9SAndreas Gohr        global $conf;
57*af1904f9SAndreas Gohr        global $saved_conf;
58*af1904f9SAndreas Gohr        if($lng){
59*af1904f9SAndreas Gohr            $link = ':'.$this->tns.$lng.':'.$idpart;
60*af1904f9SAndreas Gohr            $name = $lng;
61*af1904f9SAndreas Gohr        }else{
62*af1904f9SAndreas Gohr            $link = ':'.$this->tns.$idpart;
63*af1904f9SAndreas Gohr            if(!$conf['lang_before_translation']){
64*af1904f9SAndreas Gohr              $name = $conf['lang'];
65*af1904f9SAndreas Gohr            } else {
66*af1904f9SAndreas Gohr              $name = $conf['lang_before_translation'];
67*af1904f9SAndreas Gohr            }
68*af1904f9SAndreas Gohr        }
69*af1904f9SAndreas Gohr        return array($link,$name);
70*af1904f9SAndreas Gohr    }
71*af1904f9SAndreas Gohr
72*af1904f9SAndreas Gohr
73*af1904f9SAndreas Gohr
74*af1904f9SAndreas Gohr}
75