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_Iconv 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
 * @author    Sergey Korotkov <sergey@pushok.com>
34
 * @copyright 2004-2007 Lorenzo Alberton, Sergey Korotkov
35
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
36
 * @version   CVS: $Id: Iconv.php 305985 2010-12-05 22:55:33Z clockwerx $
37
 * @link      http://pear.php.net/package/Translation2
38
 */
39
 
40
/**
41
 * Load Translation2 decorator base class
42
 */
43
require_once 'Translation2/Decorator.php';
44
 
45
/**
46
 * Translation2 Iconv Decorator
47
 *
48
 * Decorator to change the encoding of the stored translation to the
49
 * one given in the 'encoding' option.
50
 *
51
 * <code>
52
 * $tr->setOptions(array('encoding' => 'UTF-8'));
53
 * </code>
54
 *
55
 * @category  Internationalization
56
 * @package   Translation2
57
 * @author    Lorenzo Alberton <l.alberton@quipo.it>
58
 * @author    Sergey Korotkov <sergey@pushok.com>
59
 * @copyright 2004-2007 Lorenzo Alberton, Sergey Korotkov
60
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
61
 * @version   CVS: $Id: Iconv.php 305985 2010-12-05 22:55:33Z clockwerx $
62
 * @link      http://pear.php.net/package/Translation2
63
 * @see       http://www.php.net/htmlentities for a list of available encodings.
64
 */
65
class Translation2_Decorator_Iconv extends Translation2_Decorator
66
{
67
    // {{{ class vars
68
 
69
    /**
70
     * @var string
71
     * @access private
72
     */
73
    var $encoding = 'ISO-8859-1';
74
 
75
    /**
76
     * @var array
77
     * @access private
78
     */
79
    var $lang_encodings;
80
 
81
    // }}}
82
    // {{{ _getEncoding()
83
 
84
    /**
85
     * Get the encoding for the given langID
86
     *
87
     * @param string $langID language ID
88
     *
89
     * @return string encoding
90
     * @access private
91
     */
92
    function _getEncoding($langID = null)
93
    {
94
        if (!is_array($this->lang_encodings)) {
95
            $this->lang_encodings = array();
96
            foreach ($this->translation2->getLangs('encodings') as $langID => $encoding) {
97
                $this->lang_encodings[$langID] = $encoding;
98
            }
99
        }
100
        if (!is_null($langID) && isset($this->lang_encodings[$langID])) {
101
            return $this->lang_encodings[$langID];
102
        }
103
        return $this->lang['encoding'];
104
    }
105
 
106
    // }}}
107
    // {{{ get()
108
 
109
    /**
110
     * Get the translated string, in the new encoding
111
     *
112
     * @param string $stringID    string ID
113
     * @param string $pageID      page/group ID
114
     * @param string $langID      language ID
115
     * @param string $defaultText Text to display when the string is empty
116
     *
117
     * @return string
118
     */
119
    function get($stringID, $pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null, $defaultText = null)
120
    {
121
        $str = $this->translation2->get($stringID, $pageID, $langID, $defaultText);
122
        if (PEAR::isError($str) || empty($str)) {
123
            return $str;
124
        }
125
        return iconv($this->_getEncoding($langID), $this->encoding, $str);
126
    }
127
 
128
    // }}}
129
    // {{{ getPage()
130
 
131
    /**
132
     * Same as getRawPage, but apply transformations when needed
133
     *
134
     * @param string $pageID page/group ID
135
     * @param string $langID language ID
136
     *
137
     * @return array
138
     */
139
    function getPage($pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null)
140
    {
141
        $data = $this->translation2->getPage($pageID, $langID);
142
        if (PEAR::isError($data)) {
143
            return $data;
144
        }
145
        $input_encoding = $this->_getEncoding($langID);
146
        foreach (array_keys($data) as $k) {
147
            if (!empty($data[$k])) {
148
                $data[$k] = iconv($input_encoding, $this->encoding, $data[$k]);
149
            }
150
        }
151
        return $data;
152
    }
153
 
154
    // }}}
155
}
156
?>