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.

parse_vbox_lang.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /*
  3. * $Id: parse_vbox_lang.php 350 2011-10-17 17:24:29Z imooreyahoo@gmail.com $
  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. $s = clean($m['source'],true);
  23. unset($m['source']);
  24. // Check for valid translation data
  25. if(is_array($m['translation']) && count($m['translation']) == 0)
  26. continue;
  27. // Translation is a numerusfourm
  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. // Translation exists
  38. } else if(is_array($m['translation'])) {
  39. // assume unfinished
  40. $m['translation'] = $s;
  41. // Translation does not exist yet
  42. } else {
  43. $m['translation'] = clean($m['translation']);
  44. }
  45. if($phpStyle) {
  46. $m['htmlized'] = htmlentities($s, ENT_NOQUOTES, 'UTF-8');
  47. if(strlen($m['htmlized']) == strlen($s)) unset($m['htmlized']);
  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'. If so, don't copy it
  54. if(@$m['translation_attr']['type'] == 'obsolete') continue;
  55. // Check to see if existing translation has 'obsolete'. If so, overwrite it
  56. if(@$lang['contexts'][$c['name']]['messages'][$s]['translation_attr']['type'] == 'obsolete') {
  57. $lang['contexts'][$c['name']]['messages'][$s] = $m;
  58. continue;
  59. }
  60. }
  61. $lang['contexts'][$c['name']]['messages'][$s][] = $m;
  62. } else {
  63. $lang['contexts'][$c['name']]['messages'][$s] = $m;
  64. }
  65. }
  66. }
  67. function clean($s) {
  68. 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')));
  69. }
  70. if($info) {
  71. echo(@$lang['contexts']['@@@']['messages']['English'][0]['translation']);
  72. $c = @$lang['contexts']['@@@']['messages']['--'][0]['translation'];
  73. if($c && $c != '--') echo(" (".$c.")");
  74. echo("\n");
  75. } else if($phpStyle) {
  76. print_r($lang);
  77. } else {
  78. echo(serialize($lang));
  79. }
  80. /**
  81. * xml2array() will convert the given XML text to an array in the XML structure.
  82. * Link: http://www.bin-co.com/php/scripts/xml2array/
  83. * Arguments : $contents - The XML text
  84. * $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.
  85. * $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.
  86. * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.
  87. * Examples: $array = xml2array(file_get_contents('feed.xml'));
  88. * $array = xml2array(file_get_contents('feed.xml', 1, 'attribute'));
  89. */
  90. function xml2array($contents, $get_attributes=1, $priority = 'tag') {
  91. if(!$contents) return array();
  92. if(!function_exists('xml_parser_create')) {
  93. //print "'xml_parser_create()' function not found!";
  94. return array();
  95. }
  96. //Get the XML parser of PHP - PHP must have this module for the parser to work
  97. $parser = xml_parser_create('');
  98. 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
  99. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  100. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  101. xml_parse_into_struct($parser, trim($contents), $xml_values);
  102. xml_parser_free($parser);
  103. if(!$xml_values) return;//Hmm...
  104. //Initializations
  105. $xml_array = array();
  106. $parents = array();
  107. $opened_tags = array();
  108. $arr = array();
  109. $current = &$xml_array; //Refference
  110. //Go through the tags.
  111. $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
  112. foreach($xml_values as $data) {
  113. unset($attributes,$value);//Remove existing values, or there will be trouble
  114. //This command will extract these variables into the foreach scope
  115. // tag(string), type(string), level(int), attributes(array).
  116. extract($data);//We could use the array by itself, but this cooler.
  117. $result = array();
  118. $attributes_data = array();
  119. if(isset($value)) {
  120. if($priority == 'tag') $result = $value;
  121. else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
  122. }
  123. //Set the attributes too.
  124. if(isset($attributes) and $get_attributes) {
  125. foreach($attributes as $attr => $val) {
  126. if($priority == 'tag') $attributes_data[$attr] = $val;
  127. else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  128. }
  129. }
  130. //See tag status and do the needed.
  131. if($type == "open") {//The starting of the tag '<tag>'
  132. $parent[$level-1] = &$current;
  133. if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
  134. $current[$tag] = $result;
  135. if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
  136. $repeated_tag_index[$tag.'_'.$level] = 1;
  137. $current = &$current[$tag];
  138. } else { //There was another element with the same tag name
  139. if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
  140. $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  141. $repeated_tag_index[$tag.'_'.$level]++;
  142. } else {//This section will make the value an array if multiple tags with the same name appear together
  143. $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
  144. $repeated_tag_index[$tag.'_'.$level] = 2;
  145. if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
  146. $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  147. unset($current[$tag.'_attr']);
  148. }
  149. }
  150. $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
  151. $current = &$current[$tag][$last_item_index];
  152. }
  153. } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
  154. //See if the key is already taken.
  155. if(!isset($current[$tag])) { //New Key
  156. $current[$tag] = $result;
  157. $repeated_tag_index[$tag.'_'.$level] = 1;
  158. if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
  159. } else { //If taken, put all things inside a list(array)
  160. if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
  161. // ...push the new element into that array.
  162. $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  163. if($priority == 'tag' and $get_attributes and $attributes_data) {
  164. $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  165. }
  166. $repeated_tag_index[$tag.'_'.$level]++;
  167. } else { //If it is not an array...
  168. $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
  169. $repeated_tag_index[$tag.'_'.$level] = 1;
  170. if($priority == 'tag' and $get_attributes) {
  171. if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
  172. $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  173. unset($current[$tag.'_attr']);
  174. }
  175. if($attributes_data) {
  176. $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  177. }
  178. }
  179. $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
  180. }
  181. }
  182. } elseif($type == 'close') { //End of tag '</tag>'
  183. $current = &$parent[$level-1];
  184. }
  185. }
  186. return($xml_array);
  187. }