Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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