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 browser language if it matches with one of the configured 54 * languages 55 */ 56 function getBrowserLang(){ 57 $rx = '/(^|,|:|;|-)('.join('|',$this->trans).')($|,|:|;|-)/i'; 58 if(preg_match($rx,$_SERVER['HTTP_ACCEPT_LANGUAGE'],$match)){ 59 return strtolower($match[2]); 60 } 61 return false; 62 } 63 64 65 /** 66 * Returns the ID and name to the wanted translation, empty $lng is default lang 67 */ 68 function buildTransID($lng,$idpart){ 69 global $conf; 70 global $saved_conf; 71 if($lng){ 72 $link = ':'.$this->tns.$lng.':'.$idpart; 73 $name = $lng; 74 }else{ 75 $link = ':'.$this->tns.$idpart; 76 if(!$conf['lang_before_translation']){ 77 $name = $conf['lang']; 78 } else { 79 $name = $conf['lang_before_translation']; 80 } 81 } 82 return array($link,$name); 83 } 84 85 86 87} 88