equal
deleted
inserted
replaced
1 <?php |
1 <?php |
2 /** |
2 /** |
3 * Class for working with PO files |
3 * Class for working with PO files |
4 * |
4 * |
5 * @version $Id: po.php 589 2010-12-18 01:40:57Z nbachiyski $ |
5 * @version $Id: po.php 718 2012-10-31 00:32:02Z nbachiyski $ |
6 * @package pomo |
6 * @package pomo |
7 * @subpackage po |
7 * @subpackage po |
8 */ |
8 */ |
9 |
9 |
10 require_once dirname(__FILE__) . '/translations.php'; |
10 require_once dirname(__FILE__) . '/translations.php'; |
16 /** |
16 /** |
17 * Routines for working with PO files |
17 * Routines for working with PO files |
18 */ |
18 */ |
19 if ( !class_exists( 'PO' ) ): |
19 if ( !class_exists( 'PO' ) ): |
20 class PO extends Gettext_Translations { |
20 class PO extends Gettext_Translations { |
21 |
21 |
22 var $comments_before_headers = ''; |
22 var $comments_before_headers = ''; |
23 |
23 |
24 /** |
24 /** |
25 * Exports headers to a PO entry |
25 * Exports headers to a PO entry |
26 * |
26 * |
78 $export = $this->export($include_headers); |
78 $export = $this->export($include_headers); |
79 $res = fwrite($fh, $export); |
79 $res = fwrite($fh, $export); |
80 if (false === $res) return false; |
80 if (false === $res) return false; |
81 return fclose($fh); |
81 return fclose($fh); |
82 } |
82 } |
83 |
83 |
84 /** |
84 /** |
85 * Text to include as a comment before the start of the PO contents |
85 * Text to include as a comment before the start of the PO contents |
86 * |
86 * |
87 * Doesn't need to include # in the beginning of lines, these are added automatically |
87 * Doesn't need to include # in the beginning of lines, these are added automatically |
88 */ |
88 */ |
89 function set_comment_before_headers( $text ) { |
89 function set_comment_before_headers( $text ) { |
90 $this->comments_before_headers = $text; |
90 $this->comments_before_headers = $text; |
91 } |
91 } |
118 } |
118 } |
119 // remove empty strings |
119 // remove empty strings |
120 $po = str_replace("$newline$quote$quote", '', $po); |
120 $po = str_replace("$newline$quote$quote", '', $po); |
121 return $po; |
121 return $po; |
122 } |
122 } |
123 |
123 |
124 /** |
124 /** |
125 * Gives back the original string from a PO-formatted string |
125 * Gives back the original string from a PO-formatted string |
126 * |
126 * |
127 * @static |
127 * @static |
128 * @param string $string PO-formatted string |
128 * @param string $string PO-formatted string |
129 * @return string enascaped string |
129 * @return string enascaped string |
130 */ |
130 */ |
131 function unpoify($string) { |
131 function unpoify($string) { |
151 } |
151 } |
152 return $unpoified; |
152 return $unpoified; |
153 } |
153 } |
154 |
154 |
155 /** |
155 /** |
156 * Inserts $with in the beginning of every new line of $string and |
156 * Inserts $with in the beginning of every new line of $string and |
157 * returns the modified string |
157 * returns the modified string |
158 * |
158 * |
159 * @static |
159 * @static |
160 * @param string $string prepend lines in this string |
160 * @param string $string prepend lines in this string |
161 * @param string $with prepend lines with this string |
161 * @param string $with prepend lines with this string |
227 } else { |
227 } else { |
228 $this->add_entry($res['entry']); |
228 $this->add_entry($res['entry']); |
229 } |
229 } |
230 } |
230 } |
231 PO::read_line($f, 'clear'); |
231 PO::read_line($f, 'clear'); |
232 return $res !== false; |
232 if ( false === $res ) { |
233 } |
233 return false; |
234 |
234 } |
|
235 if ( ! $this->headers && ! $this->entries ) { |
|
236 return false; |
|
237 } |
|
238 return true; |
|
239 } |
|
240 |
235 function read_entry($f, $lineno = 0) { |
241 function read_entry($f, $lineno = 0) { |
236 $entry = new Translation_Entry(); |
242 $entry = new Translation_Entry(); |
237 // where were we in the last step |
243 // where were we in the last step |
238 // can be: comment, msgctxt, msgid, msgid_plural, msgstr, msgstr_plural |
244 // can be: comment, msgctxt, msgid, msgid_plural, msgstr, msgstr_plural |
239 $context = ''; |
245 $context = ''; |
334 if (array() == array_filter($entry->translations, create_function('$t', 'return $t || "0" === $t;'))) { |
340 if (array() == array_filter($entry->translations, create_function('$t', 'return $t || "0" === $t;'))) { |
335 $entry->translations = array(); |
341 $entry->translations = array(); |
336 } |
342 } |
337 return array('entry' => $entry, 'lineno' => $lineno); |
343 return array('entry' => $entry, 'lineno' => $lineno); |
338 } |
344 } |
339 |
345 |
340 function read_line($f, $action = 'read') { |
346 function read_line($f, $action = 'read') { |
341 static $last_line = ''; |
347 static $last_line = ''; |
342 static $use_last_line = false; |
348 static $use_last_line = false; |
343 if ('clear' == $action) { |
349 if ('clear' == $action) { |
344 $last_line = ''; |
350 $last_line = ''; |
347 if ('put-back' == $action) { |
353 if ('put-back' == $action) { |
348 $use_last_line = true; |
354 $use_last_line = true; |
349 return true; |
355 return true; |
350 } |
356 } |
351 $line = $use_last_line? $last_line : fgets($f); |
357 $line = $use_last_line? $last_line : fgets($f); |
|
358 $line = ( "\r\n" == substr( $line, -2 ) ) ? rtrim( $line, "\r\n" ) . "\n" : $line; |
352 $last_line = $line; |
359 $last_line = $line; |
353 $use_last_line = false; |
360 $use_last_line = false; |
354 return $line; |
361 return $line; |
355 } |
362 } |
356 |
363 |
357 function add_comment_to_entry(&$entry, $po_comment_line) { |
364 function add_comment_to_entry(&$entry, $po_comment_line) { |
358 $first_two = substr($po_comment_line, 0, 2); |
365 $first_two = substr($po_comment_line, 0, 2); |
359 $comment = trim(substr($po_comment_line, 2)); |
366 $comment = trim(substr($po_comment_line, 2)); |
360 if ('#:' == $first_two) { |
367 if ('#:' == $first_two) { |
361 $entry->references = array_merge($entry->references, preg_split('/\s+/', $comment)); |
368 $entry->references = array_merge($entry->references, preg_split('/\s+/', $comment)); |
365 $entry->flags = array_merge($entry->flags, preg_split('/,\s*/', $comment)); |
372 $entry->flags = array_merge($entry->flags, preg_split('/,\s*/', $comment)); |
366 } else { |
373 } else { |
367 $entry->translator_comments = trim($entry->translator_comments . "\n" . $comment); |
374 $entry->translator_comments = trim($entry->translator_comments . "\n" . $comment); |
368 } |
375 } |
369 } |
376 } |
370 |
377 |
371 function trim_quotes($s) { |
378 function trim_quotes($s) { |
372 if ( substr($s, 0, 1) == '"') $s = substr($s, 1); |
379 if ( substr($s, 0, 1) == '"') $s = substr($s, 1); |
373 if ( substr($s, -1, 1) == '"') $s = substr($s, 0, -1); |
380 if ( substr($s, -1, 1) == '"') $s = substr($s, 0, -1); |
374 return $s; |
381 return $s; |
375 } |
382 } |