Rev Author Line No. Line
228 kaklik 1 <?php
2  
3 /**
4 * This file merges the two PO template files (singapore.pot and singapore.admin.pot)
5 * with all existing language files (singapore.LANG.po)
6 *
7 * @author Joel Sjögren <joel dot sjogren at nonea dot se>
8 * @author Tamlyn Rhodes <tam at zenology dot co dot uk>
9 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License
10 * @copyright (c)2003-2005 Tamlyn Rhodes
11 * @version $Id: compile.php,v 1.9 2006/04/29 16:18:52 tamlyn Exp $
12 */
13  
14 // Programs to call (insert path to them if necessary)
15 $GETTEXT_MERGE = "msgmerge";
16 $BASEPATH = realpath("..")."/";
17  
18  
19 //require config class
20 require_once $BASEPATH."includes/config.class.php";
21  
22 //get config object
23 $config = sgConfig::getInstance();
24 $config->loadConfig($BASEPATH."singapore.ini");
25  
26 $OUTPUTPATH = $BASEPATH.$config->pathto_locale;
27 $standardPot = $OUTPUTPATH."singapore.pot";
28 $adminPot = $OUTPUTPATH."singapore.admin.pot";
29 $createbackups = true;
30  
31  
32  
33 /**
34 * Parses a directory and returns full path to all the files
35 * matching the filter (file name suffix)
36 *
37 * @param string $dir full directory name (must end with /)
38 * @param string $filter file name suffixes separated by | (optional, default don't filter)
39 * @return array an array with all files
40 **/
41 function parseDirectory ($dir, $filter = 'php|html|tpl|inc')
42 {
43 $ret = array();
44 if (is_dir($dir)) {
45 $d = dir($dir);
46 while (($file = $d->read()) !== false) {
47 if ($file == '.' || $file == '..') continue;
48 $fullfile = $d->path . $file;
49 if (is_dir($fullfile)) $ret = array_merge($ret,parseDirectory($fullfile."/"));
50 else if (!$filter || preg_match("/\.({$filter})$/i", $file)) $ret[] = $fullfile;
51 }
52 }
53 return $ret;
54 }
55  
56  
57 /**
58 * Parses a PO file and writes a file with
59 * serialized strings for PHP
60 *
61 * @param string $input file to read from
62 * @param string $output file to write to
63 * @return bool success
64 **/
65 function parsePO ($input, $output)
66 {
67 // Open PO-file
68 file_exists($input) or die("The file {$input} doesn't exit.\n");
69 $fp = @fopen($input, "r") or die("Couldn't open {$input}.\n");
70  
71 $type = 0;
72 $strings = array();
73 $escape = "\n";
74 $string = "";
75 $plural = 0;
76 $header = "";
77 while (!feof($fp)) {
78 $line = trim(fgets($fp,1024));
79 if ($line == "" || $line[0] == "#") continue;
80 // New (msgid "text")
81 if (preg_match("/msgid[[:space:]]+\"(.+)\"$/i", $line, $m)) {
82 $type = 1;
83 $string = stripcslashes($m[1]);
84 // New id on several rows (msgid "") or header
85 } elseif (preg_match("/msgid[[:space:]]+\"\"$/i", $line, $m)) {
86 $type = 2;
87 $string = "";
88 // Add to id on several rows ("")
89 } elseif (preg_match("/^\"(.*)\"$/i", $line, $m) && ($type == 1 || $type == 2 || $type == 3)) {
90 $type = 3;
91 $string .= stripcslashes($m[1]);
92 // New string (msgstr "text")
93 } elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $m) && ($type == 1 || $type == 3) && $string) {
94 $strings[$string] = stripcslashes($m[1]);
95 $type = 4;
96 // New string on several rows (msgstr "")
97 } elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $m) && ($type == 1 || $type == 3) && $string) {
98 $type = 4;
99 $strings[$string] = "";
100 // Add to string on several rows ("")
101 } elseif (preg_match("/^\"(.*)\"$/i", $line, $m) && $type == 4 && $string) {
102 $strings[$string] .= stripcslashes($m[1]);
103  
104 /////Plural forms/////
105 // New plural id (msgid_plural "text")
106 } elseif (preg_match("/msgid_plural[[:space:]]+\".*\"$/i", $line, $m)) {
107 $type = 6;
108 // New plural string (msgstr[N] "text")
109 } elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"(.+)\"$/i", $line, $m) && ($type == 6) && $string) {
110 $plural = $m[1];
111 $strings[$string][$plural] = stripcslashes($m[2]);
112 $type = 6;
113 // New plural string on several rows (msgstr[N] "")
114 } elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"\"$/i", $line, $m) && ($type == 6) && $string) {
115 $plural = $m[1];
116 $strings[$string][$plural] = "";
117 $type = 6;
118 // Add to plural string on several rows ("")
119 } elseif (preg_match("/^\"(.*)\"$/i", $line, $m) && $type == 6 && $string) {
120 $strings[$string][$plural] .= stripcslashes($m[1]);
121  
122 /////Header section/////
123 // Start header section
124 } elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $m) && $type == 2 && !$string) {
125 $header = stripcslashes($m[1]);
126 $type = 5;
127 // Start header section
128 } elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $m) && !$string) {
129 $header = "";
130 $type = 5;
131 // Add to header section
132 } elseif (preg_match("/^\"(.*)\"$/i", $line, $m) && $type == 5) {
133 $header .= stripcslashes($m[1]);
134  
135 // Reset
136 } else {
137 unset($strings[$string]);
138 $type = 0;
139 $string = "";
140 $plural = 0;
141 }
142 }
143  
144 // Close PO-file
145 fclose($fp);
146  
147 // Extract plural forms from header
148 if(preg_match("/Plural-Forms:[[:space:]]+(.+)/i", $header, $m)) {
149 $pluralString = str_replace("n","\$n",$m[1]);
150 $pluralString = str_replace(" plural","\$plural",$pluralString);
151 } else {
152 $pluralString = "\$nplurals=1; \$plural=\$n==1?0:1;";
153 }
154  
155 // Extract character set from header
156 if(preg_match("/Content-Type:(.+)charset=(.+)/i", $header, $m))
157 $charset = $m[2];
158 else
159 $charset = "";
160  
161 // Extract language name from header
162 if(preg_match("/Language-Team:[[:space:]]+([^<\n]+)/i", $header, $m))
163 $language = $m[1];
164 else
165 $language = "Unknown";
166  
167 $strings[0]["charset"] = $charset;
168 $strings[0]["language"] = $language;
169 $strings[0]["plural"] = $pluralString;
170  
171 // Open data file for writing
172 $fp = @fopen($output, "wb") or die("Couldn't open file ({$output}).\n");
173 fwrite($fp, serialize($strings));
174 fclose($fp);
175  
176 //set permissions on new PMO file
177 @chmod($output, octdec($GLOBALS['config']->file_mode));
178  
179 // Return
180 return true;
181 }
182  
183  
184 ?>
185 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
186 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
187  
188 <html xmlns="http://www.w3.org/1999/xhtml">
189 <head>
190 <title>i18n po compiler</title>
191 <link rel="stylesheet" type="text/css" href="tools.css" />
192 </head>
193  
194 <body>
195  
196 <h1>i18n po compiler</h1>
197  
198 <p><?php
199 $files = parseDirectory($OUTPUTPATH, 'po');
200 foreach ($files as $file) {
201 if (!preg_match("/singapore\.(admin\.)?[\w]+\.po$/i", $file)) continue;
202 $outfile = preg_replace("/\.[^\.]+$/", ".pmo", $file);
203 if(parsePO($file, $outfile))
204 echo "Parsed $file to $outfile<br />";
205 else
206 echo "Could not parse $file<br />";
207 }
208  
209 ?>
210 </p>
211  
212 <p>All operations complete. <a href="index.html">Return</a> to tools.</p>
213  
214 </body>
215 </html>