1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
/**
|
3 |
|
|
* @file
|
4 |
|
|
* API for internationalization strings
|
5 |
|
|
*/
|
6 |
|
|
|
7 |
|
|
/**
|
8 |
|
|
* String object that contains source and translations.
|
9 |
|
|
*
|
10 |
|
|
* Note all database operations must go through textgroup object so we can switch storage at some point.
|
11 |
|
|
*/
|
12 |
|
|
class i18n_string_object {
|
13 |
|
|
// Updated source string
|
14 |
|
|
public $string;
|
15 |
|
|
// Properties from locale source
|
16 |
|
|
public $lid;
|
17 |
|
|
public $source;
|
18 |
|
|
public $textgroup;
|
19 |
|
|
public $location;
|
20 |
|
|
public $context;
|
21 |
|
|
public $version;
|
22 |
|
|
// Properties from i18n_tring
|
23 |
|
|
public $type;
|
24 |
|
|
public $objectid;
|
25 |
|
|
public $property;
|
26 |
|
|
public $objectkey;
|
27 |
|
|
public $format;
|
28 |
|
|
// Properties from metadata
|
29 |
|
|
public $title;
|
30 |
|
|
// Array of translations to multiple languages
|
31 |
6331c987
|
Assos Assos
|
public $translations = array();
|
32 |
85ad3d82
|
Assos Assos
|
// Textgroup object
|
33 |
|
|
protected $_textgroup;
|
34 |
|
|
|
35 |
|
|
/**
|
36 |
|
|
* Class constructor
|
37 |
|
|
*/
|
38 |
|
|
public function __construct($data = NULL) {
|
39 |
|
|
if ($data) {
|
40 |
|
|
$this->set_properties($data);
|
41 |
|
|
}
|
42 |
6331c987
|
Assos Assos
|
// Attempt to re-build the data from the persistent cache.
|
43 |
|
|
$this->rebuild_from_cache($data);
|
44 |
85ad3d82
|
Assos Assos
|
}
|
45 |
6331c987
|
Assos Assos
|
|
46 |
|
|
/**
|
47 |
|
|
* Rebuild the object data based on the persistent cache.
|
48 |
|
|
*
|
49 |
|
|
* Since the textgroup defines if a string is cacheable or not the caching
|
50 |
|
|
* of the string objects happens in the textgroup handler itself.
|
51 |
|
|
*
|
52 |
|
|
* @see i18n_string_textgroup_cached::__destruct()
|
53 |
|
|
*/
|
54 |
|
|
protected function rebuild_from_cache($data = NULL) {
|
55 |
|
|
// Check if we've the required information to repopulate the cache and do so
|
56 |
|
|
// if possible.
|
57 |
|
|
$meta_data_exist = isset($this->textgroup) && isset($this->type) && isset($this->objectid) && isset($this->property);
|
58 |
|
|
if ($meta_data_exist && ($cache = cache_get($this->get_cid())) && !empty($cache->data)) {
|
59 |
|
|
// Re-spawn the cached data.
|
60 |
|
|
// @TODO do we need a array_diff to ensure we don't overwrite the data
|
61 |
|
|
// provided by the $data parameter?
|
62 |
|
|
$this->set_properties($cache->data);
|
63 |
|
|
}
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
/**
|
67 |
|
|
* Reset cache, needed for tests.
|
68 |
|
|
*/
|
69 |
|
|
public function cache_reset() {
|
70 |
|
|
$this->translations = array();
|
71 |
|
|
// Ensure a possible persistent cache of this object is cleared too.
|
72 |
|
|
cache_clear_all($this->get_cid(), 'cache', TRUE);
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
/**
|
76 |
|
|
* Returns the caching id for this object.
|
77 |
|
|
*
|
78 |
|
|
* @return string
|
79 |
|
|
* The caching id.
|
80 |
|
|
*/
|
81 |
|
|
public function get_cid() {
|
82 |
|
|
return 'i18n:string:obj:' . $this->get_name();
|
83 |
|
|
}
|
84 |
|
|
|
85 |
85ad3d82
|
Assos Assos
|
/**
|
86 |
|
|
* Get message parameters from context and string.
|
87 |
|
|
*/
|
88 |
|
|
public function get_args() {
|
89 |
|
|
return array(
|
90 |
|
|
'%location' => $this->location,
|
91 |
|
|
'%textgroup' => $this->textgroup,
|
92 |
|
|
'%string' => ($string = $this->get_string()) ? $string : t('[empty string]'),
|
93 |
|
|
);
|
94 |
|
|
}
|
95 |
|
|
/**
|
96 |
|
|
* Set context properties
|
97 |
|
|
*/
|
98 |
|
|
public function set_context($context) {
|
99 |
|
|
$parts = is_array($context) ? $context : explode(':', $context);
|
100 |
|
|
$this->context = is_array($context) ? implode(':', $context) : $context;
|
101 |
|
|
// Location will be the full string name
|
102 |
|
|
$this->location = $this->textgroup . ':' . $this->context;
|
103 |
|
|
$this->type = array_shift($parts);
|
104 |
|
|
$this->objectid = $parts ? array_shift($parts) : '';
|
105 |
|
|
$this->objectkey = (int)$this->objectid;
|
106 |
|
|
// Remaining elements glued again with ':'
|
107 |
|
|
$this->property = $parts ? implode(':', $parts) : '';
|
108 |
6331c987
|
Assos Assos
|
|
109 |
|
|
// Attempt to re-build the other data from the persistent cache.
|
110 |
|
|
$this->rebuild_from_cache();
|
111 |
|
|
|
112 |
85ad3d82
|
Assos Assos
|
return $this;
|
113 |
|
|
}
|
114 |
|
|
/**
|
115 |
|
|
* Get string name including textgroup and context
|
116 |
|
|
*/
|
117 |
|
|
public function get_name() {
|
118 |
|
|
return $this->textgroup . ':' . $this->type . ':' . $this->objectid . ':' . $this->property;
|
119 |
|
|
}
|
120 |
|
|
/**
|
121 |
|
|
* Get source string
|
122 |
|
|
*/
|
123 |
|
|
public function get_string() {
|
124 |
|
|
if (isset($this->string)) {
|
125 |
|
|
return $this->string;
|
126 |
|
|
}
|
127 |
|
|
elseif (isset($this->source)) {
|
128 |
|
|
return $this->source;
|
129 |
|
|
}
|
130 |
|
|
elseif ($this->textgroup()->debug) {
|
131 |
|
|
return empty($this->lid) ? t('[Source not found]') : t('[String not found]');
|
132 |
|
|
}
|
133 |
|
|
else {
|
134 |
|
|
return '';
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
/**
|
138 |
|
|
* Set source string
|
139 |
|
|
*
|
140 |
|
|
* @param $string
|
141 |
|
|
* Plain string or array with 'string', 'format', etc...
|
142 |
|
|
*/
|
143 |
|
|
public function set_string($string) {
|
144 |
|
|
if (is_array($string)) {
|
145 |
|
|
$this->string = isset($string['string']) ? $string['string'] : NULL;
|
146 |
|
|
if (isset($string['format'])) {
|
147 |
|
|
$this->format = $string['format'];
|
148 |
|
|
}
|
149 |
6331c987
|
Assos Assos
|
if (isset($string['title'])) {
|
150 |
|
|
$this->title = $string['title'];
|
151 |
|
|
}
|
152 |
85ad3d82
|
Assos Assos
|
}
|
153 |
|
|
else {
|
154 |
|
|
$this->string = $string;
|
155 |
|
|
}
|
156 |
6331c987
|
Assos Assos
|
|
157 |
85ad3d82
|
Assos Assos
|
return $this;
|
158 |
|
|
}
|
159 |
|
|
/**
|
160 |
|
|
* Get string title.
|
161 |
|
|
*/
|
162 |
|
|
public function get_title() {
|
163 |
|
|
return isset($this->title) ? $this->title : t('String');
|
164 |
|
|
}
|
165 |
|
|
/**
|
166 |
|
|
* Get translation to language from string object
|
167 |
|
|
*/
|
168 |
|
|
public function get_translation($langcode) {
|
169 |
e4c061ad
|
Assos Assos
|
if (!isset($this->translations[$langcode])) {
|
170 |
85ad3d82
|
Assos Assos
|
$translation = $this->textgroup()->load_translation($this, $langcode);
|
171 |
|
|
if ($translation && isset($translation->translation)) {
|
172 |
|
|
$this->set_translation($translation, $langcode);
|
173 |
|
|
}
|
174 |
|
|
else {
|
175 |
|
|
// No source, no translation
|
176 |
|
|
$this->translations[$langcode] = FALSE;
|
177 |
|
|
}
|
178 |
|
|
}
|
179 |
|
|
// Which doesn't mean we've got a translation, only that we've got the result cached
|
180 |
|
|
return $this->translations[$langcode];
|
181 |
|
|
}
|
182 |
|
|
/**
|
183 |
|
|
* Set translation for language
|
184 |
|
|
*
|
185 |
|
|
* @param $translation
|
186 |
|
|
* Translation object (from database) or string
|
187 |
|
|
*/
|
188 |
|
|
public function set_translation($translation, $langcode = NULL) {
|
189 |
|
|
if (is_object($translation)) {
|
190 |
|
|
$langcode = $langcode ? $langcode : $translation->language;
|
191 |
|
|
$string = isset($translation->translation) ? $translation->translation : FALSE;
|
192 |
|
|
$this->set_properties($translation);
|
193 |
|
|
}
|
194 |
|
|
else {
|
195 |
|
|
$string = $translation;
|
196 |
|
|
}
|
197 |
|
|
$this->translations[$langcode] = $string;
|
198 |
|
|
return $this;
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
/**
|
202 |
|
|
* Format the resulting translation or the default string applying callbacks
|
203 |
|
|
*
|
204 |
|
|
* There's a hidden variable, 'i18n_string_debug', that when set to TRUE will display additional info
|
205 |
|
|
*/
|
206 |
|
|
public function format_translation($langcode, $options = array()) {
|
207 |
|
|
$options += array('langcode' => $langcode, 'sanitize' => TRUE, 'cache' => FALSE, 'debug' => $this->textgroup()->debug);
|
208 |
|
|
if ($translation = $this->get_translation($langcode)) {
|
209 |
|
|
$string = $translation;
|
210 |
|
|
if (isset($options['filter'])) {
|
211 |
|
|
$string = call_user_func($options['filter'], $string);
|
212 |
|
|
}
|
213 |
|
|
}
|
214 |
|
|
else {
|
215 |
|
|
// Get default source string if no translation.
|
216 |
|
|
$string = $this->get_string();
|
217 |
|
|
$options['sanitize'] = !empty($options['sanitize default']);
|
218 |
|
|
}
|
219 |
|
|
if (!empty($this->format)) {
|
220 |
|
|
$options += array('format' => $this->format);
|
221 |
|
|
}
|
222 |
|
|
// Add debug information if enabled
|
223 |
|
|
if ($options['debug']) {
|
224 |
|
|
$info = array($langcode, $this->textgroup, $this->context);
|
225 |
|
|
if (!empty($this->format)) {
|
226 |
|
|
$info[] = $this->format;
|
227 |
|
|
}
|
228 |
|
|
$options += array('suffix' => '');
|
229 |
|
|
$options['suffix'] .= ' [' . implode(':', $info) . ']';
|
230 |
|
|
}
|
231 |
|
|
// Finally, apply options, filters, callback, etc...
|
232 |
|
|
return i18n_string_format($string, $options);
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
/**
|
236 |
|
|
* Get source string provided a string object.
|
237 |
|
|
*
|
238 |
|
|
* @return
|
239 |
|
|
* String object if source exists.
|
240 |
|
|
*/
|
241 |
|
|
public function get_source() {
|
242 |
|
|
// If already searched and not found we don't have a source,
|
243 |
|
|
if (isset($this->lid) && !$this->lid) {
|
244 |
|
|
return NULL;
|
245 |
|
|
}
|
246 |
|
|
elseif (!isset($this->lid) || !isset($this->source)) {
|
247 |
|
|
// We may have lid from loading a translation but not loaded the source yet.
|
248 |
|
|
if ($source = $this->textgroup()->load_source($this)) {
|
249 |
|
|
// Set properties but don't override existing ones
|
250 |
|
|
$this->set_properties($source, FALSE, FALSE);
|
251 |
|
|
if (!isset($this->string)) {
|
252 |
|
|
$this->string = $source->source;
|
253 |
|
|
}
|
254 |
|
|
return $this;
|
255 |
|
|
}
|
256 |
|
|
else {
|
257 |
|
|
$this->lid = FALSE;
|
258 |
|
|
return NULL;
|
259 |
|
|
}
|
260 |
|
|
}
|
261 |
|
|
else {
|
262 |
|
|
return $this;
|
263 |
|
|
}
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
/**
|
267 |
|
|
* Set properties from object or array
|
268 |
|
|
*
|
269 |
|
|
* @param $properties
|
270 |
|
|
* Obejct or array of properties
|
271 |
|
|
* @param $set_null
|
272 |
|
|
* Whether to set null properties too
|
273 |
|
|
* @param $override
|
274 |
|
|
* Whether to set properties that are already set in this object
|
275 |
|
|
*/
|
276 |
|
|
public function set_properties($properties, $set_null = TRUE, $override = TRUE) {
|
277 |
|
|
foreach ((array)$properties as $field => $value) {
|
278 |
|
|
if (property_exists($this, $field) && ($set_null || isset($value)) && ($override || !isset($this->$field))) {
|
279 |
|
|
$this->$field = $value;
|
280 |
|
|
}
|
281 |
|
|
}
|
282 |
|
|
return $this;
|
283 |
|
|
}
|
284 |
|
|
/**
|
285 |
|
|
* Access textgroup object
|
286 |
|
|
*/
|
287 |
|
|
protected function textgroup() {
|
288 |
|
|
if (!isset($this->_textgroup)) {
|
289 |
|
|
$this->_textgroup = i18n_string_textgroup($this->textgroup);
|
290 |
|
|
}
|
291 |
|
|
return $this->_textgroup;
|
292 |
|
|
}
|
293 |
|
|
/**
|
294 |
|
|
* Update this string.
|
295 |
|
|
*/
|
296 |
|
|
public function update($options = array()) {
|
297 |
|
|
return $this->textgroup()->string_update($this, $options);
|
298 |
|
|
}
|
299 |
|
|
/**
|
300 |
|
|
* Delete this string.
|
301 |
|
|
*/
|
302 |
|
|
public function remove($options = array()) {
|
303 |
|
|
return $this->textgroup()->string_remove($this, $options);
|
304 |
|
|
}
|
305 |
|
|
/**
|
306 |
|
|
* Check whether there is any problem for the user to translate a this string.
|
307 |
|
|
*
|
308 |
|
|
* @param $account
|
309 |
|
|
* Optional user account, defaults to current user.
|
310 |
|
|
*
|
311 |
|
|
* @return
|
312 |
|
|
* None if the user has access to translate the string.
|
313 |
|
|
* Error message if the user cannot translate that string.
|
314 |
|
|
*/
|
315 |
|
|
public function check_translate_access($account = NULL) {
|
316 |
|
|
return i18n_string_translate_check_string($this, $account);
|
317 |
|
|
}
|
318 |
|
|
}
|
319 |
|
|
|
320 |
|
|
/**
|
321 |
|
|
* Textgroup handler for i18n_string API
|
322 |
|
|
*/
|
323 |
|
|
class i18n_string_textgroup_default {
|
324 |
|
|
// Text group name
|
325 |
|
|
public $textgroup;
|
326 |
|
|
// Debug flag, set to true to print out more information.
|
327 |
|
|
public $debug;
|
328 |
|
|
// Cached or preloaded string objects
|
329 |
6331c987
|
Assos Assos
|
public $strings = array();
|
330 |
85ad3d82
|
Assos Assos
|
// Multiple translations search map
|
331 |
6331c987
|
Assos Assos
|
protected $cache_multiple = array();
|
332 |
85ad3d82
|
Assos Assos
|
|
333 |
|
|
/**
|
334 |
|
|
* Class constructor.
|
335 |
|
|
*
|
336 |
|
|
* There are to hidden variables to produce debugging information:
|
337 |
|
|
* - 'i18n_string_debug', generic for all text groups.
|
338 |
|
|
* - 'i18n_string_debug_TEXTGROUP', enable debug only for TEXTGROUP.
|
339 |
|
|
*/
|
340 |
|
|
public function __construct($textgroup) {
|
341 |
|
|
$this->textgroup = $textgroup;
|
342 |
|
|
$this->debug = variable_get('i18n_string_debug', FALSE) || variable_get('i18n_string_debug_' . $textgroup, FALSE);
|
343 |
|
|
}
|
344 |
|
|
/**
|
345 |
|
|
* Build string object
|
346 |
|
|
*
|
347 |
|
|
* @param $context
|
348 |
|
|
* Context array or string
|
349 |
|
|
* @param $string string
|
350 |
|
|
* Current value for string source
|
351 |
|
|
*/
|
352 |
|
|
public function build_string($context, $string = NULL) {
|
353 |
|
|
// First try to locate string on cache
|
354 |
|
|
$context = is_array($context) ? implode(':', $context) : $context;
|
355 |
|
|
if ($cached = $this->cache_get($context)) {
|
356 |
|
|
$i18nstring = $cached;
|
357 |
|
|
}
|
358 |
|
|
else {
|
359 |
|
|
$i18nstring = new i18n_string_object();
|
360 |
|
|
$i18nstring->textgroup = $this->textgroup;
|
361 |
|
|
$i18nstring->set_context($context);
|
362 |
|
|
$this->cache_set($context, $i18nstring);
|
363 |
|
|
}
|
364 |
|
|
if (isset($string)) {
|
365 |
|
|
$i18nstring->set_string($string);
|
366 |
|
|
}
|
367 |
|
|
return $i18nstring;
|
368 |
|
|
}
|
369 |
|
|
/**
|
370 |
|
|
* Add source string to the locale tables for translation.
|
371 |
|
|
*
|
372 |
|
|
* It will also add data into i18n_string table for faster retrieval and indexing of groups of strings.
|
373 |
|
|
* Some string context doesn't have a numeric oid (I.e. content types), it will be set to zero.
|
374 |
|
|
*
|
375 |
|
|
* This function checks for already existing string without context for this textgroup and updates it accordingly.
|
376 |
|
|
* It is intended for backwards compatibility, using already created strings.
|
377 |
|
|
*
|
378 |
|
|
* @param $i18nstring
|
379 |
|
|
* String object
|
380 |
|
|
* @param $format
|
381 |
|
|
* Text format, for strings that will go through some filter
|
382 |
|
|
* @return
|
383 |
|
|
* Update status.
|
384 |
|
|
*/
|
385 |
|
|
protected function string_add($i18nstring, $options = array()) {
|
386 |
|
|
$options += array('watchdog' => TRUE);
|
387 |
|
|
// Default return status if nothing happens
|
388 |
|
|
$status = -1;
|
389 |
|
|
$source = NULL;
|
390 |
|
|
$location = $i18nstring->location;
|
391 |
|
|
// The string may not be allowed for translation depending on its format.
|
392 |
|
|
if (!$this->string_check($i18nstring, $options)) {
|
393 |
|
|
// The format may have changed and it's not allowed now, delete the source string
|
394 |
|
|
return $this->string_remove($i18nstring, $options);
|
395 |
|
|
}
|
396 |
|
|
elseif ($source = $i18nstring->get_source()) {
|
397 |
|
|
if ($source->source != $i18nstring->string || $source->location != $location) {
|
398 |
|
|
$i18nstring->location = $location;
|
399 |
|
|
// String has changed, mark translations for update
|
400 |
|
|
$status = $this->save_source($i18nstring);
|
401 |
|
|
db_update('locales_target')
|
402 |
|
|
->fields(array('i18n_status' => I18N_STRING_STATUS_UPDATE))
|
403 |
|
|
->condition('lid', $source->lid)
|
404 |
|
|
->execute();
|
405 |
|
|
}
|
406 |
|
|
elseif (empty($source->version)) {
|
407 |
|
|
// When refreshing strings, we've done version = 0, update it
|
408 |
|
|
$this->save_source($i18nstring);
|
409 |
|
|
}
|
410 |
|
|
}
|
411 |
|
|
else {
|
412 |
|
|
// We don't have the source object, create it
|
413 |
|
|
$status = $this->save_source($i18nstring);
|
414 |
|
|
}
|
415 |
|
|
// Make sure we have i18n_string part, create or update
|
416 |
|
|
// This will also create the source object if doesn't exist
|
417 |
|
|
$this->save_string($i18nstring);
|
418 |
|
|
|
419 |
|
|
if ($options['watchdog']) {
|
420 |
|
|
switch ($status) {
|
421 |
|
|
case SAVED_UPDATED:
|
422 |
|
|
watchdog('i18n_string', 'Updated string %location for textgroup %textgroup: %string', $i18nstring->get_args());
|
423 |
|
|
break;
|
424 |
|
|
case SAVED_NEW:
|
425 |
|
|
watchdog('i18n_string', 'Created string %location for text group %textgroup: %string', $i18nstring->get_args());
|
426 |
|
|
break;
|
427 |
|
|
}
|
428 |
|
|
}
|
429 |
|
|
return $status;
|
430 |
|
|
}
|
431 |
|
|
|
432 |
|
|
/**
|
433 |
|
|
* Check if string is ok for translation
|
434 |
|
|
*/
|
435 |
|
|
protected static function string_check($i18nstring, $options = array()) {
|
436 |
|
|
$options += array('messages' => FALSE, 'watchdog' => TRUE);
|
437 |
|
|
if (!empty($i18nstring->format) && !i18n_string_allowed_format($i18nstring->format)) {
|
438 |
|
|
// This format is not allowed, so we remove the string, in this case we produce a warning
|
439 |
|
|
drupal_set_message(t('The string %location for textgroup %textgroup is not allowed for translation because of its text format.', $i18nstring->get_args()), 'warning');
|
440 |
|
|
return FALSE;
|
441 |
|
|
}
|
442 |
|
|
else {
|
443 |
|
|
return TRUE;
|
444 |
|
|
}
|
445 |
|
|
}
|
446 |
|
|
|
447 |
|
|
/**
|
448 |
|
|
* Filter array of strings
|
449 |
|
|
*
|
450 |
6331c987
|
Assos Assos
|
* @param array $string_list
|
451 |
|
|
* Array of strings to be filtered.
|
452 |
|
|
* @param array $filter
|
453 |
85ad3d82
|
Assos Assos
|
* Array of name value conditions.
|
454 |
6331c987
|
Assos Assos
|
*
|
455 |
|
|
* @return array
|
456 |
|
|
* Strings from $string_list that match the filter conditions.
|
457 |
85ad3d82
|
Assos Assos
|
*/
|
458 |
|
|
protected static function string_filter($string_list, $filter) {
|
459 |
|
|
// Remove 'language' and '*' conditions.
|
460 |
|
|
if (isset($filter['language'])) {
|
461 |
|
|
unset($filter['language']);
|
462 |
|
|
}
|
463 |
|
|
while ($field = array_search('*', $filter)) {
|
464 |
|
|
unset($filter[$field]);
|
465 |
|
|
}
|
466 |
|
|
foreach ($string_list as $key => $string) {
|
467 |
|
|
foreach ($filter as $field => $value) {
|
468 |
|
|
if ($string->$field != $value) {
|
469 |
|
|
unset($string_list[$key]);
|
470 |
|
|
break;
|
471 |
|
|
}
|
472 |
|
|
}
|
473 |
|
|
}
|
474 |
|
|
return $string_list;
|
475 |
|
|
}
|
476 |
|
|
|
477 |
|
|
/**
|
478 |
|
|
* Build query for i18n_string table
|
479 |
|
|
*/
|
480 |
|
|
protected static function string_query($context, $multiple = FALSE) {
|
481 |
|
|
// Search the database using lid if we've got it or textgroup, context otherwise
|
482 |
|
|
$query = db_select('i18n_string', 's')->fields('s');
|
483 |
|
|
if (!empty($context->lid)) {
|
484 |
|
|
$query->condition('s.lid', $context->lid);
|
485 |
|
|
}
|
486 |
|
|
else {
|
487 |
|
|
$query->condition('s.textgroup', $context->textgroup);
|
488 |
|
|
if (!$multiple) {
|
489 |
|
|
$query->condition('s.context', $context->context);
|
490 |
|
|
}
|
491 |
|
|
else {
|
492 |
|
|
// Query multiple strings
|
493 |
|
|
foreach (array('type', 'objectid', 'property') as $field) {
|
494 |
|
|
if (!empty($context->$field)) {
|
495 |
|
|
$query->condition('s.' . $field, $context->$field);
|
496 |
|
|
}
|
497 |
|
|
}
|
498 |
|
|
}
|
499 |
|
|
}
|
500 |
|
|
return $query;
|
501 |
|
|
}
|
502 |
|
|
|
503 |
|
|
/**
|
504 |
|
|
* Remove string object.
|
505 |
|
|
*
|
506 |
|
|
* @return
|
507 |
|
|
* SAVED_DELETED | FALSE (If the operation failed because no source)
|
508 |
|
|
*/
|
509 |
|
|
public function string_remove($i18nstring, $options = array()) {
|
510 |
|
|
$options += array('watchdog' => TRUE, 'messages' => $this->debug);
|
511 |
|
|
if ($source = $i18nstring->get_source()) {
|
512 |
|
|
db_delete('locales_target')->condition('lid', $source->lid)->execute();
|
513 |
|
|
db_delete('i18n_string')->condition('lid', $source->lid)->execute();
|
514 |
|
|
db_delete('locales_source')->condition('lid', $source->lid)->execute();
|
515 |
|
|
$this->cache_set($source->context, NULL);
|
516 |
|
|
if ($options['watchdog']) {
|
517 |
|
|
watchdog('i18n_string', 'Deleted string %location for text group %textgroup: %string', $i18nstring->get_args());
|
518 |
|
|
}
|
519 |
|
|
if ($options['messages']) {
|
520 |
|
|
drupal_set_message(t('Deleted string %location for text group %textgroup: %string', $i18nstring->get_args()));
|
521 |
|
|
}
|
522 |
|
|
return SAVED_DELETED;
|
523 |
|
|
}
|
524 |
|
|
else {
|
525 |
|
|
if ($options['messages']) {
|
526 |
|
|
drupal_set_message(t('Cannot delete string, not found %location for text group %textgroup: %string', $i18nstring->get_args()));
|
527 |
|
|
}
|
528 |
|
|
return FALSE;
|
529 |
|
|
}
|
530 |
|
|
}
|
531 |
|
|
|
532 |
|
|
/**
|
533 |
|
|
* Translate string object
|
534 |
|
|
*
|
535 |
|
|
* @param $i18nstring
|
536 |
|
|
* String object
|
537 |
|
|
* @param $options
|
538 |
|
|
* Array with aditional options
|
539 |
|
|
*/
|
540 |
|
|
protected function string_translate($i18nstring, $options = array()) {
|
541 |
|
|
$langcode = isset($options['langcode']) ? $options['langcode'] : i18n_langcode();
|
542 |
|
|
// Search for existing translation (result will be cached in this function call)
|
543 |
|
|
$i18nstring->get_translation($langcode);
|
544 |
|
|
return $i18nstring;
|
545 |
|
|
}
|
546 |
|
|
|
547 |
|
|
/**
|
548 |
|
|
* Update / create / remove string.
|
549 |
|
|
*
|
550 |
|
|
* @param $name
|
551 |
|
|
* String context.
|
552 |
|
|
* @pram $string
|
553 |
|
|
* New value of string for update/create. May be empty for removing.
|
554 |
|
|
* @param $format
|
555 |
|
|
* Text format, that must have been checked against allowed formats for translation
|
556 |
|
|
* @param $options
|
557 |
|
|
* Processing options, the ones used here are:
|
558 |
|
|
* - 'watchdog', whether to produce watchdog messages.
|
559 |
|
|
* - 'messages', whether to produce user messages.
|
560 |
|
|
* - 'check', whether to check string format and then update/delete if not allowed.
|
561 |
|
|
* @return status
|
562 |
|
|
* SAVED_UPDATED | SAVED_NEW | SAVED_DELETED | FALSE (If the string is to be removed but has no source)
|
563 |
|
|
*/
|
564 |
|
|
public function string_update($i18nstring, $options = array()) {
|
565 |
|
|
$options += array('watchdog' => TRUE, 'messages' => $this->debug, 'check' => TRUE);
|
566 |
|
|
if ((!$options['check'] || $this->string_check($i18nstring, $options)) && $i18nstring->get_string()) {
|
567 |
|
|
// String is ok, has a value so we store it into the database.
|
568 |
|
|
$status = $this->string_add($i18nstring, $options);
|
569 |
|
|
}
|
570 |
|
|
elseif ($i18nstring->get_source()) {
|
571 |
|
|
// Just remove it if we already had a source created before.
|
572 |
|
|
$status = $this->string_remove($i18nstring, $options);
|
573 |
|
|
}
|
574 |
|
|
else {
|
575 |
|
|
// String didn't pass validation or we have an empty string but was not stored anyway.
|
576 |
|
|
$status = FALSE;
|
577 |
|
|
}
|
578 |
|
|
if ($options['messages']) {
|
579 |
|
|
switch ($status) {
|
580 |
|
|
case SAVED_UPDATED:
|
581 |
|
|
drupal_set_message(t('Updated string %location for text group %textgroup: %string', $i18nstring->get_args()));
|
582 |
|
|
break;
|
583 |
|
|
case SAVED_NEW:
|
584 |
|
|
drupal_set_message(t('Created string %location for text group %textgroup: %string', $i18nstring->get_args()));
|
585 |
|
|
break;
|
586 |
|
|
}
|
587 |
|
|
}
|
588 |
|
|
if ($options['watchdog']) {
|
589 |
|
|
switch ($status) {
|
590 |
|
|
case SAVED_UPDATED:
|
591 |
|
|
watchdog('i18n_string', 'Updated string %location for text group %textgroup: %string', $i18nstring->get_args());
|
592 |
|
|
break;
|
593 |
|
|
case SAVED_NEW:
|
594 |
|
|
watchdog('i18n_string', 'Created string %location for text group %textgroup: %string', $i18nstring->get_args());
|
595 |
|
|
break;
|
596 |
|
|
}
|
597 |
|
|
}
|
598 |
|
|
return $status;
|
599 |
|
|
}
|
600 |
|
|
|
601 |
|
|
/**
|
602 |
|
|
* Set string object into cache
|
603 |
|
|
*/
|
604 |
|
|
protected function cache_set($context, $string) {
|
605 |
|
|
$this->strings[$context] = $string;
|
606 |
|
|
}
|
607 |
|
|
|
608 |
|
|
/**
|
609 |
|
|
* Get translation from cache
|
610 |
|
|
*/
|
611 |
|
|
protected function cache_get($context) {
|
612 |
|
|
return isset($this->strings[$context]) ? $this->strings[$context] : NULL;
|
613 |
|
|
}
|
614 |
|
|
|
615 |
|
|
/**
|
616 |
|
|
* Reset cache, needed for tests
|
617 |
|
|
*/
|
618 |
|
|
public function cache_reset() {
|
619 |
|
|
$this->strings = array();
|
620 |
|
|
$this->string_format = array();
|
621 |
6331c987
|
Assos Assos
|
|
622 |
|
|
// Reset the persistent caches.
|
623 |
|
|
cache_clear_all('i18n:string:tgroup:' . $this->textgroup , 'cache', TRUE);
|
624 |
|
|
// Reset the complete string object cache too.
|
625 |
|
|
cache_clear_all('i18n:string:obj:', 'cache', TRUE);
|
626 |
85ad3d82
|
Assos Assos
|
}
|
627 |
|
|
|
628 |
|
|
/**
|
629 |
|
|
* Load multiple strings.
|
630 |
|
|
*
|
631 |
|
|
* @return array
|
632 |
|
|
* List of strings indexed by full string name.
|
633 |
|
|
*/
|
634 |
|
|
public function load_strings($conditions = array()) {
|
635 |
|
|
// Add textgroup condition and load all
|
636 |
|
|
$conditions['textgroup'] = $this->textgroup;
|
637 |
|
|
$list = array();
|
638 |
|
|
foreach (i18n_string_load_multiple($conditions) as $string) {
|
639 |
|
|
$list[$string->get_name()] = $string;
|
640 |
|
|
$this->cache_set($string->context, $string);
|
641 |
|
|
}
|
642 |
|
|
return $list;
|
643 |
|
|
}
|
644 |
|
|
|
645 |
|
|
/**
|
646 |
|
|
* Load string source from db
|
647 |
|
|
*/
|
648 |
|
|
public static function load_source($i18nstring) {
|
649 |
|
|
// Search the database using lid if we've got it or textgroup, context otherwise
|
650 |
|
|
$query = db_select('locales_source', 's')->fields('s');
|
651 |
|
|
$query->leftJoin('i18n_string', 'i', 's.lid = i.lid');
|
652 |
|
|
$query->fields('i', array('format', 'objectid', 'type', 'property', 'objectindex'));
|
653 |
|
|
if (!empty($i18nstring->lid)) {
|
654 |
|
|
$query->condition('s.lid', $i18nstring->lid);
|
655 |
|
|
}
|
656 |
|
|
else {
|
657 |
|
|
$query->condition('s.textgroup', $i18nstring->textgroup);
|
658 |
|
|
$query->condition('s.context', $i18nstring->context);
|
659 |
|
|
}
|
660 |
|
|
// Speed up the query, we just need one row
|
661 |
|
|
return $query->range(0, 1)->execute()->fetchObject();
|
662 |
|
|
}
|
663 |
|
|
|
664 |
|
|
/**
|
665 |
|
|
* Load translation from db
|
666 |
|
|
*
|
667 |
|
|
* @todo Optimize when we've already got the source string
|
668 |
|
|
*/
|
669 |
|
|
public static function load_translation($i18nstring, $langcode) {
|
670 |
|
|
// Search the database using lid if we've got it or textgroup, context otherwise
|
671 |
|
|
if (!empty($i18nstring->lid)) {
|
672 |
6331c987
|
Assos Assos
|
// We've already got lid, we just need translation data
|
673 |
85ad3d82
|
Assos Assos
|
$query = db_select('locales_target', 't');
|
674 |
|
|
$query->condition('t.lid', $i18nstring->lid);
|
675 |
|
|
}
|
676 |
|
|
else {
|
677 |
|
|
// Still don't have lid, load string properties too
|
678 |
|
|
$query = db_select('i18n_string', 's')->fields('s');
|
679 |
|
|
$query->leftJoin('locales_target', 't', 's.lid = t.lid');
|
680 |
|
|
$query->condition('s.textgroup', $i18nstring->textgroup);
|
681 |
|
|
$query->condition('s.context', $i18nstring->context);
|
682 |
|
|
}
|
683 |
|
|
// Add translation fields
|
684 |
|
|
$query->fields('t', array('translation', 'i18n_status'));
|
685 |
|
|
$query->condition('t.language', $langcode);
|
686 |
|
|
// Speed up the query, we just need one row
|
687 |
|
|
$query->range(0, 1);
|
688 |
|
|
return $query->execute()->fetchObject();
|
689 |
|
|
}
|
690 |
|
|
|
691 |
|
|
/**
|
692 |
|
|
* Save / update string object
|
693 |
|
|
*
|
694 |
|
|
* There seems to be a race condition sometimes so skip errors, #277711
|
695 |
|
|
*
|
696 |
|
|
* @param $string
|
697 |
|
|
* Full string object to be saved
|
698 |
|
|
* @param $source
|
699 |
|
|
* Source string object
|
700 |
|
|
*/
|
701 |
|
|
protected function save_string($string, $update = FALSE) {
|
702 |
|
|
if (!$string->get_source()) {
|
703 |
|
|
// Create source string so we get an lid
|
704 |
|
|
$this->save_source($string);
|
705 |
|
|
}
|
706 |
|
|
if (!isset($string->objectkey)) {
|
707 |
|
|
$string->objectkey = (int)$string->objectid;
|
708 |
|
|
}
|
709 |
|
|
if (!isset($string->format)) {
|
710 |
|
|
$string->format = '';
|
711 |
|
|
}
|
712 |
|
|
$status = db_merge('i18n_string')
|
713 |
|
|
->key(array('lid' => $string->lid))
|
714 |
|
|
->fields(array(
|
715 |
|
|
'textgroup' => $string->textgroup,
|
716 |
|
|
'context' => $string->context,
|
717 |
|
|
'objectid' => $string->objectid,
|
718 |
|
|
'type' => $string->type,
|
719 |
|
|
'property' => $string->property,
|
720 |
|
|
'objectindex' => $string->objectkey,
|
721 |
|
|
'format' => $string->format,
|
722 |
|
|
))
|
723 |
|
|
->execute();
|
724 |
|
|
return $status;
|
725 |
|
|
}
|
726 |
|
|
|
727 |
|
|
/**
|
728 |
|
|
* Save translation to the db
|
729 |
|
|
*
|
730 |
|
|
* @param $string
|
731 |
|
|
* Full string object with translation data (language, translation)
|
732 |
|
|
*/
|
733 |
|
|
protected function save_translation($string, $langcode) {
|
734 |
|
|
db_merge('locales_target')
|
735 |
|
|
->key(array('lid' => $string->lid, 'language' => $langcode))
|
736 |
|
|
->fields(array('translation' => $string->get_translation($langcode)))
|
737 |
|
|
->execute();
|
738 |
|
|
}
|
739 |
|
|
|
740 |
|
|
/**
|
741 |
|
|
* Save source string (create / update)
|
742 |
|
|
*/
|
743 |
|
|
protected static function save_source($source) {
|
744 |
|
|
if (isset($source->string)) {
|
745 |
|
|
$source->source = $source->string;
|
746 |
|
|
}
|
747 |
|
|
if (empty($source->version)) {
|
748 |
|
|
$source->version = 1;
|
749 |
|
|
}
|
750 |
|
|
return drupal_write_record('locales_source', $source, !empty($source->lid) ? 'lid' : array());
|
751 |
|
|
}
|
752 |
|
|
|
753 |
|
|
/**
|
754 |
|
|
* Remove source and translations for user defined string.
|
755 |
|
|
*
|
756 |
|
|
* Though for most strings the 'name' or 'string id' uniquely identifies that string,
|
757 |
|
|
* there are some exceptions (like profile categories) for which we need to use the
|
758 |
|
|
* source string itself as a search key.
|
759 |
|
|
*
|
760 |
|
|
* @param $context
|
761 |
|
|
* Textgroup and location glued with ':'.
|
762 |
|
|
* @param $string
|
763 |
|
|
* Optional source string (string in default language).
|
764 |
|
|
*/
|
765 |
|
|
public function context_remove($context, $string = NULL, $options = array()) {
|
766 |
|
|
$options += array('messages' => $this->debug);
|
767 |
|
|
$i18nstring = $this->build_string($context, $string);
|
768 |
|
|
$status = $this->string_remove($i18nstring, $options);
|
769 |
|
|
|
770 |
|
|
return $this;
|
771 |
|
|
}
|
772 |
|
|
|
773 |
|
|
/**
|
774 |
|
|
* Translate source string
|
775 |
|
|
*/
|
776 |
|
|
public function context_translate($context, $string, $options = array()) {
|
777 |
|
|
$i18nstring = $this->build_string($context, $string);
|
778 |
|
|
return $this->string_translate($i18nstring, $options);
|
779 |
|
|
}
|
780 |
|
|
|
781 |
|
|
/**
|
782 |
|
|
* Update / create translation source for user defined strings.
|
783 |
|
|
*
|
784 |
|
|
* @param $name
|
785 |
|
|
* Textgroup and location glued with ':'.
|
786 |
|
|
* @param $string
|
787 |
|
|
* Source string in default language. Default language may or may not be English.
|
788 |
|
|
* @param $options
|
789 |
|
|
* Array with additional options:
|
790 |
|
|
* - 'format', String format if the string has text format.
|
791 |
|
|
* - 'messages', Whether to print out status messages.
|
792 |
|
|
* - 'check', whether to check string format and then update/delete if not allowed.
|
793 |
|
|
*/
|
794 |
|
|
public function context_update($context, $string, $options = array()) {
|
795 |
|
|
$options += array('format' => FALSE, 'messages' => $this->debug, 'watchdog' => TRUE, 'check' => TRUE);
|
796 |
|
|
$i18nstring = $this->build_string($context, $string);
|
797 |
|
|
$i18nstring->format = $options['format'];
|
798 |
|
|
$this->string_update($i18nstring, $options);
|
799 |
|
|
return $this;
|
800 |
|
|
}
|
801 |
|
|
|
802 |
|
|
/**
|
803 |
|
|
* Build combinations of an array of arrays respecting keys.
|
804 |
|
|
*
|
805 |
|
|
* Example:
|
806 |
|
|
* array(array(a,b), array(1,2)) will translate into
|
807 |
|
|
* array(a,1), array(a,2), array(b,1), array(b,2)
|
808 |
|
|
*/
|
809 |
|
|
protected static function multiple_combine($properties) {
|
810 |
|
|
$combinations = array();
|
811 |
|
|
// Get first key, value. We need to make sure the array pointer is reset.
|
812 |
|
|
$value = reset($properties);
|
813 |
|
|
$key = key($properties);
|
814 |
|
|
array_shift($properties);
|
815 |
|
|
$values = is_array($value) ? $value : array($value);
|
816 |
|
|
foreach ($values as $value) {
|
817 |
|
|
if ($properties) {
|
818 |
|
|
foreach (self::multiple_combine($properties) as $merge) {
|
819 |
|
|
$combinations[] = array_merge(array($key => $value), $merge);
|
820 |
|
|
}
|
821 |
|
|
}
|
822 |
|
|
else {
|
823 |
|
|
$combinations[] = array($key => $value);
|
824 |
|
|
}
|
825 |
|
|
}
|
826 |
|
|
return $combinations;
|
827 |
|
|
}
|
828 |
|
|
|
829 |
|
|
/**
|
830 |
|
|
* Get multiple translations with search conditions.
|
831 |
|
|
*
|
832 |
|
|
* @param $translations
|
833 |
|
|
* Array of translation objects as loaded from the db.
|
834 |
|
|
* @param $langcode
|
835 |
|
|
* Language code, array of language codes or * to search all translations.
|
836 |
|
|
*
|
837 |
|
|
* @return array
|
838 |
|
|
* Array of i18n string objects.
|
839 |
|
|
*/
|
840 |
|
|
protected function multiple_translation_build($translations, $langcode) {
|
841 |
|
|
$strings = array();
|
842 |
|
|
foreach ($translations as $translation) {
|
843 |
|
|
// The string object may be already in list
|
844 |
|
|
if (isset($strings[$translation->context])) {
|
845 |
|
|
$string = $strings[$translation->context];
|
846 |
|
|
}
|
847 |
|
|
else {
|
848 |
|
|
$string = $this->build_string($translation->context);
|
849 |
|
|
$string->set_properties($translation);
|
850 |
|
|
$strings[$string->context] = $string;
|
851 |
|
|
}
|
852 |
|
|
// If this is a translation we set it there too
|
853 |
|
|
if ($translation->language && $translation->translation) {
|
854 |
|
|
$string->set_translation($translation);
|
855 |
|
|
}
|
856 |
|
|
elseif ($langcode) {
|
857 |
|
|
// This may only happen when we have a source string but not translation.
|
858 |
|
|
$string->set_translation(FALSE, $langcode);
|
859 |
|
|
}
|
860 |
|
|
}
|
861 |
|
|
return $strings;
|
862 |
|
|
}
|
863 |
|
|
|
864 |
|
|
/**
|
865 |
|
|
* Load multiple translations from db
|
866 |
|
|
*
|
867 |
|
|
* @todo Optimize when we've already got the source object
|
868 |
|
|
*
|
869 |
|
|
* @param $conditions
|
870 |
|
|
* Array of field values to use as query conditions.
|
871 |
|
|
* @param $langcode
|
872 |
|
|
* Language code to search.
|
873 |
|
|
* @param $index
|
874 |
|
|
* Field to use as index for the result.
|
875 |
|
|
* @return array
|
876 |
|
|
* Array of string objects with translation set.
|
877 |
|
|
*/
|
878 |
|
|
protected function multiple_translation_load($conditions, $langcode) {
|
879 |
|
|
$conditions += array(
|
880 |
|
|
'language' => $langcode,
|
881 |
|
|
'textgroup' => $this->textgroup
|
882 |
|
|
);
|
883 |
|
|
// We may be querying all translations at the same time or just one language.
|
884 |
|
|
// The language field needs some special treatment though.
|
885 |
|
|
$query = db_select('i18n_string', 's')->fields('s');
|
886 |
|
|
$query->leftJoin('locales_target', 't', 's.lid = t.lid');
|
887 |
|
|
$query->fields('t', array('translation', 'language', 'i18n_status'));
|
888 |
|
|
foreach ($conditions as $field => $value) {
|
889 |
|
|
// Single array value, reduce array
|
890 |
|
|
if (is_array($value) && count($value) == 1) {
|
891 |
|
|
$value = reset($value);
|
892 |
|
|
}
|
893 |
|
|
if ($value === '*') {
|
894 |
|
|
continue;
|
895 |
|
|
}
|
896 |
|
|
elseif ($field == 'language') {
|
897 |
|
|
$query->condition('t.language', $value);
|
898 |
|
|
}
|
899 |
|
|
else {
|
900 |
|
|
$query->condition('s.' . $field, $value);
|
901 |
|
|
}
|
902 |
|
|
}
|
903 |
|
|
return $this->multiple_translation_build($query->execute()->fetchAll(), $langcode);
|
904 |
|
|
}
|
905 |
|
|
|
906 |
|
|
/**
|
907 |
|
|
* Search multiple translations with key combinations.
|
908 |
|
|
*
|
909 |
|
|
* Each $context field may be a single value, an array of values or '*'.
|
910 |
|
|
* Example:
|
911 |
|
|
* array('term', array(1,2), '*')
|
912 |
|
|
* This will be mapped into the following conditions (provided language code is 'es')
|
913 |
|
|
* array('type' => 'term', 'objectid' => array(1,2), 'property' => '*', 'language' => 'es')
|
914 |
|
|
* And will result in these combinations to search for
|
915 |
|
|
* array('type' => 'term', 'objectid' => 1, 'property' => '*', 'language' => 'es')
|
916 |
|
|
* array('type' => 'term', 'objectid' => 2, 'property' => '*', 'language' => 'es')
|
917 |
|
|
*
|
918 |
|
|
* @param $context array
|
919 |
|
|
* Array with String context conditions.
|
920 |
|
|
*
|
921 |
|
|
* @return
|
922 |
|
|
* Array of translation objects indexed by context.
|
923 |
|
|
*/
|
924 |
|
|
public function multiple_translation_search($context, $langcode) {
|
925 |
|
|
// First, build conditions and identify the variable field.
|
926 |
|
|
$keys = array('type', 'objectid', 'property');
|
927 |
|
|
$conditions = array_combine($keys, $context) + array('language' => $langcode);
|
928 |
|
|
// Find existing searches in cache, compile remaining ones.
|
929 |
|
|
$translations = $search = array();
|
930 |
|
|
foreach ($this->multiple_combine($conditions) as $combination) {
|
931 |
|
|
$cached = $this->multiple_cache_get($combination);
|
932 |
|
|
if (isset($cached)) {
|
933 |
|
|
// Cache hit. Merge and remove value from search.
|
934 |
|
|
$translations += $cached;
|
935 |
|
|
}
|
936 |
|
|
else {
|
937 |
|
|
// Not in cache, add to search conditions skipping duplicated values.
|
938 |
|
|
// As array_merge_recursive() has some bug in PHP 5.2, http://drupal.org/node/1244598
|
939 |
|
|
// we use our simplified version here, instead of $search = array_merge_recursive($search, $combination);
|
940 |
|
|
foreach ($combination as $key => $value) {
|
941 |
|
|
if (!isset($search[$key]) || !in_array($value, $search[$key], TRUE)) {
|
942 |
|
|
$search[$key][] = $value;
|
943 |
|
|
}
|
944 |
|
|
}
|
945 |
|
|
}
|
946 |
|
|
}
|
947 |
|
|
// If we've got any search values left, find translations.
|
948 |
|
|
if ($search) {
|
949 |
|
|
// Load translations for conditions and set them to the cache
|
950 |
|
|
$loaded = $this->multiple_translation_load($search, $langcode);
|
951 |
|
|
if ($loaded) {
|
952 |
|
|
$translations += $loaded;
|
953 |
|
|
}
|
954 |
|
|
// Set cache for each of the multiple search keys.
|
955 |
|
|
foreach ($this->multiple_combine($search) as $combination) {
|
956 |
|
|
$list = $loaded ? $this->string_filter($loaded, $combination) : array();
|
957 |
|
|
$this->multiple_cache_set($combination, $list);
|
958 |
|
|
}
|
959 |
|
|
}
|
960 |
|
|
return $translations;
|
961 |
|
|
}
|
962 |
|
|
|
963 |
|
|
/**
|
964 |
|
|
* Set multiple cache.
|
965 |
|
|
*
|
966 |
|
|
* @param $context
|
967 |
|
|
* String context with language property at the end.
|
968 |
|
|
* @param $strings
|
969 |
|
|
* Array of strings (may be empty) to cache.
|
970 |
|
|
*/
|
971 |
|
|
protected function multiple_cache_set($context, $strings) {
|
972 |
|
|
$cache_key = implode(':', $context);
|
973 |
|
|
$this->cache_multiple[$cache_key] = $strings;
|
974 |
|
|
}
|
975 |
|
|
|
976 |
|
|
/**
|
977 |
|
|
* Get strings from multiple cache.
|
978 |
|
|
*
|
979 |
|
|
* @param $context array
|
980 |
|
|
* String context as array with language property at the end.
|
981 |
|
|
*
|
982 |
|
|
* @return mixed
|
983 |
|
|
* Array of strings (may be empty) if we've got a cache hit.
|
984 |
|
|
* Null otherwise.
|
985 |
|
|
*/
|
986 |
|
|
protected function multiple_cache_get($context) {
|
987 |
|
|
$cache_key = implode(':', $context);
|
988 |
|
|
if (isset($this->cache_multiple[$cache_key])) {
|
989 |
|
|
return $this->cache_multiple[$cache_key];
|
990 |
|
|
}
|
991 |
|
|
else {
|
992 |
|
|
// Now we try more generic keys. For instance, if we are searching 'term:1:*'
|
993 |
|
|
// we may try too 'term:*:*' and filter out the results.
|
994 |
|
|
foreach ($context as $key => $value) {
|
995 |
|
|
if ($value != '*') {
|
996 |
|
|
$try = array_merge($context, array($key => '*'));
|
997 |
|
|
$cache_key = implode(':', $try);
|
998 |
|
|
if (isset($this->cache_multiple[$cache_key])) {
|
999 |
|
|
// As we've found some more generic key, we need to filter using original conditions.
|
1000 |
|
|
$strings = $this->string_filter($this->cache_multiple[$cache_key], $context);
|
1001 |
|
|
return $strings;
|
1002 |
|
|
}
|
1003 |
|
|
}
|
1004 |
|
|
}
|
1005 |
|
|
// If we've reached here, we didn't find any cache match.
|
1006 |
|
|
return NULL;
|
1007 |
|
|
}
|
1008 |
|
|
}
|
1009 |
|
|
|
1010 |
|
|
/**
|
1011 |
|
|
* Translate array of source strings
|
1012 |
|
|
*
|
1013 |
|
|
* @param $context
|
1014 |
|
|
* Context array with placeholders (*)
|
1015 |
|
|
* @param $strings
|
1016 |
|
|
* Optional array of source strings indexed by the placeholder property
|
1017 |
|
|
*
|
1018 |
|
|
* @return array
|
1019 |
|
|
* Array of string objects (with translation) indexed by the placeholder field
|
1020 |
|
|
*/
|
1021 |
|
|
public function multiple_translate($context, $strings = array(), $options = array()) {
|
1022 |
|
|
// First, build conditions and identify the variable field
|
1023 |
|
|
$search = $context = array_combine(array('type', 'objectid', 'property'), $context);
|
1024 |
|
|
$langcode = isset($options['langcode']) ? $options['langcode'] : i18n_langcode();
|
1025 |
|
|
// If we've got keyed source strings set the array of keys on the placeholder field
|
1026 |
|
|
// or if not, remove that condition so we search all strings with that keys.
|
1027 |
|
|
foreach ($search as $field => $value) {
|
1028 |
|
|
if ($value === '*') {
|
1029 |
|
|
$property = $field;
|
1030 |
|
|
if ($strings) {
|
1031 |
|
|
$search[$field] = array_keys($strings);
|
1032 |
|
|
}
|
1033 |
|
|
}
|
1034 |
|
|
}
|
1035 |
|
|
// Now we'll add the language code to conditions and get the translations indexed by the property field
|
1036 |
|
|
$result = $this->multiple_translation_search($search, $langcode);
|
1037 |
|
|
// Remap translations using property field. If we've got strings it is important that they are in the same order.
|
1038 |
|
|
$translations = $strings;
|
1039 |
|
|
foreach ($result as $key => $i18nstring) {
|
1040 |
|
|
$translations[$i18nstring->$property] = $i18nstring;
|
1041 |
|
|
}
|
1042 |
|
|
// Set strings as source or create
|
1043 |
|
|
foreach ($strings as $key => $source) {
|
1044 |
|
|
if (isset($translations[$key]) && is_object($translations[$key])) {
|
1045 |
|
|
$translations[$key]->set_string($source);
|
1046 |
|
|
}
|
1047 |
|
|
else {
|
1048 |
|
|
// Not found any string for this property, create it to map in the response
|
1049 |
|
|
// But make sure we set this language's translation to FALSE so we don't search again
|
1050 |
|
|
$newcontext = $context;
|
1051 |
|
|
$newcontext[$property] = $key;
|
1052 |
|
|
$translations[$key] = $this->build_string($newcontext)
|
1053 |
|
|
->set_string($source)
|
1054 |
|
|
->set_translation(FALSE, $langcode);
|
1055 |
|
|
}
|
1056 |
|
|
}
|
1057 |
|
|
return $translations;
|
1058 |
|
|
}
|
1059 |
|
|
|
1060 |
|
|
/**
|
1061 |
|
|
* Update string translation, only if source exists.
|
1062 |
|
|
*
|
1063 |
|
|
* @param $context
|
1064 |
|
|
* String context as array
|
1065 |
|
|
* @param $langcode
|
1066 |
|
|
* Language code to create the translation for
|
1067 |
|
|
* @param $translation
|
1068 |
|
|
* String translation for this language
|
1069 |
|
|
*/
|
1070 |
|
|
function update_translation($context, $langcode, $translation) {
|
1071 |
|
|
$i18nstring = $this->build_string($context);
|
1072 |
|
|
if ($source = $i18nstring->get_source()) {
|
1073 |
|
|
$source->set_translation($translation, $langcode);
|
1074 |
|
|
$this->save_translation($source, $langcode);
|
1075 |
|
|
return $source;
|
1076 |
|
|
}
|
1077 |
|
|
}
|
1078 |
|
|
|
1079 |
|
|
/**
|
1080 |
|
|
* Recheck strings after update
|
1081 |
|
|
*/
|
1082 |
|
|
public function update_check() {
|
1083 |
|
|
// Find strings in locales_source that have no data in i18n_string
|
1084 |
|
|
$query = db_select('locales_source', 'l')
|
1085 |
|
|
->fields('l')
|
1086 |
|
|
->condition('l.textgroup', $this->textgroup);
|
1087 |
|
|
$alias = $query->leftJoin('i18n_string', 's', 'l.lid = s.lid');
|
1088 |
|
|
$query->isNull('s.lid');
|
1089 |
|
|
foreach ($query->execute()->fetchAll() as $string) {
|
1090 |
|
|
$i18nstring = $this->build_string($string->context, $string->source);
|
1091 |
|
|
$this->save_string($i18nstring);
|
1092 |
|
|
}
|
1093 |
|
|
}
|
1094 |
|
|
|
1095 |
|
|
}
|
1096 |
|
|
|
1097 |
|
|
/**
|
1098 |
|
|
* String object wrapper
|
1099 |
|
|
*/
|
1100 |
|
|
class i18n_string_object_wrapper extends i18n_object_wrapper {
|
1101 |
|
|
// Text group object
|
1102 |
|
|
protected $textgroup;
|
1103 |
|
|
// Properties for translation
|
1104 |
|
|
protected $properties;
|
1105 |
|
|
|
1106 |
|
|
/**
|
1107 |
|
|
* Get object strings for translation
|
1108 |
|
|
*
|
1109 |
|
|
* This will return a simple array of string objects, indexed by full string name.
|
1110 |
|
|
*
|
1111 |
|
|
* @param $options
|
1112 |
|
|
* Array with processing options.
|
1113 |
|
|
* - 'empty', whether to return empty strings, defaults to FALSE.
|
1114 |
|
|
*/
|
1115 |
|
|
public function get_strings($options = array()) {
|
1116 |
|
|
$options += array('empty' => FALSE);
|
1117 |
|
|
$strings = array();
|
1118 |
|
|
foreach ($this->get_properties() as $textgroup => $textgroup_list) {
|
1119 |
|
|
foreach ($textgroup_list as $type => $type_list) {
|
1120 |
|
|
foreach ($type_list as $object_id => $object_list) {
|
1121 |
|
|
foreach ($object_list as $key => $string) {
|
1122 |
|
|
if ($options['empty'] || !empty($string['string'])) {
|
1123 |
|
|
// Build string object, that will trigger static caches everywhere.
|
1124 |
|
|
$i18nstring = i18n_string_textgroup($textgroup)
|
1125 |
|
|
->build_string(array($type, $object_id, $key))
|
1126 |
|
|
->set_string($string);
|
1127 |
|
|
$strings[$i18nstring->get_name()] = $i18nstring;
|
1128 |
|
|
}
|
1129 |
|
|
}
|
1130 |
|
|
}
|
1131 |
|
|
}
|
1132 |
|
|
}
|
1133 |
|
|
return $strings;
|
1134 |
|
|
}
|
1135 |
|
|
/**
|
1136 |
|
|
* Get object translatable properties
|
1137 |
|
|
*
|
1138 |
|
|
* This will return a big array indexed by textgroup, object type, object id and string key.
|
1139 |
|
|
* Each element is an array with string information, and may have these properties:
|
1140 |
|
|
* - 'string', the string itself, will be NULL if the object doesn't have that string
|
1141 |
|
|
* - 'format', string format when needed
|
1142 |
|
|
* - 'title', string readable name
|
1143 |
|
|
*/
|
1144 |
|
|
public function get_properties() {
|
1145 |
|
|
if (!isset($this->properties)) {
|
1146 |
|
|
$this->properties = $this->build_properties();
|
1147 |
|
|
// Call hook_i18n_string_list_TEXTGROUP_alter(), last chance for modules
|
1148 |
|
|
drupal_alter('i18n_string_list_' . $this->get_textgroup(), $this->properties, $this->type, $this->object);
|
1149 |
|
|
|
1150 |
|
|
}
|
1151 |
|
|
return $this->properties;
|
1152 |
|
|
}
|
1153 |
|
|
|
1154 |
|
|
/**
|
1155 |
|
|
* Build properties from object.
|
1156 |
|
|
*/
|
1157 |
|
|
protected function build_properties() {
|
1158 |
|
|
list($string_type, $object_id) = $this->get_string_context();
|
1159 |
|
|
$object_keys = array(
|
1160 |
|
|
$this->get_textgroup(),
|
1161 |
|
|
$string_type,
|
1162 |
|
|
$object_id,
|
1163 |
|
|
);
|
1164 |
|
|
$strings = array();
|
1165 |
|
|
foreach ($this->get_string_info('properties', array()) as $field => $info) {
|
1166 |
|
|
$info = is_array($info) ? $info : array('title' => $info);
|
1167 |
|
|
$field_name = isset($info['field']) ? $info['field'] : $field;
|
1168 |
|
|
$value = $this->get_field($field_name);
|
1169 |
|
|
$strings[$this->get_textgroup()][$string_type][$object_id][$field] = array(
|
1170 |
|
|
'string' => is_array($value) || isset($info['empty']) && $value === $info['empty'] ? NULL : $value,
|
1171 |
|
|
'title' => $info['title'],
|
1172 |
|
|
'format' => isset($info['format']) ? $this->get_field($info['format']) : NULL,
|
1173 |
|
|
'name' => array_merge($object_keys, array($field)),
|
1174 |
|
|
);
|
1175 |
|
|
}
|
1176 |
|
|
return $strings;
|
1177 |
|
|
}
|
1178 |
|
|
|
1179 |
|
|
/**
|
1180 |
|
|
* Get string context
|
1181 |
|
|
*/
|
1182 |
|
|
public function get_string_context() {
|
1183 |
|
|
return array($this->get_string_info('type'), $this->get_key());
|
1184 |
|
|
}
|
1185 |
|
|
|
1186 |
|
|
/**
|
1187 |
|
|
* Get translate path for object
|
1188 |
|
|
*
|
1189 |
|
|
* @param $langcode
|
1190 |
|
|
* Language code if we want ti for a specific language
|
1191 |
|
|
*/
|
1192 |
|
|
public function get_translate_path($langcode = NULL) {
|
1193 |
|
|
$replacements = array('%i18n_language' => $langcode ? $langcode : '');
|
1194 |
|
|
if ($path = $this->get_string_info('translate path')) {
|
1195 |
|
|
return $this->path_replace($path, $replacements);
|
1196 |
|
|
}
|
1197 |
|
|
elseif ($path = $this->get_info('translate tab')) {
|
1198 |
|
|
// If we've got a translate tab path, we just add language to it
|
1199 |
|
|
return $this->path_replace($path . '/%i18n_language', $replacements);
|
1200 |
|
|
}
|
1201 |
|
|
}
|
1202 |
|
|
|
1203 |
|
|
/**
|
1204 |
|
|
* Translation mode for object
|
1205 |
|
|
*/
|
1206 |
|
|
public function get_translate_mode() {
|
1207 |
|
|
return !$this->get_langcode() ? I18N_MODE_LOCALIZE : I18N_MODE_NONE;
|
1208 |
|
|
}
|
1209 |
|
|
|
1210 |
|
|
/**
|
1211 |
|
|
* Get textgroup name
|
1212 |
|
|
*/
|
1213 |
|
|
public function get_textgroup() {
|
1214 |
|
|
return $this->get_string_info('textgroup');
|
1215 |
|
|
}
|
1216 |
|
|
|
1217 |
|
|
/**
|
1218 |
|
|
* Get textgroup object
|
1219 |
|
|
*/
|
1220 |
|
|
protected function textgroup() {
|
1221 |
|
|
if (!isset($this->textgroup)) {
|
1222 |
|
|
$this->textgroup = i18n_string_textgroup($this->get_textgroup());
|
1223 |
|
|
}
|
1224 |
|
|
return $this->textgroup;
|
1225 |
|
|
}
|
1226 |
|
|
|
1227 |
|
|
/**
|
1228 |
|
|
* Translate object.
|
1229 |
|
|
*
|
1230 |
|
|
* Translations are cached so it runs only once per language.
|
1231 |
|
|
*
|
1232 |
|
|
* @return object/array
|
1233 |
|
|
* A clone of the object with its properties translated.
|
1234 |
|
|
*/
|
1235 |
|
|
public function translate($langcode, $options = array()) {
|
1236 |
|
|
// We may have it already translated. As objects are statically cached, translations are too.
|
1237 |
e4c061ad
|
Assos Assos
|
if (!isset($this->translations[$langcode])) {
|
1238 |
85ad3d82
|
Assos Assos
|
$this->translations[$langcode] = $this->translate_object($langcode, $options);
|
1239 |
|
|
}
|
1240 |
|
|
return $this->translations[$langcode];
|
1241 |
|
|
}
|
1242 |
|
|
|
1243 |
|
|
/**
|
1244 |
|
|
* Translate access (localize strings)
|
1245 |
|
|
*/
|
1246 |
|
|
protected function localize_access() {
|
1247 |
|
|
// We could check also whether the object has strings to translate:
|
1248 |
|
|
// && $this->get_strings(array('empty' => TRUE))
|
1249 |
|
|
// However it may be better to display the 'No available strings' message
|
1250 |
|
|
// for the user to have a clue of what's going on. See i18n_string_translate_page_object()
|
1251 |
|
|
return user_access('translate interface') && user_access('translate user-defined strings');
|
1252 |
|
|
}
|
1253 |
|
|
|
1254 |
|
|
/**
|
1255 |
|
|
* Translate all properties for object.
|
1256 |
|
|
*
|
1257 |
|
|
* On top of object strings we search for all textgroup:type:objectid:* properties
|
1258 |
|
|
*
|
1259 |
|
|
* @param $langcode
|
1260 |
|
|
* A clone of the object or array
|
1261 |
|
|
*/
|
1262 |
|
|
protected function translate_object($langcode, $options) {
|
1263 |
|
|
// Clone object or array so we don't affect the original one.
|
1264 |
|
|
$object = is_object($this->object) ? clone $this->object : $this->object;
|
1265 |
|
|
// Get object strings for translatable properties.
|
1266 |
|
|
if ($strings = $this->get_strings()) {
|
1267 |
|
|
// We preload some of the property translations with a single query.
|
1268 |
|
|
if ($context = $this->get_translate_context($langcode, $options)) {
|
1269 |
|
|
$found = $this->textgroup()->multiple_translation_search($context, $langcode);
|
1270 |
|
|
}
|
1271 |
|
|
// Replace all strings in object.
|
1272 |
|
|
foreach ($strings as $i18nstring) {
|
1273 |
|
|
$this->translate_field($object, $i18nstring, $langcode, $options);
|
1274 |
|
|
}
|
1275 |
|
|
}
|
1276 |
|
|
return $object;
|
1277 |
|
|
}
|
1278 |
|
|
|
1279 |
|
|
/**
|
1280 |
|
|
* Context to be pre-loaded before translation.
|
1281 |
|
|
*/
|
1282 |
|
|
protected function get_translate_context($langcode, $options) {
|
1283 |
|
|
// One-query translation of all textgroup:type:objectid:* properties
|
1284 |
|
|
$context = $this->get_string_context();
|
1285 |
|
|
$context[] = '*';
|
1286 |
|
|
return $context;
|
1287 |
|
|
}
|
1288 |
|
|
|
1289 |
|
|
/**
|
1290 |
|
|
* Translate object property.
|
1291 |
|
|
*
|
1292 |
|
|
* Mot often, this is a direct field set, but sometimes fields may have different formats.
|
1293 |
|
|
*/
|
1294 |
|
|
protected function translate_field(&$object, $i18nstring, $langcode, $options) {
|
1295 |
|
|
$field_name = $i18nstring->property;
|
1296 |
|
|
$translation = $i18nstring->format_translation($langcode, $options);
|
1297 |
|
|
if (is_object($object)) {
|
1298 |
|
|
$object->$field_name = $translation;
|
1299 |
|
|
}
|
1300 |
|
|
elseif (is_array($object)) {
|
1301 |
|
|
$object[$field_name] = $translation;
|
1302 |
|
|
}
|
1303 |
|
|
}
|
1304 |
|
|
|
1305 |
|
|
/**
|
1306 |
|
|
* Remove all strings for this object.
|
1307 |
|
|
*/
|
1308 |
|
|
public function strings_remove($options = array()) {
|
1309 |
|
|
$result = array();
|
1310 |
|
|
foreach ($this->load_strings() as $key => $string) {
|
1311 |
|
|
$result[$key] = $string->remove($options);
|
1312 |
|
|
}
|
1313 |
|
|
return _i18n_string_result_count($result);
|
1314 |
|
|
}
|
1315 |
|
|
|
1316 |
|
|
/**
|
1317 |
|
|
* Update all strings for this object.
|
1318 |
|
|
*/
|
1319 |
|
|
public function strings_update($options = array()) {
|
1320 |
|
|
$options += array('empty' => TRUE, 'update' => TRUE);
|
1321 |
|
|
$result = array();
|
1322 |
|
|
$existing = $this->load_strings();
|
1323 |
|
|
// Update object strings
|
1324 |
|
|
foreach ($this->get_strings($options) as $key => $string) {
|
1325 |
|
|
$result[$key] = $string->update($options);
|
1326 |
|
|
unset($existing[$key]);
|
1327 |
|
|
}
|
1328 |
|
|
// Delete old existing strings.
|
1329 |
|
|
foreach ($existing as $key => $string) {
|
1330 |
|
|
$result[$key] = $string->remove($options);
|
1331 |
|
|
}
|
1332 |
|
|
return _i18n_string_result_count($result);
|
1333 |
|
|
}
|
1334 |
|
|
|
1335 |
|
|
/**
|
1336 |
|
|
* Load all existing strings for this object.
|
1337 |
|
|
*/
|
1338 |
|
|
public function load_strings() {
|
1339 |
|
|
list($type, $id) = $this->get_string_context();
|
1340 |
|
|
return $this->textgroup()->load_strings(array('type' => $type, 'objectid' => $id));
|
1341 |
|
|
}
|
1342 |
|
|
}
|
1343 |
6331c987
|
Assos Assos
|
|
1344 |
|
|
|
1345 |
|
|
/**
|
1346 |
|
|
* Textgroup handler for i18n_string API which integrated persistent caching.
|
1347 |
|
|
*/
|
1348 |
|
|
class i18n_string_textgroup_cached extends i18n_string_textgroup_default {
|
1349 |
|
|
|
1350 |
|
|
/**
|
1351 |
|
|
* Defines the timeout for the persistent caching.
|
1352 |
|
|
* @var int
|
1353 |
|
|
*/
|
1354 |
|
|
public $caching_time = CACHE_TEMPORARY;
|
1355 |
|
|
|
1356 |
|
|
/**
|
1357 |
|
|
* Extends the existing constructor with a cache handling.
|
1358 |
|
|
*
|
1359 |
|
|
* @param string $textgroup
|
1360 |
|
|
* The name of this textgroup.
|
1361 |
|
|
*/
|
1362 |
|
|
public function __construct($textgroup) {
|
1363 |
|
|
parent::__construct($textgroup);
|
1364 |
|
|
// Fetch persistent caches, the persistent caches contain only metadata.
|
1365 |
|
|
// Those metadata are processed by the related cache_get() methods.
|
1366 |
|
|
foreach (array('cache_multiple', 'strings') as $caches_type) {
|
1367 |
|
|
if (($cache = cache_get('i18n:string:tgroup:' . $this->textgroup . ':' . $caches_type)) && !empty($cache->data)) {
|
1368 |
|
|
$this->{$caches_type} = $cache->data;
|
1369 |
|
|
}
|
1370 |
|
|
}
|
1371 |
|
|
}
|
1372 |
|
|
|
1373 |
|
|
/**
|
1374 |
|
|
* Class destructor.
|
1375 |
|
|
*
|
1376 |
|
|
* Updates the persistent caches for the next usage.
|
1377 |
|
|
* This function not only stores the data of the textgroup objects but also
|
1378 |
|
|
* of the string objects. That way we ensure that only cacheable string object
|
1379 |
|
|
* go into the persistent cache.
|
1380 |
|
|
*/
|
1381 |
|
|
public function __destruct() {
|
1382 |
|
|
// Reduce size to cache by removing NULL values.
|
1383 |
|
|
$this->strings = array_filter($this->strings);
|
1384 |
|
|
|
1385 |
|
|
|
1386 |
|
|
$strings_to_cache = array();
|
1387 |
|
|
// Store the persistent caches. We just store the metadata the translations
|
1388 |
|
|
// are stored by the string object itself. However storing the metadata
|
1389 |
|
|
// reduces the number of DB queries executed during runtime.
|
1390 |
|
|
$cache_data = array();
|
1391 |
|
|
foreach ($this->strings as $context => $i18n_string_object) {
|
1392 |
|
|
$cache_data[$context] = $context;
|
1393 |
|
|
$strings_to_cache[$context] = $i18n_string_object;
|
1394 |
|
|
}
|
1395 |
|
|
cache_set('i18n:string:tgroup:' . $this->textgroup . ':strings', $cache_data, 'cache', $this->caching_time);
|
1396 |
|
|
|
1397 |
|
|
$cache_data = array();
|
1398 |
|
|
foreach ($this->cache_multiple as $pattern => $strings) {
|
1399 |
|
|
foreach ($strings as $context => $i18n_string_object) {
|
1400 |
|
|
$cache_data[$pattern][$context] = $context;
|
1401 |
|
|
$strings_to_cache[$context] = $i18n_string_object;
|
1402 |
|
|
}
|
1403 |
|
|
}
|
1404 |
|
|
cache_set('i18n:string:tgroup:' . $this->textgroup . ':cache_multiple', $cache_data, 'cache', $this->caching_time);
|
1405 |
|
|
|
1406 |
|
|
// Cache the string objects related to this textgroup.
|
1407 |
|
|
// Store only the public visible data into the persistent cache.
|
1408 |
|
|
foreach ($strings_to_cache as $i18n_string_object) {
|
1409 |
|
|
// If this isn't an object it's an unprocessed cache item and doesn't need
|
1410 |
|
|
// to be stored again.
|
1411 |
|
|
if (is_object($i18n_string_object)) {
|
1412 |
|
|
cache_set($i18n_string_object->get_cid(), get_object_vars($i18n_string_object), 'cache', $this->caching_time);
|
1413 |
|
|
}
|
1414 |
|
|
}
|
1415 |
|
|
}
|
1416 |
|
|
|
1417 |
|
|
/**
|
1418 |
|
|
* Reset cache, needed for tests.
|
1419 |
|
|
*
|
1420 |
|
|
* Takes care of the persistent caches.
|
1421 |
|
|
*/
|
1422 |
|
|
public function cache_reset() {
|
1423 |
|
|
// Reset the persistent caches.
|
1424 |
|
|
cache_clear_all('i18n:string:tgroup:' . $this->textgroup , 'cache', TRUE);
|
1425 |
|
|
// Reset the complete string object cache too. This will affect string
|
1426 |
|
|
// objects of other textgroups as well.
|
1427 |
|
|
cache_clear_all('i18n:string:obj:', 'cache', TRUE);
|
1428 |
|
|
|
1429 |
|
|
return parent::cache_reset();
|
1430 |
|
|
}
|
1431 |
|
|
|
1432 |
|
|
/**
|
1433 |
|
|
* Get translation from cache.
|
1434 |
|
|
*
|
1435 |
|
|
* Extends the original handler with persistent caching.
|
1436 |
|
|
*
|
1437 |
|
|
* @param string $context
|
1438 |
|
|
* The context to look out for.
|
1439 |
|
|
* @return i18n_string_object|NULL
|
1440 |
|
|
* The string object if available or NULL otherwise.
|
1441 |
|
|
*/
|
1442 |
|
|
protected function cache_get($context) {
|
1443 |
|
|
if (isset($this->strings[$context])) {
|
1444 |
|
|
// If the cache contains a string re-build i18n_string_object.
|
1445 |
|
|
if (is_string($this->strings[$context])) {
|
1446 |
|
|
$i8n_string_object = new i18n_string_object(array('textgroup' => $this->textgroup));
|
1447 |
|
|
$i8n_string_object->set_context($context);
|
1448 |
|
|
$this->strings[$context] = $i8n_string_object;
|
1449 |
|
|
}
|
1450 |
|
|
// Now run the original handling.
|
1451 |
|
|
return parent::cache_get($context);
|
1452 |
|
|
}
|
1453 |
|
|
return NULL;
|
1454 |
|
|
}
|
1455 |
|
|
|
1456 |
|
|
/**
|
1457 |
|
|
* Get strings from multiple cache.
|
1458 |
|
|
*
|
1459 |
|
|
* @param $context array
|
1460 |
|
|
* String context as array with language property at the end.
|
1461 |
|
|
*
|
1462 |
|
|
* @return mixed
|
1463 |
|
|
* Array of strings (may be empty) if we've got a cache hit.
|
1464 |
|
|
* Null otherwise.
|
1465 |
|
|
*/
|
1466 |
|
|
protected function multiple_cache_get($context) {
|
1467 |
|
|
// Ensure the values from the persistent cache are properly re-build.
|
1468 |
|
|
$cache_key = implode(':', $context);
|
1469 |
|
|
if (isset($this->cache_multiple[$cache_key])) {
|
1470 |
|
|
foreach ($this->cache_multiple[$cache_key] as $cached_context) {
|
1471 |
|
|
if (is_string($cached_context)) {
|
1472 |
|
|
$i8n_string_object = new i18n_string_object(array('textgroup' => $this->textgroup));
|
1473 |
|
|
$i8n_string_object->set_context($cached_context);
|
1474 |
|
|
$this->cache_multiple[$cache_key][$cached_context] = $i8n_string_object;
|
1475 |
|
|
}
|
1476 |
|
|
}
|
1477 |
|
|
}
|
1478 |
|
|
else {
|
1479 |
|
|
// Now we try more generic keys. For instance, if we are searching 'term:1:*'
|
1480 |
|
|
// we may try too 'term:*:*' and filter out the results.
|
1481 |
|
|
foreach ($context as $key => $value) {
|
1482 |
|
|
if ($value != '*') {
|
1483 |
|
|
$try = array_merge($context, array($key => '*'));
|
1484 |
|
|
return $this->multiple_cache_get($try);
|
1485 |
|
|
}
|
1486 |
|
|
}
|
1487 |
|
|
}
|
1488 |
|
|
return parent::multiple_cache_get($context);
|
1489 |
|
|
}
|
1490 |
|
|
|
1491 |
|
|
public function string_update($i18nstring, $options = array()) {
|
1492 |
|
|
// Flush persistent cache.
|
1493 |
|
|
cache_clear_all($i18nstring->get_cid(), 'cache', TRUE);
|
1494 |
|
|
return parent::string_update($i18nstring, $options);
|
1495 |
|
|
}
|
1496 |
|
|
|
1497 |
|
|
public function string_remove($i18nstring, $options = array()) {
|
1498 |
|
|
// Flush persistent cache.
|
1499 |
|
|
cache_clear_all($i18nstring->get_cid(), 'cache', TRUE);
|
1500 |
|
|
return parent::string_remove($i18nstring, $options);
|
1501 |
|
|
}
|
1502 |
|
|
} |