| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
|
|
3 |
|
|
|
4 |
/**
|
|
|
5 |
* Contains the Translation2_Decorator_CacheMemory 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-2007 Lorenzo Alberton
|
|
|
34 |
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
|
|
|
35 |
* @version CVS: $Id: CacheMemory.php 305985 2010-12-05 22:55:33Z clockwerx $
|
|
|
36 |
* @link http://pear.php.net/package/Translation2
|
|
|
37 |
*/
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Load Translation2 decorator base class
|
|
|
41 |
*/
|
|
|
42 |
require_once 'Translation2/Decorator.php';
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Allows redefinition of alternate key for empty pageID
|
|
|
46 |
*/
|
|
|
47 |
if (!defined('TRANSLATION2_EMPTY_PAGEID_KEY')) {
|
|
|
48 |
define('TRANSLATION2_EMPTY_PAGEID_KEY', 'array_key_4_empty_pageID');
|
|
|
49 |
}
|
|
|
50 |
/**
|
|
|
51 |
* Allows redefinition of alternate key for null pageID
|
|
|
52 |
*/
|
|
|
53 |
if (!defined('TRANSLATION2_NULL_PAGEID_KEY')) {
|
|
|
54 |
define('TRANSLATION2_NULL_PAGEID_KEY', 'array_key_4_null_pageID');
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Decorator to cache fetched data in memory
|
|
|
59 |
*
|
|
|
60 |
* @category Internationalization
|
|
|
61 |
* @package Translation2
|
|
|
62 |
* @author Lorenzo Alberton <l.alberton@quipo.it>
|
|
|
63 |
* @copyright 2004-2007 Lorenzo Alberton
|
|
|
64 |
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
|
|
|
65 |
* @version CVS: $Id: CacheMemory.php 305985 2010-12-05 22:55:33Z clockwerx $
|
|
|
66 |
* @link http://pear.php.net/package/Translation2
|
|
|
67 |
*/
|
|
|
68 |
class Translation2_Decorator_CacheMemory extends Translation2_Decorator
|
|
|
69 |
{
|
|
|
70 |
// {{{ class vars
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Translated strings array
|
|
|
74 |
* Used for cache purposes.
|
|
|
75 |
* No parameter substitution or fallback langs here.
|
|
|
76 |
* @var array
|
|
|
77 |
* @access protected
|
|
|
78 |
*/
|
|
|
79 |
var $rawData = array();
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* set prefetch on/off
|
|
|
83 |
* @var boolean
|
|
|
84 |
* @access protected
|
|
|
85 |
*/
|
|
|
86 |
var $prefetch = true;
|
|
|
87 |
|
|
|
88 |
// }}}
|
|
|
89 |
// {{{ _getPageIDKey()
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* return a valid array key based on pageID value
|
|
|
93 |
*
|
|
|
94 |
* @param mixed $pageID (string or null)
|
|
|
95 |
*
|
|
|
96 |
* @return string
|
|
|
97 |
* @access private
|
|
|
98 |
*/
|
|
|
99 |
function _getPageIDKey($pageID)
|
|
|
100 |
{
|
|
|
101 |
if (is_null($pageID)) {
|
|
|
102 |
return TRANSLATION2_NULL_PAGEID_KEY;
|
|
|
103 |
}
|
|
|
104 |
if (empty($pageID)) {
|
|
|
105 |
return TRANSLATION2_EMPTY_PAGEID_KEY;
|
|
|
106 |
}
|
|
|
107 |
if ($pageID == TRANSLATION2_DEFAULT_PAGEID) {
|
|
|
108 |
return $this->translation2->currentPageID;
|
|
|
109 |
}
|
|
|
110 |
return $pageID;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
// }}}
|
|
|
114 |
// {{{ getRaw()
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Get translated string (as-is)
|
|
|
118 |
*
|
|
|
119 |
* First check if the string is cached, if not => fetch the page
|
|
|
120 |
* from the container and cache it for later use.
|
|
|
121 |
*
|
|
|
122 |
* @param string $stringID string ID
|
|
|
123 |
* @param string $pageID page/group ID
|
|
|
124 |
* @param string $langID language ID
|
|
|
125 |
* @param string $defaultText Text to display when the strings in both
|
|
|
126 |
* the default and the fallback lang are empty
|
|
|
127 |
*
|
|
|
128 |
* @return string
|
|
|
129 |
*/
|
|
|
130 |
function getRaw($stringID, $pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null, $defaultText = null)
|
|
|
131 |
{
|
|
|
132 |
$pageID_key = $this->_getPageIDKey($pageID);
|
|
|
133 |
$langID_key = empty($langID) ? $this->translation2->lang['id'] : $langID;
|
|
|
134 |
|
|
|
135 |
if (!array_key_exists($langID_key, $this->rawData)) {
|
|
|
136 |
$this->rawData[$langID_key] = array();
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
if ($this->prefetch) {
|
|
|
140 |
$this->getRawPage($pageID, $langID);
|
|
|
141 |
}
|
|
|
142 |
if (array_key_exists($pageID_key, $this->rawData[$langID_key])) {
|
|
|
143 |
if (PEAR::isError($this->rawData[$langID_key][$pageID_key])) {
|
|
|
144 |
return $this->rawData[$langID_key][$pageID_key];
|
|
|
145 |
}
|
|
|
146 |
$str = (isset($this->rawData[$langID_key][$pageID_key][$stringID]) ?
|
|
|
147 |
$this->rawData[$langID_key][$pageID_key][$stringID] : ''); //empty string or null value?
|
|
|
148 |
} else {
|
|
|
149 |
$str = $this->translation2->getRaw($stringID, $pageID, $langID, $defaultText);
|
|
|
150 |
}
|
|
|
151 |
return $str;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
// }}}
|
|
|
155 |
// {{{ get()
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* Get translated string
|
|
|
159 |
*
|
|
|
160 |
* First check if the string is cached, if not => fetch the page
|
|
|
161 |
* from the container and cache it for later use.
|
|
|
162 |
*
|
|
|
163 |
* @param string $stringID string ID
|
|
|
164 |
* @param string $pageID page/group ID
|
|
|
165 |
* @param string $langID language ID
|
|
|
166 |
* @param string $defaultText Text to display when the strings in both
|
|
|
167 |
* the default and the fallback lang are empty
|
|
|
168 |
*
|
|
|
169 |
* @return string
|
|
|
170 |
*/
|
|
|
171 |
function get($stringID, $pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null, $defaultText = null)
|
|
|
172 |
{
|
|
|
173 |
$str = $this->getRaw($stringID, $pageID, $langID, $defaultText);
|
|
|
174 |
return $this->_replaceParams($str);
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
// }}}
|
|
|
178 |
// {{{ getRawPage()
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Get the array of strings in a page
|
|
|
182 |
*
|
|
|
183 |
* First check if the strings are cached, if not => fetch the page
|
|
|
184 |
* from the container and cache it for later use.
|
|
|
185 |
*
|
|
|
186 |
* @param string $pageID page/group ID
|
|
|
187 |
* @param string $langID language ID
|
|
|
188 |
*
|
|
|
189 |
* @return array
|
|
|
190 |
*/
|
|
|
191 |
function getRawPage($pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null)
|
|
|
192 |
{
|
|
|
193 |
$pageID_key = $this->_getPageIDKey($pageID);
|
|
|
194 |
$langID_key = empty($langID) ? $this->translation2->lang['id'] : $langID;
|
|
|
195 |
|
|
|
196 |
if (!array_key_exists($langID_key, $this->rawData)) {
|
|
|
197 |
$this->rawData[$langID_key] = array();
|
|
|
198 |
}
|
|
|
199 |
if (!array_key_exists($pageID_key, $this->rawData[$langID_key])) {
|
|
|
200 |
$this->rawData[$langID_key][$pageID_key] =
|
|
|
201 |
$this->translation2->getRawPage($pageID, $langID);
|
|
|
202 |
}
|
|
|
203 |
return $this->rawData[$langID_key][$pageID_key];
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
// }}}
|
|
|
207 |
// {{{ getPage()
|
|
|
208 |
|
|
|
209 |
/**
|
|
|
210 |
* Same as getRawPage, but resort to fallback language and
|
|
|
211 |
* replace parameters when needed
|
|
|
212 |
*
|
|
|
213 |
* @param string $pageID page/group ID
|
|
|
214 |
* @param string $langID language ID
|
|
|
215 |
*
|
|
|
216 |
* @return array
|
|
|
217 |
*/
|
|
|
218 |
function getPage($pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null)
|
|
|
219 |
{
|
|
|
220 |
$pageID_key = $this->_getPageIDKey($pageID);
|
|
|
221 |
$langID_key = empty($langID) ? $this->translation2->lang['id'] : $langID;
|
|
|
222 |
|
|
|
223 |
$this->getRawPage($pageID, $langID);
|
|
|
224 |
return $this->_replaceParams($this->rawData[$langID_key][$pageID_key]);
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
// }}}
|
|
|
228 |
}
|
|
|
229 |
?>
|