Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
 
4
/**
5
 * Contains the Translation2_Decorator base class
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. The name of the author may not be used to endorse or promote products
17
 *    derived from this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
20
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
 * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
23
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * @category  Internationalization
31
 * @package   Translation2
32
 * @author    Lorenzo Alberton <l.alberton@quipo.it>
33
 * @copyright 2004-2005 Lorenzo Alberton
34
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
35
 * @version   CVS: $Id: Decorator.php 305985 2010-12-05 22:55:33Z clockwerx $
36
 * @link      http://pear.php.net/package/Translation2
37
 */
38
 
39
/**
40
 * Translation2_Decorator. Base Decorator class for Translation2
41
 *
42
 * Extend this class to provide custom decorators.
43
 * Some decorators are already bundled with the package.
44
 *
45
 * @category  Internationalization
46
 * @package   Translation2
47
 * @author    Lorenzo Alberton <l.alberton@quipo.it>
48
 * @copyright 2004-2005 Lorenzo Alberton
49
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
50
 * @link      http://pear.php.net/package/Translation2
51
 * @abstract
52
 */
53
class Translation2_Decorator extends Translation2
54
{
55
    // {{{ class vars
56
 
57
    /**
58
     * Translation2 object being decorated
59
     * @var object
60
     * @access protected
61
     */
62
    var $translation2;
63
 
64
    /**
65
     * @var object
66
     * @access protected
67
     */
68
    var $storage;
69
 
70
    /**
71
     * @var array
72
     * @access protected
73
     */
74
    var $lang;
75
 
76
    /**
77
     * @var string
78
     * @access protected
79
     */
80
    var $currentPageID;
81
 
82
    // }}}
83
    // {{{ Constructor
84
 
85
    /**
86
     * Constructor
87
     * Constructs the Translation2_Decorator
88
     *
89
     * @param object &$translation2 Translation2 object to decorate
90
     */
91
    function Translation2_Decorator(& $translation2)
92
    {
93
        $this->translation2 = & $translation2;
94
        //used for debug only
95
        $this->storage       = & $translation2->storage;
96
        $this->currentPageID = & $translation2->currentPageID;
97
        $this->lang          = & $translation2->lang;
98
    }
99
 
100
    // }}}
101
    // {{{ setOptions()
102
 
103
    /**
104
     * set Decorator options
105
     *
106
     * @param array $options decorator options
107
     *
108
     * @return self
109
     */
110
    function setOptions($options=array())
111
    {
112
        if (is_array($options)) {
113
            foreach ($options as $option => $value) {
114
                $this->setOption($option, $value);
115
            }
116
        }
117
        return $this;
118
    }
119
 
120
    // }}}
121
    // {{{ setOption()
122
 
123
    /**
124
     * set Decorator option
125
     *
126
     * @param string $option option name
127
     * @param mixed  $value  option value
128
     *
129
     * @return self
130
     */
131
    function setOption($option, $value=null)
132
    {
133
        if (isset($this->$option)) {
134
            $this->$option = $value;
135
        } elseif (is_a($this->translation2, 'Translation2_Decorator')) {
136
            $this->translation2->setOption($option, $value);
137
        }
138
        return $this;
139
    }
140
 
141
    // }}}
142
    // {{{ setContainerOptions()
143
 
144
    /**
145
     * Set some storage driver options
146
     *
147
     * @param array $options storage driver options
148
     *
149
     * @return void
150
     * @access protected
151
     */
152
    function setContainerOptions($options)
153
    {
154
        $this->storage->_parseOptions($options);
155
    }
156
 
157
    // }}}
158
    // {{{ getDecorator()
159
 
160
    /**
161
     * Return an instance of a decorator
162
     *
163
     * @param string $decorator Name of the decorator
164
     * @param object $object    instance [optional]
165
     *
166
     * @return object Decorator object reference
167
     * @access public
168
     */
169
    function & getDecorator($decorator)
170
    {
171
        if (func_num_args() > 1) {
172
            $obj = func_get_arg(1);
173
            $new_decorator =& $this->translation2->getDecorator($decorator, $obj);
174
        } else {
175
            $new_decorator =& $this->translation2->getDecorator($decorator, $this);
176
        }
177
        return $new_decorator;
178
    }
179
 
180
    // }}}
181
    // {{{ setCharset()
182
 
183
    /**
184
     * Set charset used to read/store the translations
185
     *
186
     * @param string $charset character set (encoding)
187
     *
188
     * @return PEAR_Error on failure
189
     */
190
    function setCharset($charset)
191
    {
192
        return $this->translation2->setCharset($charset);
193
    }
194
 
195
    // }}}
196
    // {{{ setLang()
197
 
198
    /**
199
     * Set default language
200
     *
201
     * @param string $langID language ID
202
     *
203
     * @return true|PEAR_Error
204
     */
205
    function setLang($langID)
206
    {
207
        return $this->translation2->setLang($langID);
208
    }
209
 
210
    // }}}
211
    // {{{ setPageID($pageID)
212
 
213
    /**
214
     * Set default page
215
     *
216
     * @param string $pageID page/group ID
217
     *
218
     * @return void
219
     */
220
    function setPageID($pageID = null)
221
    {
222
        $this->translation2->setPageID($pageID);
223
    }
224
 
225
    // }}}
226
    // {{{ getLang()
227
 
228
    /**
229
     * Get language info
230
     *
231
     * @param string $langID language ID
232
     * @param string $format ['name', 'meta', 'error_text', 'array']
233
     *
234
     * @return mixed [string | array], depending on $format
235
     */
236
    function getLang($langID=null, $format='name')
237
    {
238
        return $this->translation2->getLang($langID, $format);
239
    }
240
 
241
    // }}}
242
    // {{{ getLangs()
243
 
244
    /**
245
     * Get languages
246
     *
247
     * @param string $format ['ids', 'names', 'array']
248
     *
249
     * @return array
250
     */
251
    function getLangs($format='name')
252
    {
253
        return $this->translation2->getLangs($format);
254
    }
255
 
256
    // }}}
257
    // {{{ setParams()
258
 
259
    /**
260
     * Set parameters for next string
261
     *
262
     * @param array $params array of replacement parameters
263
     *
264
     * @return void
265
     */
266
    function setParams($params = null)
267
    {
268
        $this->translation2->setParams($params);
269
    }
270
 
271
    // }}}
272
    // {{{ getRaw()
273
 
274
    /**
275
     * Get translated string
276
     *
277
     * No filter is applied.
278
     *
279
     * @param string $stringID    string ID
280
     * @param string $pageID      page/group ID
281
     * @param string $langID      language ID
282
     * @param string $defaultText Text to display when the strings in both
283
     *                            the default and the fallback lang are empty
284
     *
285
     * @return string
286
     */
287
    function getRaw($stringID, $pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null, $defaultText = '')
288
    {
289
        return $this->translation2->getRaw($stringID, $pageID, $langID, $defaultText);
290
    }
291
 
292
    // }}}
293
    // {{{ get()
294
 
295
    /**
296
     * Get translated string
297
     *
298
     * All the filters are applied.
299
     *
300
     * @param string $stringID    string ID
301
     * @param string $pageID      page/group ID
302
     * @param string $langID      language ID
303
     * @param string $defaultText Text to display when the string is empty
304
     *               NB: This parameter is only used in the DefaultText decorator
305
     *
306
     * @return string
307
     */
308
    function get($stringID, $pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null, $defaultText = '')
309
    {
310
        return $this->translation2->get($stringID, $pageID, $langID, $defaultText);
311
    }
312
 
313
    // }}}
314
    // {{{ getRawPage()
315
 
316
    /**
317
     * Get the array of strings in a page
318
     *
319
     * Fetch the strings from the container, without any replacing
320
     *
321
     * @param string $pageID page/group ID
322
     * @param string $langID language ID
323
     *
324
     * @return array
325
     */
326
    function getRawPage($pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null)
327
    {
328
        return $this->translation2->getRawPage($pageID, $langID);
329
    }
330
 
331
    // }}}
332
    // {{{ getPage()
333
 
334
    /**
335
     * Same as getRawPage, but resort to fallback language and
336
     * replace parameters when needed
337
     *
338
     * @param string $pageID page/group ID
339
     * @param string $langID language ID
340
     *
341
     * @return array
342
     */
343
    function getPage($pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null)
344
    {
345
        $this->translation2->getPage($pageID, $langID);
346
    }
347
 
348
    // }}}
349
    // {{{ _replaceParams()
350
 
351
    /**
352
     * Replace parameters in strings
353
     *
354
     * @param mixed $strings strings where the replacements must occur
355
     *
356
     * @return mixed
357
     * @access protected
358
     */
359
    function _replaceParams($strings)
360
    {
361
        return $this->translation2->_replaceParams($strings);
362
    }
363
 
364
    // }}}
365
    // {{{ replaceEmptyStringsWithKeys()
366
 
367
    /**
368
     * Replace empty strings with their stringID
369
     *
370
     * @param array $strings array of strings to be replaced if empty
371
     *
372
     * @return array
373
     * @access public
374
     */
375
    function replaceEmptyStringsWithKeys($strings)
376
    {
377
        return $this->translation2->replaceEmptyStringsWithKeys($strings);
378
    }
379
 
380
    // }}}
381
    // {{{ getStringID()
382
 
383
    /**
384
     * Get the stringID for the given string. This method is the reverse of get().
385
     *
386
     * @param string $string This is NOT the stringID, this is a real string.
387
     *               The method will search for its matching stringID, and then
388
     *               it will return the associate string in the selected language.
389
     * @param string $pageID page/group ID
390
     *
391
     * @return string
392
     */
393
    function getStringID($string, $pageID = TRANSLATION2_DEFAULT_PAGEID)
394
    {
395
        return $this->translation2->getStringID($string, $pageID);
396
    }
397
 
398
    // }}}
399
    // {{{ __clone()
400
 
401
    /**
402
     * Clone internal object references
403
     *
404
     * This method is called automatically by PHP5
405
     *
406
     * @return void
407
     * @access protected
408
     */
409
    function __clone()
410
    {
411
        $this->translation2 = clone($this->translation2);
412
    }
413
 
414
    // }}}
415
}
416
?>