| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
|
|
3 |
|
|
|
4 |
/**
|
|
|
5 |
* Contains the Translation2_Decorator_DefaultText 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 Rolf 'Red' Ochsenbein <red@raven.ch>
|
|
|
34 |
* @copyright 2004-2007 Lorenzo Alberton, Rolf 'Red' Ochsenbein
|
|
|
35 |
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
|
|
|
36 |
* @version CVS: $Id: DefaultText.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 |
* Decorator to provide a fallback text for empty strings.
|
|
|
47 |
*
|
|
|
48 |
* If the string is empty, return the <parameter>defaultText</parameter> parameter.
|
|
|
49 |
* If the <parameter>defaultText</parameter> parameter is empty too, then return
|
|
|
50 |
* "$emptyPostfix.$outputString.$emptyPrefix", the three variables
|
|
|
51 |
* being class properties you can set to a custom string.
|
|
|
52 |
*
|
|
|
53 |
* @category Internationalization
|
|
|
54 |
* @package Translation2
|
|
|
55 |
* @author Lorenzo Alberton <l.alberton@quipo.it>
|
|
|
56 |
* @author Rolf 'Red' Ochsenbein <red@raven.ch>
|
|
|
57 |
* @copyright 2004-2007 Lorenzo Alberton, Rolf 'Red' Ochsenbein
|
|
|
58 |
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
|
|
|
59 |
* @version CVS: $Id: DefaultText.php 305985 2010-12-05 22:55:33Z clockwerx $
|
|
|
60 |
* @link http://pear.php.net/package/Translation2
|
|
|
61 |
*/
|
|
|
62 |
class Translation2_Decorator_DefaultText extends Translation2_Decorator
|
|
|
63 |
{
|
|
|
64 |
// {{{ class vars
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* String appended to the returned string when the string is empty
|
|
|
68 |
* and it's replaced by its $stringID. It can be used to mark unreplaced
|
|
|
69 |
* strings.
|
|
|
70 |
* @var string
|
|
|
71 |
* @access protected
|
|
|
72 |
*/
|
|
|
73 |
var $emptyPostfix = '';
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* String prepended to the returned string when the string is empty
|
|
|
77 |
* and it's replaced by its $stringID. It can be used to mark unreplaced
|
|
|
78 |
* strings.
|
|
|
79 |
* @var string
|
|
|
80 |
* @access protected
|
|
|
81 |
*/
|
|
|
82 |
var $emptyPrefix = '';
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* String to output when there was no translation
|
|
|
86 |
* %stringID% will be replaced with the stringID
|
|
|
87 |
* %stringID_url% will replaced with a urlencoded stringID
|
|
|
88 |
* %url% will be replaced with the targeted url
|
|
|
89 |
* @var string
|
|
|
90 |
* @access protected
|
|
|
91 |
*/
|
|
|
92 |
//var $outputString = '%stringID%<a href="%url%">(T)</a>';
|
|
|
93 |
var $outputString = '%stringID%';
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Targeted URL of strings without translations
|
|
|
97 |
* @var string
|
|
|
98 |
* @access protected
|
|
|
99 |
*/
|
|
|
100 |
var $url = '#';
|
|
|
101 |
|
|
|
102 |
// }}}
|
|
|
103 |
// {{{ get()
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Get translated string
|
|
|
107 |
*
|
|
|
108 |
* If the string is empty, return the $defaultText if not empty,
|
|
|
109 |
* the $stringID otherwise.
|
|
|
110 |
*
|
|
|
111 |
* @param string $stringID string ID
|
|
|
112 |
* @param string $pageID page/group ID
|
|
|
113 |
* @param string $langID language ID
|
|
|
114 |
* @param string $defaultText Text to display when the string is empty
|
|
|
115 |
*
|
|
|
116 |
* @return string
|
|
|
117 |
*/
|
|
|
118 |
function get($stringID, $pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null, $defaultText = '')
|
|
|
119 |
{
|
|
|
120 |
if ($pageID == TRANSLATION2_DEFAULT_PAGEID) {
|
|
|
121 |
$pageID = $this->translation2->currentPageID;
|
|
|
122 |
}
|
|
|
123 |
$str = $this->translation2->get($stringID, $pageID, $langID);
|
|
|
124 |
if (!empty($str)) {
|
|
|
125 |
return $str;
|
|
|
126 |
}
|
|
|
127 |
if (!empty($defaultText)) {
|
|
|
128 |
return $this->_replaceParams($defaultText);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
$search = array(
|
|
|
132 |
'%stringID%',
|
|
|
133 |
'%stringID_url%',
|
|
|
134 |
'%pageID_url%',
|
|
|
135 |
'%url%'
|
|
|
136 |
);
|
|
|
137 |
$replace = array(
|
|
|
138 |
$stringID,
|
|
|
139 |
urlencode($stringID),
|
|
|
140 |
urlencode($pageID),
|
|
|
141 |
$this->url
|
|
|
142 |
);
|
|
|
143 |
return $this->_replaceParams(
|
|
|
144 |
$this->emptyPrefix
|
|
|
145 |
.str_replace($search, $replace, $this->outputString)
|
|
|
146 |
.$this->emptyPostfix
|
|
|
147 |
);
|
|
|
148 |
//$str = (empty($defaultText) ? $this->emptyPrefix.$stringID.$this->emptyPostfix : $defaultText);
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
// }}}
|
|
|
152 |
// {{{ getPage()
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* Replace empty strings with their $stringID
|
|
|
156 |
*
|
|
|
157 |
* @param string $pageID page/group ID
|
|
|
158 |
* @param string $langID language ID
|
|
|
159 |
*
|
|
|
160 |
* @return array
|
|
|
161 |
*/
|
|
|
162 |
function getPage($pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null)
|
|
|
163 |
{
|
|
|
164 |
$data = $this->translation2->getPage($pageID, $langID);
|
|
|
165 |
return $this->replaceEmptyStringsWithKeys($data);
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
// }}}
|
|
|
169 |
// {{{ getStringID
|
|
|
170 |
|
|
|
171 |
/**
|
|
|
172 |
* Get the stringID for the given string. This method is the reverse of get().
|
|
|
173 |
* If the requested string is unknown to the system,
|
|
|
174 |
* the requested string will be returned.
|
|
|
175 |
*
|
|
|
176 |
* @param string $string This is NOT the stringID, this is a real string.
|
|
|
177 |
* The method will search for its matching stringID,
|
|
|
178 |
* and then it will return the associate string in the
|
|
|
179 |
* selected language.
|
|
|
180 |
* @param string $pageID page/group ID
|
|
|
181 |
*
|
|
|
182 |
* @return string
|
|
|
183 |
*/
|
|
|
184 |
function &getStringID($string, $pageID = TRANSLATION2_DEFAULT_PAGEID)
|
|
|
185 |
{
|
|
|
186 |
if ($pageID == TRANSLATION2_DEFAULT_PAGEID) {
|
|
|
187 |
$pageID = $this->translation2->currentPageID;
|
|
|
188 |
}
|
|
|
189 |
$stringID = $this->storage->getStringID($string, $pageID);
|
|
|
190 |
if (empty($stringID)) {
|
|
|
191 |
$stringID = $string;
|
|
|
192 |
}
|
|
|
193 |
return $stringID;
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
?>
|