1<?php 2/** 3 * __vbox_language class and trans() function 4 * 5 * @author Ian Moore (imoore76 at yahoo dot com) 6 * @copyright Copyright (C) 2010-2015 Ian Moore (imoore76 at yahoo dot com) 7 * @version $Id: language.php 591 2015-04-11 22:40:47Z imoore76 $ 8 * @see languages/languages.txt 9 * @package phpVirtualBox 10 * 11 */ 12 13global $_vbox_language; 14 15// Settings contains language 16require_once(dirname(__FILE__).'/config.php'); 17require_once(dirname(__FILE__).'/utils.php'); 18 19define('VBOX_BASE_LANG_DIR', dirname(dirname(dirname(__FILE__))) .'/languages'); 20 21/** 22 * Language class. Parses language file and stores translations in an array. 23 * 24 * @author Ian Moore (imoore76 at yahoo dot com) 25 * @copyright Copyright (C) 2010-2015 Ian Moore (imoore76 at yahoo dot com) 26 * @version $Id: language.php 591 2015-04-11 22:40:47Z imoore76 $ 27 * @package phpVirtualBox 28 * 29 * @global __vbox_language $GLOBALS['_vbox_language'] global __vbox_language instance 30 * @name $_vbox_language 31*/ 32class __vbox_language { 33 34 /** 35 * Static language data used for translations 36 * @var array 37 */ 38 static $langdata = null; 39 40 /** 41 * 42 * Constructor parses language file and stores translations. 43 */ 44 function __construct() { 45 46 # Already initialized? 47 if(is_array(self::$langdata)) return; 48 49 self::$langdata = array(); 50 51 $settings = new phpVBoxConfigClass(); 52 $lang = strtolower($settings->language); 53 54 if(@$_COOKIE['vboxLanguage']) { 55 $lang = str_replace(array('/','\\','.'),'',$_COOKIE['vboxLanguage']); 56 } 57 58 // File as specified 59 if($lang && file_exists(VBOX_BASE_LANG_DIR.'/source/'.$lang.'.dat')) { 60 @define('VBOXLANG', $lang); 61 62 // No lang file found 63 } else { 64 $lang = 'en'; 65 @define('VBOXLANG', $lang); 66 self::$langdata['contexts'] = array(); 67 return; 68 } 69 70 71 self::$langdata = unserialize(@file_get_contents(VBOX_BASE_LANG_DIR.'/source/'.$lang.'.dat')); 72 73 $xmlObj = simplexml_load_string(@file_get_contents(VBOX_BASE_LANG_DIR.'/'.$lang.'.xml')); 74 $arrXml = $this->objectsIntoArray($xmlObj); 75 76 $lang = array(); 77 if(!@$arrXml['context'][0]) $arrXml['context'] = array($arrXml['context']); 78 foreach($arrXml['context'] as $c) { 79 80 if(!is_array($c) || !@$c['name']) continue; 81 if(!@$c['message'][0]) $c['message'] = array($c['message']); 82 83 $lang['contexts'][$c['name']] = array(); 84 $lang['contexts'][$c['name']]['messages'] = array(); 85 86 foreach($c['message'] as $m) { 87 88 if(!is_array($m)) continue; 89 90 $s = $m['source']; 91 unset($m['source']); 92 $lang['contexts'][$c['name']]['messages'][$s] = $m; 93 } 94 } 95 self::$langdata = array_merge_recursive(self::$langdata, $lang); 96 } 97 98 /** 99 * Translate text. 100 * @param string $item message to translate 101 * @param string $context context in which the translation should be performed 102 */ 103 function trans($item,$context='phpVirtualBox') { 104 $t = @self::$langdata['contexts'][$context]['messages'][$item]['translation']; 105 return ($t ? $t : $item); 106 } 107 108 /** 109 * 110 * Converts objects into array. Called from class constructor. 111 * @param array|object $arrObjData object or array to convert to array 112 * @param array $arrSkipIndices array of indices to skip 113 * @return array 114 */ 115 function objectsIntoArray($arrObjData, $arrSkipIndices = array()) 116 { 117 $arrData = array(); 118 119 // if input is object, convert into array 120 if (is_object($arrObjData)) { 121 $arrObjData = get_object_vars($arrObjData); 122 } 123 124 if (is_array($arrObjData)) { 125 foreach ($arrObjData as $index => $value) { 126 if (is_object($value) || is_array($value)) { 127 $value = $this->objectsIntoArray($value, $arrSkipIndices); // recursive call 128 } 129 if (in_array($index, $arrSkipIndices)) { 130 continue; 131 } 132 $arrData[$index] = $value; 133 } 134 } 135 return $arrData; 136 } 137 138} 139 140/** 141 * Translate text. If $GLOBALS['_vbox_language'] is not set, create a new 142 * instance and pass params to its trans() method 143 * @param string $msg message to translate 144 * @param string $context context in which the translation should be performed 145 * @uses $_vbox_language 146 * @return string 147 */ 148function trans($msg,$context='phpVirtualBox') { 149 if(!is_object($GLOBALS['_vbox_language'])) $GLOBALS['_vbox_language'] = new __vbox_language(); 150 return $GLOBALS['_vbox_language']->trans($msg,$context); 151} 152