forked from splitbrain/dokuwiki-plugin-translation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.php
191 lines (164 loc) · 6.11 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
<?php
/**
* 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]>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');
class action_plugin_translation extends DokuWiki_Action_Plugin {
/**
* for th helper plugin
*/
var $hlp = null;
var $locale;
/**
* Constructor. Load helper plugin
*/
function action_plugin_translation(){
$this->hlp =& plugin_load('helper', 'translation');
}
/**
* Registe the events
*/
function register(&$controller) {
// should the lang be applied to UI?
if($this->getConf('translateui')){
$scriptName = basename($_SERVER['PHP_SELF']);
switch ($scriptName) {
case 'js.php':
$controller->register_hook('INIT_LANG_LOAD', 'BEFORE', $this, 'translation_js');
$controller->register_hook('JS_CACHE_USE', 'BEFORE', $this, 'translation_jscache');
break;
case 'ajax.php':
$controller->register_hook('INIT_LANG_LOAD', 'BEFORE', $this, 'translate_media_manager');
break;
case 'mediamanager.php':
$controller->register_hook('MEDIAMANAGER_STARTED', 'BEFORE', $this, 'translation_hook');
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'setJsCacheKey');
break;
default:
$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'translation_hook');
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'setJsCacheKey');
}
}
$controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'AFTER', $this, 'translation_search');
}
function setJsCacheKey(&$event, $args) {
if (!isset($this->locale)) return false;
$count = count($event->data['script']);
for ($i = 0; $i<$count; $i++) {
if (strpos($event->data['script'][$i]['src'], '/lib/exe/js.php') !== false) {
$event->data['script'][$i]['src'] .= '&lang='.hsc($this->locale);
}
}
return false;
}
function translation_js(&$event, $args) {
global $conf;
if(!isset($_GET['lang'])) return;
if(!in_array($_GET['lang'],$this->hlp->trans)) return;
$lang = $_GET['lang'];
$event->data = $lang;
$conf['lang'] = $lang;
}
function translation_jscache(&$event, $args) {
if (!isset($_GET['lang'])) return;
if(!in_array($_GET['lang'],$this->hlp->trans)) return;
$lang = $_GET['lang'];
// reuse the constructor to reinitialize the cache key
$event->data->cache(
$event->data->key . $lang,
$event->data->ext
);
}
function translate_media_manager(&$event, $args) {
global $conf;
if (isset($_REQUEST['ID'])) {
$id = getID();
$lc = $this->hlp->getLangPart($id);
} elseif (isset($_SESSION[DOKU_COOKIE]['translationlc'])) {
$lc = $_SESSION[DOKU_COOKIE]['translationlc'];
} else {
return;
}
$conf['lang'] = $lc;
$event->data = $lc;
}
/**
* Change the UI language in foreign language namespaces
*/
function translation_hook(&$event, $args) {
global $ID;
global $lang;
global $conf;
global $ACT;
// redirect away from start page?
if($this->conf['redirectstart'] && $ID == $conf['start'] && $ACT == 'show'){
$lc = $this->hlp->getBrowserLang();
if(!$lc) $lc = $conf['lang'];
header('Location: '.wl($lc.':'.$conf['start'],'',true,'&'));
exit;
}
// check if we are in a foreign language namespace
$lc = $this->hlp->getLangPart($ID);
// store language in session (for page related views only)
if(in_array($ACT,array('show','recent','diff','edit','preview','source','subscribe'))){
$_SESSION[DOKU_COOKIE]['translationlc'] = $lc;
}
if(!$lc) $lc = $_SESSION[DOKU_COOKIE]['translationlc'];
if(!$lc) return;
if(file_exists(DOKU_INC.'inc/lang/'.$lc.'/lang.php')) {
require(DOKU_INC.'inc/lang/'.$lc.'/lang.php');
}
$conf['lang_before_translation'] = $conf['lang']; //store for later access in syntax plugin
$conf['lang'] = $lc;
$this->locale = $lc;
return true;
}
/**
* Resort page match results so that results are ordered by translation, having the
* default language first
*/
function translation_search(&$event, $args) {
if($event->data['has_titles']){
// sort into translation slots
$res = array();
foreach($event->result as $r => $t){
$tr = $this->hlp->getLangPart($r);
if(!is_array($res["x$tr"])) $res["x$tr"] = array();
$res["x$tr"][] = array($r,$t);
}
// sort by translations
ksort($res);
// combine
$event->result = array();
foreach($res as $r){
foreach($r as $l){
$event->result[$l[0]] = $l[1];
}
}
}else{
# legacy support for old DokuWiki hooks
// sort into translation slots
$res = array();
foreach($event->result as $r){
$tr = $this->hlp->getLangPart($r);
if(!is_array($res["x$tr"])) $res["x$tr"] = array();
$res["x$tr"][] = $r;
}
// sort by translations
ksort($res);
// combine
$event->result = array();
foreach($res as $r){
$event->result = array_merge($event->result,$r);
}
}
}
}
//Setup VIM: ex: et ts=4 :