-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathaction.php
287 lines (253 loc) · 9.41 KB
/
action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
use dokuwiki\Extension\ActionPlugin;
use dokuwiki\Extension\EventHandler;
use dokuwiki\Extension\Event;
/**
* Translation Plugin: Simple multilanguage plugin
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreas Gohr <[email protected]>
* @author Guy Brand <[email protected]>
*/
class action_plugin_translation extends ActionPlugin
{
/**
* For the helper plugin
* @var helper_plugin_translation
*/
protected $helper;
/**
* Constructor. Load helper plugin
*/
public function __construct()
{
$this->helper = plugin_load('helper', 'translation');
}
/**
* Registers a callback function for a given event
*
* @param EventHandler $controller
*/
public function register(EventHandler $controller)
{
if ($this->getConf('translateui')) {
$controller->register_hook('INIT_LANG_LOAD', 'BEFORE', $this, 'translateUI');
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'translateJS');
$controller->register_hook('JS_CACHE_USE', 'BEFORE', $this, 'translateJSCache');
} else {
$controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, 'addLanguageAttributes');
}
if ($this->getConf('redirectstart')) {
$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'redirectStartPage');
}
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'addHrefLangAttributes');
$controller->register_hook('COMMON_PAGETPL_LOAD', 'AFTER', $this, 'handlePageTemplates');
$controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'AFTER', $this, 'sortSearchResults');
}
/**
* Hook Callback. Set the language for the UI
*
* @param Event $event INIT_LANG_LOAD
*/
public function translateUI(Event $event)
{
global $conf;
global $ACT;
global $INPUT;
// $ID is not set yet, so we have to get it ourselves
$id = getID();
// For ID based access, get the language from the ID, try request param or session otherwise
if (
isset($ACT) &&
in_array(act_clean($ACT), ['show', 'recent', 'diff', 'edit', 'preview', 'source', 'subscribe'])
) {
$locale = $this->helper->getLangPart($id ?? '');
$_SESSION[DOKU_COOKIE]['translationlc'] = $locale; // IDs always reset the language
} elseif ($INPUT->has('lang')) {
$locale = $INPUT->str('lang');
} else {
$locale = $_SESSION[DOKU_COOKIE]['translationlc'] ?? '';
}
// ensure a valid locale was given by using it as fake ID
$locale = $this->helper->getLangPart("$locale:foo");
// if the language is not the default language, set the language
if ($locale && $locale !== $conf['lang']) {
$conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin
$event->data = $locale;
$conf['lang'] = $locale;
}
}
/**
* Hook Callback. Pass language code to JavaScript dispatcher
*
* @param Event $event TPL_METAHEADER_OUTPUT
*/
public function translateJS(Event $event)
{
global $conf;
$count = count($event->data['script']);
for ($i = 0; $i < $count; $i++) {
if (
array_key_exists('src', $event->data['script'][$i]) &&
strpos($event->data['script'][$i]['src'], '/lib/exe/js.php') !== false
) {
$event->data['script'][$i]['src'] .= '&lang=' . hsc($conf['lang']);
}
}
}
/**
* Hook Callback. Cache JavaScript per language
*
* @param Event $event JS_CACHE_USE
*/
public function translateJSCache(Event $event)
{
global $conf;
// reuse the constructor to reinitialize the cache key
$event->data->__construct(
$event->data->key . $conf['lang'],
$event->data->ext
);
}
/**
* Hook Callback. Add lang and dir attributes when UI isn't translated
*
* @param Event $event TPL_CONTENT_DISPLAY
*/
public function addLanguageAttributes(Event $event)
{
global $ID;
global $conf;
if (!$this->helper->istranslatable($ID)) return;
$locale = $this->helper->getLangPart($ID ?? '');
if ($locale && $locale !== $conf['lang']) {
if (file_exists(DOKU_INC . 'inc/lang/' . $locale . '/lang.php')) {
$lang = [];
include(DOKU_INC . 'inc/lang/' . $locale . '/lang.php');
$direction = $lang['direction'] ?? 'ltr';
$event->data = '<div lang="' . hsc($locale) . '" dir="' . hsc($direction) . '">' .
$event->data .
'</div>';
}
}
}
/**
* Hook Callback. Redirect to translated start page
*
* @param Event $event DOKUWIKI_STARTED
*/
public function redirectStartPage(Event $event)
{
global $ID;
global $ACT;
global $conf;
if ($ID == $conf['start'] && $ACT == 'show') {
$lc = $this->helper->getBrowserLang();
[$translatedStartpage, ] = $this->helper->buildTransID($lc, $conf['start']);
if (cleanID($translatedStartpage) !== cleanID($ID)) {
send_redirect(wl(cleanID($translatedStartpage), '', true));
}
}
}
/**
* Hook Callback. Add hreflang attributes to the page header
*
* @param Event $event TPL_METAHEADER_OUTPUT
*/
public function addHrefLangAttributes(Event $event)
{
global $ID;
global $conf;
if (!$this->helper->istranslatable($ID)) return;
$translations = $this->helper->getAvailableTranslations($ID);
if ($translations) {
foreach ($translations as $lc => $translation) {
$event->data['link'][] = [
'rel' => 'alternate',
'hreflang' => $lc,
'href' => wl(cleanID($translation), '', true),
];
}
}
$default = $conf['lang_before_translation'] ?? $conf['lang'];
$defaultlink = $this->helper->buildTransID($default, ($this->helper->getTransParts($ID))[1])[0];
$event->data['link'][] = [
'rel' => 'alternate',
'hreflang' => 'x-default',
'href' => wl(cleanID($defaultlink), '', true),
];
}
/**
* Hook Callback. Make current language available as page template placeholder and handle
* original language copying
*
* @param Event $event COMMON_PAGETPL_LOAD
*/
public function handlePageTemplates(Event $event)
{
global $ID;
// load orginal content as template?
if ($this->getConf('copytrans') && $this->helper->istranslatable($ID, false)) {
// look for existing translations
$translations = $this->helper->getAvailableTranslations($ID);
if ($translations) {
// find original language (might've been provided via parameter or use first translation)
$orig = (string)$_REQUEST['fromlang'];
if (!$orig) $orig = array_key_first($translations);
// load file
$origfile = $translations[$orig];
$event->data['tpl'] = io_readFile(wikiFN($origfile));
// prefix with warning
$warn = io_readFile($this->localFN('totranslate'));
if ($warn) $warn .= "\n\n";
$event->data['tpl'] = $warn . $event->data['tpl'];
// show user a choice of translations if any
if (count($translations) > 1) {
$links = [];
foreach (array_keys($translations) as $t) {
$links[] = '<a href="' . wl($ID, ['do' => 'edit', 'fromlang' => $t]) . '">' .
$this->helper->getLocalName($t) .
'</a>';
}
msg(
sprintf(
$this->getLang('transloaded'),
$this->helper->getLocalName($orig),
implode(', ', $links)
)
);
}
}
}
// apply placeholders
$event->data['tpl'] = str_replace('@LANG@', $this->helper->realLC(''), $event->data['tpl']);
$event->data['tpl'] = str_replace('@TRANS@', $this->helper->getLangPart($ID), $event->data['tpl']);
}
/**
* Hook Callback. Resort page match results so that results are ordered by translation, having the
* default language first
*
* @param Event $event SEARCH_QUERY_PAGELOOKUP
*/
public function sortSearchResults(Event $event)
{
// sort into translation slots
$res = [];
foreach ($event->result as $r => $t) {
$tr = $this->helper->getLangPart($r);
if (!isset($res["x$tr"]) || !is_array($res["x$tr"])) {
$res["x$tr"] = [];
}
$res["x$tr"][] = [$r, $t];
}
// sort by translations
ksort($res);
// combine
$event->result = [];
foreach ($res as $r) {
foreach ($r as $l) {
$event->result[$l[0]] = $l[1];
}
}
}
}