You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

language.php 4.4KB

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