2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
9 * @category FileFormats
10 * @package File_Gettext
11 * @author Michael Wallner <mike@php.net>
12 * @copyright 2004-2005 Michael Wallner
13 * @license BSD, revised
14 * @version CVS: $Id: Gettext.php,v 1.7 2005/11/08 18:57:03 mike Exp $
15 * @link http://pear.php.net/package/File_Gettext
19 * Use PHPs builtin error messages
21 ini_set('track_errors', true);
26 * GNU gettext file reader and writer.
28 * #################################################################
29 * # All protected members of this class are public in its childs. #
30 * #################################################################
32 * @author Michael Wallner <mike@php.net>
33 * @version $Revision: 1.7 $
41 * associative array with all [msgid => msgstr] entries
46 var $strings = array();
51 * associative array containing meta
52 * information like project name or content type
72 * @return object Returns File_Gettext_PO or File_Gettext_MO on success
73 * or PEAR_Error on failure.
74 * @param string $format MO or PO
75 * @param string $file path to GNU gettext file
77 function &factory($format, $file = '')
79 $format = strToUpper($format);
80 if (!@include_once 'File/Gettext/' . $format . '.php') {
81 return File_Gettext::raiseError($php_errormsg);
83 $class = 'File_Gettext_' . $format;
84 $obref = &new $class($file);
91 * That's a simple fake of the 'msgfmt' console command. It reads the
92 * contents of a GNU PO file and saves them to a GNU MO file.
96 * @return mixed Returns true on success or PEAR_Error on failure.
97 * @param string $pofile path to GNU PO file
98 * @param string $mofile path to GNU MO file
100 function poFile2moFile($pofile, $mofile)
102 if (!is_file($pofile)) {
103 return File_Gettext::raiseError("File $pofile doesn't exist.");
106 include_once 'File/Gettext/PO.php';
108 $PO = &new File_Gettext_PO($pofile);
109 if (true !== ($e = $PO->load())) {
114 if (true !== ($e = $MO->save($mofile))) {
128 * @param string $string
129 * @param bool $reverse
131 function prepare($string, $reverse = false)
134 $smap = array('"', "\n", "\t", "\r");
135 $rmap = array('\\"', '\\n"' . "\n" . '"', '\\t', '\\r');
136 return (string) str_replace($smap, $rmap, $string);
138 $smap = array('/"\s+"/', '/\\\\n/', '/\\\\r/', '/\\\\t/', '/\\\\"/');
139 $rmap = array('', "\n", "\r", "\t", '"');
140 return (string) preg_replace($smap, $rmap, $string);
150 * @param string $meta
152 function meta2array($meta)
155 foreach (explode("\n", $meta) as $info) {
156 if ($info = trim($info)) {
157 list($key, $value) = explode(':', $info, 2);
158 $array[trim($key)] = trim($value);
167 * Returns meta info and strings as an array of a structure like that:
171 * 'Content-Type' => 'text/plain; charset=iso-8859-1',
172 * 'Last-Translator' => 'Michael Wallner <mike@iworks.at>',
173 * 'PO-Revision-Date' => '2004-07-21 17:03+0200',
174 * 'Language-Team' => 'German <mail@example.com>',
176 * 'strings' => array(
177 * 'All rights reserved' => 'Alle Rechte vorbehalten',
178 * 'Welcome' => 'Willkommen',
190 return array('meta' => $this->meta, 'strings' => $this->strings);
196 * Assigns meta info and strings from an array of a structure like that:
200 * 'Content-Type' => 'text/plain; charset=iso-8859-1',
201 * 'Last-Translator' => 'Michael Wallner <mike@iworks.at>',
202 * 'PO-Revision-Date' => date('Y-m-d H:iO'),
203 * 'Language-Team' => 'German <mail@example.com>',
205 * 'strings' => array(
206 * 'All rights reserved' => 'Alle Rechte vorbehalten',
207 * 'Welcome' => 'Willkommen',
216 * @param array $array
218 function fromArray($array)
220 if (!array_key_exists('strings', $array)) {
221 if (count($array) != 2) {
224 list($this->meta, $this->strings) = $array;
227 $this->meta = @$array['meta'];
228 $this->strings = @$array['strings'];
237 * @return object File_Gettext_MO
241 include_once 'File/Gettext/MO.php';
242 $MO = &new File_Gettext_MO;
243 $MO->fromArray($this->toArray());
251 * @return object File_Gettext_PO
255 include_once 'File/Gettext/PO.php';
256 $PO = &new File_Gettext_PO;
257 $PO->fromArray($this->toArray());
267 * @param string $error
270 function raiseError($error = null, $code = null)
272 include_once 'PEAR.php';
273 return PEAR::raiseError($error, $code);