3 /*=====================================================================*\
4 || ###################################################################
5 || # mkstrings (ISSO: Lost In Translation) [#]version[#]
6 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
8 || # This program is free software; you can redistribute it and/or modify
9 || # it under the terms of the GNU General Public License as published by
10 || # the Free Software Foundation; version [#]gpl[#] of the License.
12 || # This program is distributed in the hope that it will be useful, but
13 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 || # You should have received a copy of the GNU General Public License along
18 || # with this program; if not, write to the Free Software Foundation, Inc.,
19 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 || ###################################################################
21 \*=====================================================================*/
23 // ###################################################################
27 print("mkstrings creates XML string files from PHP sources that");
28 print("\nhave been localized using ISSO's localization module");
30 print("Usage: mkstrings <object caller> <ISSO path> [path] [-o <out>]");
31 print("\n\t<object caller> The PHP class that contains the string() method,");
32 print("\n\t\t\tthe localizer function; this is usually \"lang\".");
33 print("\n\t\t\tDo not include the object operator.");
34 print("\n\t<ISSO path> Path to the location of the ISSO framework");
35 print("\n\t[path] Path in which to parse strings of *.php");
36 print("\n\t[-o <out>] Out file path; by default this is ./out.strings.xml");
41 // ###################################################################
45 $search = ($argv[3] ? $argv[3] : './');
46 $out = ($argv[4] ? $argv[5] : './out.strings.xml');
48 require_once($issopath . DIRECTORY_SEPARATOR
. 'kernel.php');
50 $isso->sourcepath
= $isso->fetch_sourcepath($issopath);
52 $isso->load('functions');
56 $out = $isso->fetch_sourcepath($out) . 'out.strings.xml';
59 print("Linking framework and needed modules\n");
61 // ###################################################################
63 $filelist = $funct->scandir($search);
65 foreach ($filelist AS $dirpath => $nodes)
67 $dirpath = ($dirpath ? $isso->fetch_sourcepath($dirpath) : '');
68 foreach ($nodes AS $file)
70 $ext = $funct->fetch_extension($file);
73 $files[] = $isso->fetch_sourcepath($search) . $dirpath . $file;
75 else if ($ext == 'xml' OR $ext == 'tpl' OR $ext == 'html' OR $ext == 'htm')
77 $templates[] = $isso->fetch_sourcepath($search) . $dirpath . $file;
82 print("Generating file node list\n");
84 // ###################################################################
86 foreach ($files AS $file)
88 $data = file_get_contents($file);
91 print("ERROR reading file: $file\n");
94 print("Read file node: $file\n");
96 $strings[] = break_tokens($data, $caller);
99 // ###################################################################
101 foreach ($templates AS $file)
103 $data = file_get_contents($file);
106 print("ERROR reading file: $file\n");
108 print("Read file node: $file\n");
110 $strings[] = parse_template($data);
113 // ###################################################################
115 foreach ($strings AS $subset)
117 foreach ($subset AS $string)
119 $masters["$string"] = $string;
123 // ###################################################################
125 print("Found a total of
" . count($masters) . " localizable strings\n
");
127 $f = fopen($out, 'w');
129 fwrite($f, '<?xml version="1.0" encoding="iso
-8859-1"?>' . "\n\n
");
130 fwrite($f, '<localization>' . "\n\n
");
132 foreach ($masters AS $string)
134 fwrite($f, "\t
<string>\n");
135 fwrite($f, "\t\t
<key
><![CDATA
[$string]]></key
>\n");
136 fwrite($f, "\t\t
<value
><![CDATA
[$string]]></value
>\n");
137 fwrite($f, "\t
</string>\n");
140 fwrite($f, "\n
</localization
>");
144 print("Exported strings
as localizable file
: out
.strings
.xml
");
147 // ###################################################################
149 function break_tokens($fileraw, $caller)
151 $tokens = token_get_all($fileraw);
153 print("\t
... breaking symbols into tokens\n
");
156 foreach ($tokens AS $id => $token)
158 if (is_array($token))
160 $tokens["$id"][2] = $name = token_name($token[0]);
161 $tokens["$id"][3] = $id;
165 $tokens["$id"] = array(
179 foreach ($tokens AS $id => $token)
181 // looking for initial operator
184 if ($token[2] == 'T_VARIABLE' OR $token[2] == 'T_STRING')
186 if ($token[1] == '$' . $caller OR $token[1] == $caller)
192 // need an object operator to continue
193 else if ($stack == 1)
195 if ($token[2] == 'T_OBJECT_OPERATOR
')
204 // look for the string() method
205 else if ($stack == 2)
207 if ($token[1] == 'string')
216 // first T_CONSTANT_ENCAPSED_STRING will do
217 else if ($stack >= 3)
219 if ($token[2] == 'T_CONSTANT_ENCAPSED_STRING
')
221 $strings[] = extract_string($token[1]);
228 print("\t... found $count localizable strings\n");
234 // ###################################################################
236 function extract_string($string)
240 $string = str_replace('\\
' . $encap, $encap, $string);
241 $string = substr($string, 1, strlen($string) - 2);
246 // ###################################################################
248 function parse_template($raw)
250 $length = strlen($raw);
257 for ($i = 0; $i < $length; $i++)
261 if ($raw["$i"] == '"' AND $raw[ $i - 1 ] != '\\' AND $raw[ $i + 1 ] == '}' AND $raw[ $i - 1 ] != '@')
265 $strings[ ++$index ] = '';
269 $strings["$index"] .= $raw["$i"];
274 if ($raw["$i"] == '{')
279 else if ($stack == 1)
281 if ($raw["$i"] == '@')
291 else if ($stack == 2)
293 if ($raw["$i"] == '"')
307 print("\t... found " . count($strings) . " localizable strings\n");
313 /*=====================================================================*\
314 || ###################################################################
317 || ###################################################################
318 \*=====================================================================*/