PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Miranda IM - CK Release/Miranda/Plugins/historypp/build_scripts/trans.php

http://miranda-dev.googlecode.com/
PHP | 336 lines | 270 code | 41 blank | 25 comment | 59 complexity | 35831e2e0549839f1ea94f15d4b9bf50 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /*
  3. * Description expected...
  4. *
  5. */
  6. function file_put($filename, $text) {
  7. $f = fopen($filename,'w',false);
  8. fwrite($f,$text);
  9. fclose($f);
  10. }
  11. function parse_object() {
  12. global $ignore_menuitems, $ignore_tbitems, $lines, $lineid, $dfm_str, $dfm_prop, $dfm_noadd, $no_add;
  13. $in_strings = false;
  14. $in_str_count = 0;
  15. $in_str_name = '';
  16. $cur_object = '';
  17. if (preg_match('/object (.*):/',$lines[$lineid],$matches)) {
  18. // first object is empty
  19. if ($lineid !== 0) {
  20. $cur_object = $matches[1]."."; }
  21. $lineid = $lineid + 1;
  22. }
  23. while(false == (preg_match('/\s*end$/',rtrim($lines[$lineid])))) {
  24. $lines[$lineid] = rtrim($lines[$lineid]);
  25. if (preg_match('/^(\s)*object (.*): (.*)$/',$lines[$lineid],$matches)) {
  26. if (($ignore_menuitems && (($matches[3] == "TMenuItem")||($matches[3] == "TTntMenuItem"))) ||
  27. ($ignore_menuitems && (($matches[3] == "TSpeedButton")||($matches[3] == "TTntSpeedButton")||($matches[3] == "THppSpeedButton"))) ||
  28. ($ignore_tbitems && (($matches[3] == "TToolButton")||($matches[3] == "TTntToolButton")||($matches[3] == "THppToolButton")))) {
  29. $no_add = true;
  30. parse_object();
  31. $no_add = false;
  32. } else { parse_object(); }
  33. }
  34. // parse values
  35. // we are in .Strings = ( ... ) area
  36. if ($in_strings) {
  37. if (preg_match('/^\s*\'(.*)\'\)?$/',$lines[$lineid],$matches)) {
  38. $dfm_prop[] = $cur_object.$in_str_name.'['.$in_str_count.']';
  39. $dfm_str[] = $matches[1];
  40. $dfm_noadd[] = $no_add;
  41. $in_str_count += 1;
  42. $in_strings = !(preg_match('/\)$/',$lines[$lineid]));
  43. } else { $in_strings = false; }
  44. } else
  45. // value = '...' types
  46. if (preg_match('/^\s+([\d\w\.]*) = \'(.*)\'$/',$lines[$lineid],$matches)) {
  47. if (($matches[1] !== 'Name')&&(!preg_match('/\.Name$/',$matches[1]))) {
  48. $dfm_noadd[] = $no_add;
  49. $dfm_prop[] = $cur_object.$matches[1];
  50. $dfm_str[] = $matches[2];
  51. }
  52. } else
  53. // .Strings = ( ... ) area start
  54. if (preg_match('/^\s+([\d\w\.]+)\.Strings = \($/',$lines[$lineid],$matches)) {
  55. $in_str_count = 0;
  56. $in_strings = true;
  57. $in_str_name = $matches[1];
  58. }
  59. if ($lineid >= (count($lines)-1)) { break; }
  60. $lineid = $lineid + 1;
  61. }
  62. }
  63. function add_str($str,$line){
  64. global $pas_str,$pas_str_line;
  65. $pas_str[] = $str;
  66. $pas_str_line[] = $line;
  67. }
  68. function add_var($name,$str,$line){
  69. global $pas_var,$pas_var_str,$pas_var_line;
  70. $pas_var[] = $name;
  71. $pas_var_str[] = $str;
  72. $pas_var_line[] = $line;
  73. }
  74. function add_prop($prop,$line){
  75. global $pas_prop,$pas_prop_line;
  76. if (preg_match('/^P(Ansi|Wide)?Char\((.*)\)$/i',$prop,$matches)) {
  77. $prop = $matches[2]; }
  78. $pas_prop[] = $prop;
  79. $pas_prop_line[] = $line;
  80. }
  81. ini_set("register_globals","1");
  82. ini_set("register_argc_argv","1");
  83. $detailed_file = false;
  84. $ignore_menuitems = true;
  85. $ignore_tbitems = true;
  86. // empty captions and captions consiting from
  87. // whitespace, -, /, <, >, _ are skipped
  88. $skip_empty_captions = '/^[\s\-\<\>\/\_]*$/';
  89. if ($_SERVER["argc"] < 2) {
  90. print "Wrong params\r\n";
  91. exit;
  92. }
  93. $file_to_parse = $_SERVER["argv"][1];
  94. $file_ext = '';
  95. if (preg_match('/(.*)\.(dfm|pas|dpr)$/',$file_to_parse,$matches)) {
  96. $file_to_parse = @$matches[1];
  97. $file_ext = @$matches[2];
  98. }
  99. if (file_exists("$file_to_parse.trans.txt")) { unlink("$file_to_parse.trans.txt"); }
  100. if (file_exists("$file_to_parse.trans-err.txt")) { unlink("$file_to_parse.trans-err.txt"); }
  101. if (file_exists("$file_to_parse.trans-details.txt")) { unlink("$file_to_parse.trans-details.txt"); }
  102. $lines = array();
  103. // parse form file
  104. $lineid = 0;
  105. $no_add = false;
  106. $dfm_str = array();
  107. $dfm_prop = array();
  108. $dfm_noadd = array();
  109. if ($file_ext == 'dfm' || $file_ext == 'pas') {
  110. $lines = @file($file_to_parse.'.dfm');
  111. parse_object();
  112. }
  113. // parse .pas or .dpr
  114. $pas_str = array();
  115. $pas_prop = array();
  116. $pas_str_line = array();
  117. $pas_prop_line = array();
  118. $pas_var = array();
  119. $pas_var_line = array();
  120. $pas_var_str = array();
  121. if ($file_ext == 'pas' || $file_ext == 'dpr') {
  122. $lines = @file($file_to_parse.'.'.$file_ext);
  123. foreach($lines as $i => $line) {
  124. $line = rtrim($line);
  125. $cur_match = '';
  126. // here we capture Translate('...') text
  127. // it's buggy, because we can capture:
  128. // "...'),Translate('..."
  129. // from such string:
  130. // "Translate('...'),Translate('...')"
  131. if (preg_match_all('/Translate(W|WideW|AnsiW|String|WideString)?\s*\(\'(.*)\'\)/i',$line,$matches)) {
  132. foreach($matches[2] as $match) { add_str($match,$i); }
  133. } else
  134. // capture Translate(var)
  135. if (preg_match_all('/Translate(W|WideW|AnsiW|String|WideString)?\s*\((.*)\)/i',$line,$matches)) {
  136. foreach($matches[2] as $match) { add_prop($match,$i); }
  137. }
  138. $pattern = "/([\w\d]+)\s*\:\s*[\w\d]+\s*\=\s*\'(.*)\';$/is";
  139. if (preg_match($pattern,$line,$matches)) {
  140. add_var($matches[1],$matches[2],$line);
  141. }
  142. }
  143. }
  144. // delete properties which are actually vars
  145. foreach($pas_var as $i => $var){
  146. $found = false;
  147. $var = strtolower($var);
  148. foreach($pas_prop as $n => $prop) {
  149. $prop = strtolower($prop);
  150. if ($var == $prop) {
  151. unset($pas_prop[$n]);
  152. unset($pas_prop_line[$n]);
  153. $found = true;
  154. break;
  155. }
  156. }
  157. if (!$found) {
  158. unset($pas_var[$i]);
  159. unset($pas_var_str[$i]);
  160. unset($pas_var_line[$i]);
  161. }
  162. }
  163. // write strings to file
  164. $strings = array();
  165. $strings_d = array();
  166. $filename = $file_to_parse.'.trans.txt';
  167. $filename_d = $file_to_parse.'.trans-details.txt';
  168. if (count($dfm_str) > 0) {
  169. $strings[] = ";; Text found in $file_to_parse.dfm:";
  170. $strings_d[] = ";; Text found in $file_to_parse.dfm:";
  171. foreach($dfm_str as $i => $str){
  172. if (!preg_match($skip_empty_captions,$str)) {
  173. $strings[] = $str;
  174. $strings_d[] = "$str ($dfm_prop[$i])";
  175. }
  176. }
  177. }
  178. if (count($pas_str) > 0) {
  179. $strings[] = ";; Text found in $file_to_parse.pas:";
  180. $strings_d[] = ";; Text found in $file_to_parse.pas:";
  181. foreach($pas_str as $i => $str){
  182. $strings[] = $str;
  183. $strings_d[] = "$str (line # $pas_str_line[$i])";
  184. }
  185. }
  186. if (count($pas_var_str) > 0) {
  187. $strings[] = ";; Text from variables in $file_to_parse.pas:";
  188. $strings_d[] = ";; Text from variables in $file_to_parse.pas:";
  189. foreach($pas_var_str as $i => $str){
  190. $strings[] = $str;
  191. $strings_d[] = "$str (line # $pas_var_line[$i])";
  192. }
  193. }
  194. if (count($strings) > 0) {
  195. file_put($filename, join("\r\n", $strings));
  196. }
  197. if ($detailed_file && (count($strings) > 0)) {
  198. file_put($filename_d, join("\r\n", $strings_d));
  199. }
  200. // search for pas properties not found in dfm
  201. $not_found_in_dfm = array();
  202. foreach($pas_prop as $i => $prop) {
  203. $found = false;
  204. if (preg_match('/\{TRANSLATE-IGNORE\}$/i',$prop)) { continue; }
  205. foreach($dfm_prop as $n => $value) {
  206. if (strcasecmp($prop,$value) == 0) {
  207. unset($dfm_prop[$n]);
  208. unset($dfm_noadd[$n]);
  209. unset($dfm_str[$n]);
  210. //print "Found $prop in dfm, deleted\r\n";
  211. $found = true;
  212. break;
  213. }
  214. if (preg_match('/^([\d\.\w]+)\[([\d\.\w]+)\]$/',$prop,$matches)) {
  215. $pref = $matches[1];
  216. if (preg_match('/^'.$pref.'\[(\d+)\]/',$dfm_prop[$n])) {
  217. unset($dfm_prop[$n]);
  218. unset($dfm_noadd[$n]);
  219. unset($dfm_str[$n]);
  220. //print "Found $prop in dfm as array, deleted\r\n";
  221. $found = true;
  222. continue;
  223. }
  224. }
  225. if ($found) { break; }
  226. }
  227. if (!$found) { $not_found_in_dfm[] = $prop; }
  228. }
  229. // search for error-looking strings grabbed from pas
  230. $other_errors = array();
  231. foreach($pas_str as $i => $str) {
  232. if (preg_match('/\'{1}/',$str)) {
  233. $other_errors[] = "* This string from $file_to_parse.pas($pas_str_line[$i]) looks suspicius:";
  234. $other_errors[] = "* (generally, it's a good rule not to put anything on the line after translate function call)";
  235. $other_errors[] = "$str\r\n";
  236. }
  237. }
  238. // search for dfm properties not represented in pas
  239. $not_found_in_pas = array();
  240. foreach($dfm_prop as $i => $prop) {
  241. $found = false;
  242. if ($dfm_noadd[$i]) { continue; }
  243. if (preg_match($skip_empty_captions,$dfm_str[$i])) { continue; }
  244. foreach($pas_prop as $n => $value) {
  245. if (strcasecmp($prop,$value) == 0) {
  246. unset($pas_prop[$n]);
  247. unset($pas_prop_line[$n]);
  248. $found = true;
  249. break;
  250. }
  251. if ($found) { break; }
  252. }
  253. if (!$found) { $not_found_in_pas[] = $prop; }
  254. }
  255. if ((count($other_errors)+count($not_found_in_pas)+count($not_found_in_dfm))>0) {
  256. $strings = array();
  257. $filename = $file_to_parse.'.trans-err.txt';
  258. $strings[] = "We have errors!";
  259. if (count($not_found_in_pas) > 0) {
  260. $strings[] = "\r\n************ NOT TRANSLATED ***************";
  261. $strings[] = "Form properties not translated in .pas file";
  262. $strings[] = "See translate_guide.txt for what it can mean and how to fix it";
  263. $strings[] = "********************************************\r\n";
  264. }
  265. foreach($not_found_in_pas as $prop){ $strings[] = $prop;}
  266. if (count($not_found_in_dfm) > 0) {
  267. $strings[] = "\r\n************ NOT FOUND IN DFM **************";
  268. $strings[] = "Some properites in the code were not found in DFM,";
  269. $strings[] = "so they were not added to the strings list";
  270. $strings[] = "See translate_guide.txt for for how to make me find them";
  271. $strings[] = "********************************************\r\n";
  272. }
  273. foreach($not_found_in_dfm as $prop){ $strings[] = $prop;}
  274. if (count($other_errors) > 0) {
  275. $strings[] = "\r\n********************************************";
  276. $strings[] = "Other errors. Explanation see below.";
  277. $strings[] = "********************************************\r\n";
  278. }
  279. foreach($other_errors as $err){ $strings[] = $err;}
  280. if (count($strings) > 0) {
  281. file_put($filename, join("\r\n", $strings));
  282. }
  283. }
  284. ?>