openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

language.inc.php (5313B)


      1 <?php
      2 // OpenRat Content Management System
      3 // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
      4 //
      5 // This program is free software; you can redistribute it and/or
      6 // modify it under the terms of the GNU General Public License
      7 // as published by the Free Software Foundation; either version 2
      8 // of the License, or (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU General Public License
     16 // along with this program; if not, write to the Free Software
     17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     18 use logger\Logger;
     19 use util\ArrayUtils;
     20 use util\text\variables\VariableResolver;
     21 
     22 
     23 /**
     24  * Diese Funktion stellt ein Wort in der eingestellten
     25  * Sprache zur Verfuegung.
     26  *
     27  * @var String Name der Sprachvariablen
     28  * @var Array Liste (Assoziatives Array) von Variablen
     29  *
     30  * @package openrat.functions
     31  */
     32 function lang( $textVar,$vars = array() )
     33 {
     34 	global $conf;
     35 	$lang = $conf['language'];
     36 
     37 	$text = strtoupper($textVar);
     38 
     39 	// Abfrage, ob Textvariable vorhanden ist
     40 	if   ( isset( $lang[$text] ) )
     41 	{
     42 		$text = $lang[$text];
     43      	
     44 		// Fill in variables
     45 		if   ( $vars )
     46 		{
     47 			$resolver = new VariableResolver();
     48 
     49 			// Resolve variable
     50 			$resolver->addDefaultResolver(function ($var) use ($vars) {
     51 				return @$vars[$var];
     52 			});
     53 
     54 			$text = $resolver->resolveVariables( $text );
     55 		}
     56 
     57 		return $text;
     58 	}
     59 	
     60 	// Wenn Textvariable nicht vorhanden ist, dann als letzten Ausweg nur den Variablennamen zurueckgeben
     61 	Logger::warn('Message-Key not found: '.$textVar);
     62 	return( '?'.$textVar.'?' );
     63 }
     64 
     65 
     66 
     67 
     68 /**
     69  * Diese Funktion stellt ein Wort in der eingestellten
     70  * Sprache zur Verfuegung. Sonderzeichen werden als HTML maskiert.
     71  *
     72  * @var String Name der Sprachvariablen
     73  * @var Array Liste (Assoziatives Array) von Variablen
     74  *
     75  * @package openrat.functions
     76  * @param $key
     77  * @return unknown_type
     78  */
     79 function langHtml( $key,$vars = array() ) {
     80 
     81 	return encodeHtml( lang($key,$vars) );
     82 }
     83 
     84 /**
     85  * Ersetzt alle Zeichen mit dem Ordinalwert > 127 mit einer HTML-Maskierung.
     86  *
     87  * @return String
     88  */
     89 function encodeHtml($text)
     90 {
     91 	return translateutf8tohtml($text);
     92 }
     93 function escapeHtml($text)
     94 {
     95 	return translateutf8tohtml( htmlentities( $text ) );
     96 }
     97 
     98 
     99 
    100 
    101 // Source: http://de.php.net/manual/de/function.htmlentities.php#96648
    102 // Thx to silverbeat!
    103 // When using UTF-8 as a charset, htmlentities will only convert 1-byte and 2-byte characters.
    104 // Use this function if you also want to convert 3-byte and 4-byte characters:
    105 // converts a UTF8-string into HTML entities
    106     function translateutf8tohtml($txt) {
    107         //$txt = html_entity_decode($txt);
    108         $txt2 = '';
    109         for ($i=0;$i<strlen($txt);$i++) {
    110             $o = ord($txt{$i});
    111             if ($o<128) {
    112                 // 0..127: raw
    113                 $txt2 .= $txt{$i};
    114             } else {
    115                 $o1 = 0;
    116                 $o2 = 0;
    117                 $o3 = 0;
    118                 if ($i<strlen($txt)-1) $o1 = ord($txt{$i+1});
    119                 if ($i<strlen($txt)-2) $o2 = ord($txt{$i+2});
    120                 if ($i<strlen($txt)-3) $o3 = ord($txt{$i+3});
    121                 $hexval = 0;
    122                 if ($o>=0xc0 && $o<0xc2) {
    123                     // INVALID --- should never occur: 2-byte UTF-8 although value < 128
    124                     $hexval = $o1;
    125                     $i++;
    126                 } elseif ($o>=0xc2 && $o<0xe0 && $o1>=0x80) {
    127                     // 194..223: 2-byte UTF-8
    128                     $hexval |= ($o  & 0x1f) << 6;   // 1. byte: five bits of 1. char
    129                     $hexval |= ($o1 & 0x3f);   // 2. byte: six bits of 2. char
    130                     $i++;
    131                 } elseif ($o>=0xe0 && $o<0xf0 && $o1>=0x80 && $o2>=0x80) {
    132                     // 224..239: 3-byte UTF-8
    133                     $hexval |= ($o  & 0x0f) << 12;  // 1. byte: four bits of 1. char
    134                     $hexval |= ($o1 & 0x3f) << 6;  // 2.+3. byte: six bits of 2.+3. char
    135                     $hexval |= ($o2 & 0x3f);
    136                     $i += 2;
    137                 } elseif ($o>=0xf0 && $o<0xf4 && $o1>=0x80) {
    138                     // 240..244: 4-byte UTF-8
    139                     $hexval |= ($o  & 0x07) << 18; // 1. byte: three bits of 1. char
    140                     $hexval |= ($o1 & 0x3f) << 12; // 2.-4. byte: six bits of 2.-4. char
    141                     $hexval |= ($o2 & 0x3f) << 6;
    142                     $hexval |= ($o3 & 0x3f);
    143                     $i += 3;
    144                 } else {
    145                     // don't know ... just encode
    146                     $hexval = $o;
    147                 }
    148                 $hexstring = dechex($hexval);
    149                 if (strlen($hexstring)%2) $hexstring = '0' . $hexstring;
    150                 $txt2 .= '&#x' . $hexstring . ';';
    151             }
    152         }
    153         $result = $txt2;
    154 
    155         return $result;
    156     }
    157 
    158 
    159     
    160     
    161     
    162     
    163 
    164 /**
    165  * Diese Funktion prueft, ob ein Sprachelement vorhanden ist
    166  *
    167  * @var String Name der Sprachvariablen
    168  *
    169  * @package openrat.functions
    170  */
    171 function hasLang( $text )
    172 {
    173 	$text = strtoupper($text);
    174 
    175 	global $conf;
    176 	$lang = $conf['language'];
    177 	
    178 	return isset( $lang[$text] );
    179 }
    180 
    181 
    182 ?>