From 6bafc1755895f514d97a25b818e022e683a451c3 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 16 Aug 2006 22:05:01 +0000 Subject: [PATCH] Moving mkstrings out of lost-in-translation to the trunk/dev --- dev/mkstrings | 319 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 319 insertions(+) create mode 100755 dev/mkstrings diff --git a/dev/mkstrings b/dev/mkstrings new file mode 100755 index 0000000..59880f8 --- /dev/null +++ b/dev/mkstrings @@ -0,0 +1,319 @@ +#!/usr/bin/php + [path] [-o ]"); + print("\n\t The PHP class that contains the string() method,"); + print("\n\t\t\tthe localizer function; this is usually \"lang\"."); + print("\n\t\t\tDo not include the object operator."); + print("\n\t Path to the location of the ISSO framework"); + print("\n\t[path] Path in which to parse strings of *.php"); + print("\n\t[-o ] Out file path; by default this is ./out.strings.xml"); + print("\n"); + exit; +} + +// ################################################################### + +$caller = $argv[1]; +$issopath = $argv[2]; +$search = ($argv[3] ? $argv[3] : './'); +$out = ($argv[4] ? $argv[5] : './out.strings.xml'); + +require_once($issopath . DIRECTORY_SEPARATOR . 'kernel.php'); +$isso =& $_isso; +$isso->sourcepath = $isso->fetch_sourcepath($issopath); + +$isso->load('functions'); + +if (is_dir($out)) +{ + $out = $isso->fetch_sourcepath($out) . 'out.strings.xml'; +} + +print("Linking framework and needed modules\n"); + +// ################################################################### + +$filelist = $funct->scandir($search); + +foreach ($filelist AS $dirpath => $nodes) +{ + $dirpath = ($dirpath ? $isso->fetch_sourcepath($dirpath) : ''); + foreach ($nodes AS $file) + { + $ext = $funct->fetch_extension($file); + if ($ext == 'php') + { + $files[] = $isso->fetch_sourcepath($search) . $dirpath . $file; + } + else if ($ext == 'xml' OR $ext == 'tpl' OR $ext == 'html' OR $ext == 'htm') + { + $templates[] = $isso->fetch_sourcepath($search) . $dirpath . $file; + } + } +} + +print("Generating file node list\n"); + +// ################################################################### + +foreach ($files AS $file) +{ + $data = file_get_contents($file); + if ($data === false) + { + print("ERROR reading file: $file\n"); + continue; + } + print("Read file node: $file\n"); + + $strings[] = break_tokens($data, $caller); +} + +// ################################################################### + +foreach ($templates AS $file) +{ + $data = file_get_contents($file); + if ($data === false) + { + print("ERROR reading file: $file\n"); + } + print("Read file node: $file\n"); + + $strings[] = parse_template($data); +} + +// ################################################################### + +foreach ($strings AS $subset) +{ + foreach ($subset AS $string) + { + $masters["$string"] = $string; + } +} + +// ################################################################### + +print("Found a total of " . count($masters) . " localizable strings\n"); + +$f = fopen($out, 'w'); + +fwrite($f, '' . "\n\n"); +fwrite($f, '' . "\n\n"); + +foreach ($masters AS $string) +{ + fwrite($f, "\t\n"); + fwrite($f, "\t\t\n"); + fwrite($f, "\t\t\n"); + fwrite($f, "\t\n"); +} + +fwrite($f, "\n"); + +fclose($f); + +print("Exported strings as localizable file: out.strings.xml"); +print("\n"); + +// ################################################################### + +function break_tokens($fileraw, $caller) +{ + $tokens = token_get_all($fileraw); + + print("\t... breaking symbols into tokens\n"); + + // nominalize tokens + foreach ($tokens AS $id => $token) + { + if (is_array($token)) + { + $tokens["$id"][2] = $name = token_name($token[0]); + $tokens["$id"][3] = $id; + } + else + { + $tokens["$id"] = array( + 0 => -1, + 1 => $token, + 2 => 'X_OTHER', + 3 => $id + ); + } + } + + $stack = 0; + $count = 0; + $strings = array(); + + // parse calls + foreach ($tokens AS $id => $token) + { + // looking for initial operator + if ($stack == 0) + { + if ($token[2] == 'T_VARIABLE' OR $token[2] == 'T_STRING') + { + if ($token[1] == '$' . $caller OR $token[1] == $caller) + { + $stack++; + } + } + } + // need an object operator to continue + else if ($stack == 1) + { + if ($token[2] == 'T_OBJECT_OPERATOR') + { + $stack++; + } + else + { + $stack = 0; + } + } + // look for the string() method + else if ($stack == 2) + { + if ($token[1] == 'string') + { + $stack++; + } + else + { + $stack = 0; + } + } + // first T_CONSTANT_ENCAPSED_STRING will do + else if ($stack >= 3) + { + if ($token[2] == 'T_CONSTANT_ENCAPSED_STRING') + { + $strings[] = extract_string($token[1]); + $count++; + $stack = 0; + } + } + } + + print("\t... found $count localizable strings\n"); + print("\n"); + + return $strings; +} + +// ################################################################### + +function extract_string($string) +{ + $encap = $string{0}; + + $string = str_replace('\\' . $encap, $encap, $string); + $string = substr($string, 1, strlen($string) - 2); + + return $string; +} + +// ################################################################### + +function parse_template($raw) +{ + $length = strlen($raw); + + $stack = 0; + $capture = false; + $index = 0; + $strings = array(); + + for ($i = 0; $i < $length; $i++) + { + if ($capture) + { + if ($raw["$i"] == '"' AND $raw[ $i - 1 ] != '\\' AND $raw[ $i + 1 ] == '}' AND $raw[ $i - 1 ] != '@') + { + $stack = 0; + $capture = false; + $strings[ ++$index ] = ''; + } + else + { + $strings["$index"] .= $raw["$i"]; + } + } + else + { + if ($raw["$i"] == '{') + { + $stack++; + $capture = false; + } + else if ($stack == 1) + { + if ($raw["$i"] == '@') + { + $stack++; + } + else + { + $stack = 0; + } + $capture = false; + } + else if ($stack == 2) + { + if ($raw["$i"] == '"') + { + $stack++; + $capture = true; + } + else + { + $stack = 0; + $capture = false; + } + } + } + } + + print("\t... found " . count($strings) . " localizable strings\n"); + print("\n"); + + return $strings; +} + +/*=====================================================================*\ +|| ################################################################### +|| # $HeadURL$ +|| # $Id$ +|| ################################################################### +\*=====================================================================*/ +?> \ No newline at end of file -- 2.43.5