Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TGettext class file.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the BSD License.
7
 *
8
 * Copyright(c) 2004 by Qiang Xue. All rights reserved.
9
 *
10
 * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
11
 * The latest version of PRADO can be obtained from:
12
 * {@link http://prado.sourceforge.net/}
13
 *
14
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
15
 * @version $Revision: 1.4 $  $Date: 2005/01/09 23:36:23 $
16
 * @package System.I18N.core
17
 */
18
 
19
// +----------------------------------------------------------------------+
20
// | PEAR :: File :: Gettext                                              |
21
// +----------------------------------------------------------------------+
22
// | This source file is subject to version 3.0 of the PHP license,       |
23
// | that is available at http://www.php.net/license/3_0.txt              |
24
// | If you did not receive a copy of the PHP license and are unable      |
25
// | to obtain it through the world-wide-web, please send a note to       |
26
// | license@php.net so we can mail you a copy immediately.               |
27
// +----------------------------------------------------------------------+
28
// | Copyright (c) 2004 Michael Wallner <mike@iworks.at>                  |
29
// +----------------------------------------------------------------------+
30
//
31
// $Id: TGettext.php 2541 2008-10-21 15:05:13Z qiang.xue $
32
 
33
/**
34
 * File::Gettext
35
 *
36
 * @author      Michael Wallner <mike@php.net>
37
 * @license     PHP License
38
 */
39
 
40
/**
41
 * Use PHPs builtin error messages
42
 */
43
//ini_set('track_errors', true);
44
 
45
/**
46
 * File_Gettext
47
 *
48
 * GNU gettext file reader and writer.
49
 *
50
 * #################################################################
51
 * # All protected members of this class are public in its childs. #
52
 * #################################################################
53
 *
54
 * @author      Michael Wallner <mike@php.net>
55
 * @version     $Revision: 1.4 $
56
 * @access      public
57
 * @package System.I18N.core
58
 */
59
class TGettext
60
{
61
    /**
62
     * strings
63
     *
64
     * associative array with all [msgid => msgstr] entries
65
     *
66
     * @access  protected
67
     * @var     array
68
    */
69
    protected $strings = array();
70
 
71
    /**
72
     * meta
73
     *
74
     * associative array containing meta
75
     * information like project name or content type
76
     *
77
     * @access  protected
78
     * @var     array
79
     */
80
    protected $meta = array();
81
 
82
    /**
83
     * file path
84
     *
85
     * @access  protected
86
     * @var     string
87
     */
88
    protected $file = '';
89
 
90
    /**
91
     * Factory
92
     *
93
     * @static
94
     * @access  public
95
     * @return  object  Returns File_Gettext_PO or File_Gettext_MO on success
96
     *                  or PEAR_Error on failure.
97
     * @param   string  $format MO or PO
98
     * @param   string  $file   path to GNU gettext file
99
     */
100
    function factory($format, $file = '')
101
    {
102
        $format = strToUpper($format);
103
        $filename = dirname(__FILE__).'/'.$format.'.php';
104
        if(is_file($filename) == false)
105
        	throw new Exception ("Class file $file not found");
106
 
107
        include_once $filename;
108
        $class = 'TGettext_' . $format;
109
 
110
        return new $class($file);
111
    }
112
 
113
    /**
114
     * poFile2moFile
115
     *
116
     * That's a simple fake of the 'msgfmt' console command.  It reads the
117
     * contents of a GNU PO file and saves them to a GNU MO file.
118
     *
119
     * @static
120
     * @access  public
121
     * @return  mixed   Returns true on success or PEAR_Error on failure.
122
     * @param   string  $pofile path to GNU PO file
123
     * @param   string  $mofile path to GNU MO file
124
     */
125
    function poFile2moFile($pofile, $mofile)
126
    {
127
        if (!is_file($pofile)) {
128
            throw new Exception("File $pofile doesn't exist.");
129
        }
130
 
131
        include_once dirname(__FILE__).'/PO.php';
132
 
133
        $PO = new TGettext_PO($pofile);
134
        if (true !== ($e = $PO->load())) {
135
            return $e;
136
        }
137
 
138
        $MO = $PO->toMO();
139
        if (true !== ($e = $MO->save($mofile))) {
140
            return $e;
141
        }
142
        unset($PO, $MO);
143
 
144
        return true;
145
    }
146
 
147
    /**
148
     * prepare
149
     *
150
     * @static
151
     * @access  protected
152
     * @return  string
153
     * @param   string  $string
154
     * @param   bool    $reverse
155
     */
156
    function prepare($string, $reverse = false)
157
    {
158
        if ($reverse) {
159
            $smap = array('"', "\n", "\t", "\r");
160
            $rmap = array('\"', '\\n"' . "\n" . '"', '\\t', '\\r');
161
            return (string) str_replace($smap, $rmap, $string);
162
        } else {
163
        	$string = preg_replace('/"\s+"/', '', $string);
164
            $smap = array('\\n', '\\r', '\\t', '\"');
165
            $rmap = array("\n", "\r", "\t", '"');
166
            return (string) str_replace($smap, $rmap, $string);
167
        }
168
    }
169
 
170
    /**
171
     * meta2array
172
     *
173
     * @static
174
     * @access  public
175
     * @return  array
176
     * @param   string  $meta
177
     */
178
    function meta2array($meta)
179
    {
180
        $array = array();
181
        foreach (explode("\n", $meta) as $info) {
182
            if ($info = trim($info)) {
183
                list($key, $value) = explode(':', $info, 2);
184
                $array[trim($key)] = trim($value);
185
            }
186
        }
187
        return $array;
188
    }
189
 
190
    /**
191
     * toArray
192
     *
193
     * Returns meta info and strings as an array of a structure like that:
194
     * <code>
195
     *   array(
196
     *       'meta' => array(
197
     *           'Content-Type'      => 'text/plain; charset=iso-8859-1',
198
     *           'Last-Translator'   => 'Michael Wallner <mike@iworks.at>',
199
     *           'PO-Revision-Date'  => '2004-07-21 17:03+0200',
200
     *           'Language-Team'     => 'German <mail@example.com>',
201
     *       ),
202
     *       'strings' => array(
203
     *           'All rights reserved'   => 'Alle Rechte vorbehalten',
204
     *           'Welcome'               => 'Willkommen',
205
     *           // ...
206
     *       )
207
     *   )
208
     * </code>
209
     *
210
     * @see     fromArray()
211
     * @access  protected
212
     * @return  array
213
     */
214
    function toArray()
215
    {
216
    	return array('meta' => $this->meta, 'strings' => $this->strings);
217
    }
218
 
219
    /**
220
     * fromArray
221
     *
222
     * Assigns meta info and strings from an array of a structure like that:
223
     * <code>
224
     *   array(
225
     *       'meta' => array(
226
     *           'Content-Type'      => 'text/plain; charset=iso-8859-1',
227
     *           'Last-Translator'   => 'Michael Wallner <mike@iworks.at>',
228
     *           'PO-Revision-Date'  => date('Y-m-d H:iO'),
229
     *           'Language-Team'     => 'German <mail@example.com>',
230
     *       ),
231
     *       'strings' => array(
232
     *           'All rights reserved'   => 'Alle Rechte vorbehalten',
233
     *           'Welcome'               => 'Willkommen',
234
     *           // ...
235
     *       )
236
     *   )
237
     * </code>
238
     *
239
     * @see     toArray()
240
     * @access  protected
241
     * @return  bool
242
     * @param   array       $array
243
     */
244
    function fromArray($array)
245
    {
246
    	if (!array_key_exists('strings', $array)) {
247
    	    if (count($array) != 2) {
248
                return false;
249
    	    } else {
250
    	        list($this->meta, $this->strings) = $array;
251
            }
252
    	} else {
253
            $this->meta = @$array['meta'];
254
            $this->strings = @$array['strings'];
255
        }
256
        return true;
257
    }
258
 
259
    /**
260
     * toMO
261
     *
262
     * @access  protected
263
     * @return  object  File_Gettext_MO
264
     */
265
    function toMO()
266
    {
267
        include_once dirname(__FILE__).'/MO.php';
268
        $MO = new TGettext_MO;
269
        $MO->fromArray($this->toArray());
270
        return $MO;
271
    }
272
 
273
    /**
274
     * toPO
275
     *
276
     * @access  protected
277
     * @return  object      File_Gettext_PO
278
     */
279
    function toPO()
280
    {
281
        include_once dirname(__FILE__).'/PO.php';
282
        $PO = new TGettext_PO;
283
        $PO->fromArray($this->toArray());
284
        return $PO;
285
    }
286
}