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