3 Ryzom Core Web-Based Translation Tool
4 Copyright (C) 2011 Piotr Kaczmarek <p.kaczmarek@openlink.pl>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 var $pipeline_directory = "/home/kaczorek/projects/webtt/distfiles/translation/";
26 function removeComments($str)
28 /* while (($cstart = mb_strpos($str, "/*", $offset)) !== false)
33 //As a pertinent note, there's an issue with this function where parsing any string longer than 94326 characters long will silently return null. So be careful where you use it at.
34 //http://pl.php.net/manual/en/function.preg-replace.php#98843
35 ini_set('pcre.backtrack_limit', 10000000);
36 //$returnString = preg_replace('!/\*.*?\*/!s', '', $str); // /* .*? */ s
37 // added [^/] because there was //******* in translation file
38 $returnString = preg_replace('![^/]/\*.*?\*/!s', '', $str); // /* .*? */ s
40 // if (PREG_NO_ERROR !== preg_last_error())
41 if ($returnString === null)
44 var_dump("PREG ERROR");
50 function removeBOM($str)
52 // mb_internal_encoding("ISO-8859-2");
53 // var_dump(substr($str, 0, 3));
54 if(($bom = substr($str, 0,3)) == pack("CCC",0xef,0xbb,0xbf))
56 // var_dump("jest bom");
57 $bla = substr($str, 3);
70 if(($bom = substr($str, 0,3)) != pack("CCC",0xef,0xbb,0xbf))
71 return pack("CCC",0xef,0xbb,0xbf) . $str;
76 function parseLine($str)
79 // var_dump(mb_internal_encoding());
80 // mb_internal_encoding("ISO-8859-2");
81 if (mb_substr($str, 0, 2) == "//")
83 if (mb_substr($str, 0, 7) == "// DIFF")
85 list($j, $type, $command, $args) = explode(" ", $str);
86 $command = mb_strtolower($command);
87 if ($command == "add" ||
$command == "changed")
89 $index = intval($args);
90 $command = mb_substr($str, 3);
95 else if (mb_substr($str, 0, 8) == "// INDEX")
97 list($j, $type, $index) = explode(" ", $str);
98 $type = "internal_index";
99 // $arr = explode(" ", $str);
101 else if (mb_substr($str, 0, 13) == "// HASH_VALUE")
103 list($j, $type, $hash_value) = explode(" ", $str);
104 $type = "hash_value";
106 /* if (!isset($type))
108 var_dump(isset($type));
109 debug_print_backtrace();
114 $type = mb_strtolower($type);
115 $arr = compact("type","command","index","hash_value");
118 else if (!(mb_substr($str, 0, 2) == "//") && mb_strlen($str))
122 $lBracket = mb_strpos($str, "[");
123 $rBracket = mb_strrpos($str, "]");
124 $sStart = $lBracket +
1;
125 $sEnd = $rBracket - ($sStart);
126 $identifier = trim(mb_substr($str, 0, $lBracket));
128 $sEnd = mb_strlen($str);
129 $string = mb_substr($str, $sStart, $sEnd);
130 $string = str_replace(
131 array('\\\\', '\[','\]'),
132 array('\\', '[',']'), // '
135 $arr = compact("type", "identifier", "string");
137 /* echo "<pre>################################\n";
144 function parseFile($file)
146 $parsedEnt = array();
148 $prevStringLine = false;
151 // $file = file_get_contents($this->pipeline_directory . $file);
152 // var_dump(mb_substr($file, 0,3));
153 // var_dump(substr($file, 0,3));
155 $file = $this->removeBOM($file);
157 $file = $this->removeComments($file);
159 $lines = explode("\n", $file);
165 foreach ($lines as $line)
169 echo "\n\t#################### LINE NUMBER " . $line_no++
. "\n\n";
173 // $line = rtrim($line);
174 // $line = str_replace(array("\r\n","\n","\r"), '', $line);
175 $line = rtrim($line, "\r\n");
176 $parsedLine = $this->parseLine($line);
180 echo "%%%% parsedLine\n";
181 var_dump($parsedLine);
184 echo "%%%% prevStringLine\n";
185 var_dump($prevStringLine);
192 // if line start with diff (diff files) or hash_value (translated files) and before was line with translation, then we start new ent
194 if ($prevStringLine && (
195 ($parsedLine["type"] == "diff" && $parsedEnt) ||
($parsedLine["type"] == "hash_value" && $parsedEnt)
198 /* echo "%%%% prevStringLine %%%%%\n";
199 var_dump($parsedEnt);*/
207 echo "\t%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
208 echo "\t%%%% newEnt %%%%%%%%% newEnt %%%%%%%%% newEnt %%%%%%%%% newEnt %%%%%\n";
209 echo "\t%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n";
210 var_dump($parsedEnt);
212 if (!isset($parsedEnt["diff"]) && !isset($parsedEnt["index"]))
213 $parsedEnt["index"] = $parsedEnt["internal_index"];
215 $entities[] = $parsedEnt;
220 if ($parsedLine["type"] == "internal_index")
221 $parsedEnt["internal_index"] = $parsedLine["index"];
223 if ($parsedLine["type"] == "string")
225 $prevStringLine = true;
229 echo "%%%% parsedEnt %%%%%\n";
230 var_dump($parsedEnt);
232 // echo "%%%% parsedLine %%%%%\n";
233 // var_dump($parsedLine);
236 if (!$parsedLine['identifier'])
238 if ($this->debug
) echo "ZLACZENIE \n";
239 if ($this->debug
&& !isset($parsedEnt['string']))
241 echo "!isset parsedEnt['string']\n";
243 var_dump($parsedEnt);
244 var_dump($parsedLine);
246 // $parsedEnt['string'] .= $parsedLine['string'] . "\n";
247 $parsedEnt['string'] .= "\n" . $parsedLine['string'];
251 if ($this->debug
) echo "DODANIE \n";
252 $parsedEnt +
= $parsedLine;
253 // $parsedEnt['string'] .= "\n";
258 echo "%%%% parsedEnt after %%%%%\n";
259 var_dump($parsedEnt);
263 $prevStringLine = false;
265 if ($parsedLine["type"] == "diff")
267 $parsedEnt["diff"] = $parsedEnt["command"] = $parsedLine["command"];
268 $parsedEnt["index"] = $parsedLine["index"];
273 if (!isset($parsedEnt["diff"]) && !isset($parsedEnt["index"]))
274 $parsedEnt["index"] = $parsedEnt["internal_index"];
276 $entities[] = $parsedEnt;
290 $s = str_replace("\r\n", "\n", $s);
291 $s = str_replace("\n", "\r\n", $s);
296 function buildFile($entities)
299 foreach ($entities as $ent)
301 if (isset($ent['command']))
302 $content .= '// ' . $ent['command'] . "\n";
303 if (isset($ent['hash_value']))
304 $content .= '// HASH_VALUE ' . $ent['hash_value'];
305 /* if (isset($ent['command']))
306 $content .= '// INDEX ' . $ent['internal_index'] . "\n";
308 if (!isset($ent['command']))
309 $content .= '// INDEX ' . $ent['index'] . "\n";
310 $content .= $ent['identifier'] . "\t" . '[' . $ent['string'] . ']' . "\n";
313 return $this->addBOM($this->CRLF($content));