PageRenderTime 65ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/languages/source/parse_vbox_lang.php

https://bitbucket.org/kolbyjAFK/phpvirtualbox
PHP | 245 lines | 152 code | 60 blank | 33 comment | 67 complexity | 3f5e7906853cf1aa09ff6f2af418b341 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * Parse VirtualBox (Qt) language files
  6. */
  7. if(!@$argv[1]) {
  8. echo("Usage: {$argv[0]} language_file [-php : php print_r style output] [-info : just print info]\n");
  9. exit;
  10. }
  11. $phpStyle = (@strtolower($argv[2]) == '-php');
  12. $info = (@strtolower($argv[2]) == '-info');
  13. $arrXml = xml2array(file_get_contents($argv[1]));
  14. $lang = array();
  15. $lang['contexts'] = array();
  16. foreach($arrXml['TS']['context'] as $c) {
  17. $lang['contexts'][$c['name']] = array();
  18. $lang['contexts'][$c['name']]['messages'] = array();
  19. if(@$c['message']['source']) $c['message'] = array($c['message']);
  20. foreach($c['message'] as $m) {
  21. if(!is_array($m)) continue;
  22. #if(@$m['translation_attr']['type'] == 'obsolete') continue;
  23. $s = clean($m['source'],true);
  24. unset($m['source']);
  25. // Check for valid translation data
  26. if(is_array($m['translation']) && count($m['translation']) == 0)
  27. continue;
  28. if(is_array($m['translation']) && @$m['translation']['numerusform']) {
  29. if(!is_array($m['translation']['numerusform'])) {
  30. $m['translation'] = clean($m['translation']['numerusform']);
  31. } else {
  32. foreach($m['translation']['numerusform'] as $k=>$v) {
  33. if(is_array($v)) continue;
  34. $m['translation']['numerusform'][$k] = clean($v);
  35. }
  36. }
  37. } else if(is_array($m['translation'])) {
  38. // assume unfinished
  39. $m['translation'] = $s;
  40. } else {
  41. $m['translation'] = clean($m['translation']);
  42. }
  43. if($phpStyle) {
  44. $m['htmlized'] = htmlentities($s, ENT_NOQUOTES, 'UTF-8');
  45. if(strlen($m['htmlized']) == strlen($s)) unset($m['htmlized']);
  46. } else {
  47. #unset($m['comment']);
  48. }
  49. // Messages for this context is an array
  50. if(is_array(@$lang['contexts'][$c['name']]['messages'][$s])) {
  51. // Translation for this message exists
  52. if(isset($lang['contexts'][$c['name']]['messages'][$s]['translation'])) {
  53. // Check to see if incoming translation has 'obsolete'
  54. if(@$m['translation_attr']['type'] == 'obsolete') continue;
  55. // Check to see if existing translation has 'obsolete'
  56. if(@$lang['contexts'][$c['name']]['messages'][$s]['translation_attr']['type'] == 'obsolete') continue;
  57. $lang['contexts'][$c['name']]['messages'][$s] = array($lang['contexts'][$c['name']]['messages'][$s]);
  58. }
  59. $lang['contexts'][$c['name']]['messages'][$s][] = $m;
  60. } else {
  61. $lang['contexts'][$c['name']]['messages'][$s] = $m;
  62. }
  63. }
  64. }
  65. function clean($s) {
  66. return preg_replace('/<\/?qt>/','',str_replace('&','',html_entity_decode(str_replace('&nbsp;',' ',preg_replace('/\(&[A-Za-z]\)(\s*(?:\.\.\.\s*)|:)?$/','\1',$s)), ENT_NOQUOTES, 'UTF-8')));
  67. }
  68. if($info) {
  69. echo(@$lang['contexts']['@@@']['messages']['English'][0]['translation']);
  70. $c = @$lang['contexts']['@@@']['messages']['--'][0]['translation'];
  71. if($c && $c != '--') echo(" (".$c.")");
  72. echo("\n");
  73. } else if($phpStyle) {
  74. print_r($lang);
  75. } else {
  76. echo(serialize($lang));
  77. }
  78. /**
  79. * xml2array() will convert the given XML text to an array in the XML structure.
  80. * Link: http://www.bin-co.com/php/scripts/xml2array/
  81. * Arguments : $contents - The XML text
  82. * $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.
  83. * $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.
  84. * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.
  85. * Examples: $array = xml2array(file_get_contents('feed.xml'));
  86. * $array = xml2array(file_get_contents('feed.xml', 1, 'attribute'));
  87. */
  88. function xml2array($contents, $get_attributes=1, $priority = 'tag') {
  89. if(!$contents) return array();
  90. if(!function_exists('xml_parser_create')) {
  91. //print "'xml_parser_create()' function not found!";
  92. return array();
  93. }
  94. //Get the XML parser of PHP - PHP must have this module for the parser to work
  95. $parser = xml_parser_create('');
  96. xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
  97. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  98. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  99. xml_parse_into_struct($parser, trim($contents), $xml_values);
  100. xml_parser_free($parser);
  101. if(!$xml_values) return;//Hmm...
  102. //Initializations
  103. $xml_array = array();
  104. $parents = array();
  105. $opened_tags = array();
  106. $arr = array();
  107. $current = &$xml_array; //Refference
  108. //Go through the tags.
  109. $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
  110. foreach($xml_values as $data) {
  111. unset($attributes,$value);//Remove existing values, or there will be trouble
  112. //This command will extract these variables into the foreach scope
  113. // tag(string), type(string), level(int), attributes(array).
  114. extract($data);//We could use the array by itself, but this cooler.
  115. $result = array();
  116. $attributes_data = array();
  117. if(isset($value)) {
  118. if($priority == 'tag') $result = $value;
  119. else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
  120. }
  121. //Set the attributes too.
  122. if(isset($attributes) and $get_attributes) {
  123. foreach($attributes as $attr => $val) {
  124. if($priority == 'tag') $attributes_data[$attr] = $val;
  125. else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  126. }
  127. }
  128. //See tag status and do the needed.
  129. if($type == "open") {//The starting of the tag '<tag>'
  130. $parent[$level-1] = &$current;
  131. if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
  132. $current[$tag] = $result;
  133. if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
  134. $repeated_tag_index[$tag.'_'.$level] = 1;
  135. $current = &$current[$tag];
  136. } else { //There was another element with the same tag name
  137. if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
  138. $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  139. $repeated_tag_index[$tag.'_'.$level]++;
  140. } else {//This section will make the value an array if multiple tags with the same name appear together
  141. $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
  142. $repeated_tag_index[$tag.'_'.$level] = 2;
  143. if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
  144. $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  145. unset($current[$tag.'_attr']);
  146. }
  147. }
  148. $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
  149. $current = &$current[$tag][$last_item_index];
  150. }
  151. } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
  152. //See if the key is already taken.
  153. if(!isset($current[$tag])) { //New Key
  154. $current[$tag] = $result;
  155. $repeated_tag_index[$tag.'_'.$level] = 1;
  156. if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
  157. } else { //If taken, put all things inside a list(array)
  158. if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
  159. // ...push the new element into that array.
  160. $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  161. if($priority == 'tag' and $get_attributes and $attributes_data) {
  162. $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  163. }
  164. $repeated_tag_index[$tag.'_'.$level]++;
  165. } else { //If it is not an array...
  166. $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
  167. $repeated_tag_index[$tag.'_'.$level] = 1;
  168. if($priority == 'tag' and $get_attributes) {
  169. if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
  170. $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  171. unset($current[$tag.'_attr']);
  172. }
  173. if($attributes_data) {
  174. $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  175. }
  176. }
  177. $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
  178. }
  179. }
  180. } elseif($type == 'close') { //End of tag '</tag>'
  181. $current = &$parent[$level-1];
  182. }
  183. }
  184. return($xml_array);
  185. }