|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Loads the requested language content |
|
5
|
|
|
* |
|
6
|
|
|
* PHP Version 5 |
|
7
|
|
|
* |
|
8
|
|
|
* @category Core |
|
9
|
|
|
* @package Translation |
|
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
11
|
|
|
* @copyright 2013 cSphere Team |
|
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
13
|
|
|
* @link http://www.csphere.eu |
|
14
|
|
|
**/ |
|
15
|
|
|
|
|
16
|
|
|
namespace csphere\core\translation; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Loads the requested language content |
|
20
|
|
|
* |
|
21
|
|
|
* PHP Version 5 |
|
22
|
|
|
* |
|
23
|
|
|
* @category Core |
|
24
|
|
|
* @package Translation |
|
25
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
26
|
|
|
* @copyright 2013 cSphere Team |
|
27
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
28
|
|
|
* @link http://www.csphere.eu |
|
29
|
|
|
**/ |
|
30
|
|
|
|
|
31
|
|
|
abstract class Fetch |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Language shorthandle for translation |
|
35
|
|
|
**/ |
|
36
|
|
|
private static $_language = ''; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Service loader object |
|
40
|
|
|
**/ |
|
41
|
|
|
private static $_loader = null; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* XML driver object |
|
45
|
|
|
**/ |
|
46
|
|
|
private static $_xml = null; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Cache driver object |
|
50
|
|
|
**/ |
|
51
|
|
|
private static $_cache = null; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* View driver theme name |
|
55
|
|
|
**/ |
|
56
|
|
|
private static $_theme = ''; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Store language files that are already opened |
|
60
|
|
|
**/ |
|
61
|
|
|
private static $_loaded = []; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Fetches the whole translation table |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $plugin Plugin to use for language file |
|
67
|
|
|
* |
|
68
|
|
|
* @throws \Exception |
|
69
|
|
|
* |
|
70
|
|
|
* @return array |
|
71
|
|
|
**/ |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
public static function keys($plugin) |
|
74
|
|
|
{ |
|
75
|
|
|
$exists = self::exists($plugin); |
|
76
|
|
|
|
|
77
|
|
|
// Return key if it exists |
|
78
|
|
|
if ($exists == true) { |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
$return = self::$_loaded[$plugin]; |
|
81
|
|
|
|
|
82
|
|
|
} else { |
|
83
|
|
|
|
|
84
|
|
|
// Empty plugin means that a theme translation is going on |
|
85
|
|
|
$error = ($plugin == '') ? 'Theme' : 'Plugin "' . $plugin . '"'; |
|
86
|
|
|
$error .= ' does not contain the requested translation'; |
|
87
|
|
|
|
|
88
|
|
|
throw new \Exception($error); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Delivers the requested translation string |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $plugin Plugin to use for language file |
|
98
|
|
|
* @param string $key Key to search in that file |
|
99
|
|
|
* |
|
100
|
|
|
* @throws \Exception |
|
101
|
|
|
* |
|
102
|
|
|
* @return string |
|
103
|
|
|
**/ |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
public static function key($plugin, $key) |
|
106
|
|
|
{ |
|
107
|
|
|
$exists = self::exists($plugin, $key); |
|
108
|
|
|
|
|
109
|
|
|
// Return key if it exists |
|
110
|
|
|
if ($exists == true) { |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
$return = self::$_loaded[$plugin][$key]; |
|
113
|
|
|
|
|
114
|
|
|
} else { |
|
115
|
|
|
|
|
116
|
|
|
// Empty plugin means that a theme translation is going on |
|
117
|
|
|
$error = ($plugin == '') ? 'Theme' : 'Plugin "' . $plugin . '"'; |
|
118
|
|
|
$error .= ' fails to translate this key: ' . $key; |
|
119
|
|
|
|
|
120
|
|
|
throw new \Exception($error); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $return; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Search for a key in a given plugin and the default plugin |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $plugin Plugin to use for language file |
|
130
|
|
|
* @param string $key Key to search in that file |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
**/ |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
public static function fallback($plugin, $key) |
|
136
|
|
|
{ |
|
137
|
|
|
$target = ''; |
|
138
|
|
|
$exists = self::exists($plugin, $key); |
|
139
|
|
|
|
|
140
|
|
|
// Search key in default plugin otherwise |
|
141
|
|
|
if ($exists === true) { |
|
142
|
|
|
|
|
143
|
|
|
$target = $plugin; |
|
144
|
|
|
|
|
145
|
|
|
} else { |
|
146
|
|
|
|
|
147
|
|
|
$exists = self::exists('default', $key); |
|
148
|
|
|
|
|
149
|
|
|
if ($exists === true) { |
|
150
|
|
|
|
|
151
|
|
|
$target = 'default'; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return $target; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Checks if a plugin is translated or contains a key |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $plugin Plugin to use for language file |
|
162
|
|
|
* @param string $key Key to search in that file |
|
163
|
|
|
* |
|
164
|
|
|
* @return boolean |
|
165
|
|
|
**/ |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
public static function exists($plugin, $key = '') |
|
168
|
|
|
{ |
|
169
|
|
|
// Check if translation is already loaded |
|
170
|
|
|
if (!isset(self::$_loaded[$plugin])) { |
|
171
|
|
|
|
|
172
|
|
|
self::$_loaded[$plugin] = self::_cache($plugin); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if ($key == '') { |
|
176
|
|
|
|
|
177
|
|
|
$result = isset(self::$_loaded[$plugin]) ? true : false; |
|
178
|
|
|
|
|
179
|
|
|
} else { |
|
180
|
|
|
|
|
181
|
|
|
$result = isset(self::$_loaded[$plugin][$key]) ? true : false; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $result; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Short name of active language |
|
189
|
|
|
* |
|
190
|
|
|
* @return string |
|
191
|
|
|
**/ |
|
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
public static function lang() |
|
194
|
|
|
{ |
|
195
|
|
|
// Load language short if not done yet |
|
196
|
|
|
if (self::$_language == '') { |
|
197
|
|
|
|
|
198
|
|
|
// Get user language from session |
|
199
|
|
|
$session = new \csphere\core\session\Session(); |
|
200
|
|
|
$language = $session->get('user_lang'); |
|
201
|
|
|
|
|
202
|
|
|
self::$_language = empty($language) ? 'en' : $language; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return self::$_language; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Sets options to work with translation files |
|
210
|
|
|
* |
|
211
|
|
|
* @return void |
|
212
|
|
|
**/ |
|
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
private static function _settings() |
|
215
|
|
|
{ |
|
216
|
|
|
// Set language if not done yet |
|
217
|
|
|
self::lang(); |
|
218
|
|
|
|
|
219
|
|
|
// Get basic objects |
|
220
|
|
|
self::$_loader = \csphere\core\service\Locator::get(); |
|
221
|
|
|
|
|
222
|
|
|
self::$_cache = self::$_loader->load('cache'); |
|
223
|
|
|
|
|
224
|
|
|
$view = self::$_loader->load('view'); |
|
225
|
|
|
|
|
226
|
|
|
self::$_theme = $view->getOption('theme'); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Delivers the requested translation file |
|
231
|
|
|
* |
|
232
|
|
|
* @param string $plugin Plugin to use for language file |
|
233
|
|
|
* |
|
234
|
|
|
* @return array |
|
235
|
|
|
**/ |
|
|
|
|
|
|
236
|
|
|
|
|
237
|
|
|
private static function _cache($plugin) |
|
238
|
|
|
{ |
|
239
|
|
|
// Load cache and settings if not done yet |
|
240
|
|
|
if (self::$_cache == null) { |
|
241
|
|
|
|
|
242
|
|
|
self::_settings(); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
// Empty plugin means that a theme translation is going on |
|
246
|
|
|
if ($plugin == '') { |
|
247
|
|
|
|
|
248
|
|
|
$token = 'lang_theme_' . self::$_theme . '_' . self::$_language; |
|
249
|
|
|
|
|
250
|
|
|
} else { |
|
251
|
|
|
|
|
252
|
|
|
$token = 'lang_plugin_' . $plugin . '_' . self::$_language; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
// Look for plugin and language inside cache |
|
256
|
|
|
$lang = self::$_cache->load($token); |
|
257
|
|
|
|
|
258
|
|
|
// If cache loading fails load it and create cache file |
|
259
|
|
|
if ($lang == false) { |
|
260
|
|
|
|
|
261
|
|
|
$lang = self::_open($plugin); |
|
262
|
|
|
|
|
263
|
|
|
self::$_cache->save($token, $lang); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
return $lang; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Gets the content for the requested translation file |
|
271
|
|
|
* |
|
272
|
|
|
* @param string $plugin Plugin to use for language file |
|
273
|
|
|
* |
|
274
|
|
|
* @return array |
|
275
|
|
|
**/ |
|
|
|
|
|
|
276
|
|
|
|
|
277
|
|
|
private static function _open($plugin) |
|
278
|
|
|
{ |
|
279
|
|
|
// Set XML loader if not done yet |
|
280
|
|
|
if (self::$_xml == null) { |
|
281
|
|
|
|
|
282
|
|
|
self::$_xml = self::$_loader->load('xml', 'language'); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
// Empty plugin means that a theme translation is going on |
|
286
|
|
|
if ($plugin == '') { |
|
287
|
|
|
|
|
288
|
|
|
$data = self::$_xml->source('theme', self::$_theme, self::$_language); |
|
289
|
|
|
|
|
290
|
|
|
} else { |
|
291
|
|
|
|
|
292
|
|
|
$data = self::$_xml->source('plugin', $plugin, self::$_language); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
// Array should be a list of names with their value |
|
296
|
|
|
$lang = []; |
|
297
|
|
|
$data = $data['definitions']; |
|
298
|
|
|
|
|
299
|
|
|
//@ToDo: Find out why the XML Parser delete empty values |
|
300
|
|
|
foreach ($data AS $def) { |
|
301
|
|
|
|
|
302
|
|
|
if (empty($def['value'])) { |
|
303
|
|
|
$lang[$def['name']]=""; |
|
304
|
|
|
} else { |
|
305
|
|
|
$lang[$def['name']] = $def['value']; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
|
|
311
|
|
|
return $lang; |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|